Skip to content

Commit

Permalink
Поиски DOCUMENT_ROOT в CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Aug 16, 2021
1 parent 71a1408 commit 77b066e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ LOG_FILE_PATH=/home/bitrix/logs/app.log

# При использовании Symfony Messanger
#MESSENGER_TRANSPORT_DSN=amqp://guest:guest@localhost:5672/%2f/messages

# Насильное переопределение переменных в $_SERVER (для CLI)
#HTTPS=off
#HTTP_HOST=bitrix-example.loc
#SERVER_NAME=bitrix-example.loc

# Путь к DOCUMENT_ROOT относительно корня сайта (для CLI)
# Если не задано, то будет считать путем '/bin/..'
RELATIVE_DOCUMENT_ROOT_PATH="/sites/s1"
44 changes: 39 additions & 5 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,18 +1,52 @@
#!/usr/bin/env php
<?php

use Prokl\FrameworkExtensionBundle\Services\Bitrix\LoaderBitrix;
use Prokl\CustomFrameworkExtensionsBundle\Services\Console\ConsoleCommandConfigurator;
use Symfony\Component\Dotenv\Dotenv;

@set_time_limit(0);

$_SERVER['DOCUMENT_ROOT'] = __DIR__. DIRECTORY_SEPARATOR . '..';
$GLOBALS['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'];
/** @var array $autoloadPaths Пути, где искать autoload.php композера. */
$autoloadPaths = ['/../vendor/autoload.php', '/../../current/vendor/autoload.php'];
$autoloadPath = '';

$autoloadPath = $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
foreach ($autoloadPaths as $path) {
if (file_exists($autoloadPath = realpath(__DIR__) . $path)) {
$autoloadPath = realpath(__DIR__) . $path;
break;
}
}

if (!$autoloadPath) {
die('Cannot find composer autoload.php file.' . PHP_EOL);
}

/** @noinspection PhpIncludeInspection */
require_once $autoloadPath;

$dotenv = new Dotenv();

$envFile = realpath(__DIR__.'/../') . '/.env';

if (!file_exists($envFile)) {
die('Cannot find .env file by path: ' . realpath(__DIR__.'/../' . PHP_EOL));
}
$dotenv->load($envFile);

// Попытка определить DOCUMENT_ROOT.
$documentRoot = __DIR__. '/..';
if (array_key_exists('RELATIVE_DOCUMENT_ROOT_PATH', $_ENV)) {
$documentRoot = realpath(__DIR__.'/..') . $_ENV['RELATIVE_DOCUMENT_ROOT_PATH'];
if (!file_exists($documentRoot)) {
die(
'Path to root ' . $_ENV['RELATIVE_DOCUMENT_ROOT_PATH'] . ' from RELATIVE_DOCUMENT_ROOT_PATH variable not exists' . PHP_EOL
);
}
}

$_SERVER['DOCUMENT_ROOT'] = $documentRoot;
$GLOBALS['DOCUMENT_ROOT'] = $documentRoot;

/**
* Загрузить Битрикс.
*/
Expand All @@ -21,7 +55,7 @@ $loaderBitrix->setDocumentRoot($_SERVER['DOCUMENT_ROOT']);
$loaderBitrix->initializeBitrix();

if (!$loaderBitrix->isBitrixLoaded()) {
exit('Bitrix not initialized.');
exit('Bitrix not initialized.' . PHP_EOL);
}

if (!container()->has('console.command.manager')) {
Expand Down
41 changes: 37 additions & 4 deletions bin/module
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,50 @@ use ProklUng\ModuleGenerator\Utils\ConsoleCommandConfigurator;
use ProklUng\ModuleGenerator\Utils\LoaderBitrix;
use Symfony\Component\Console\Application;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Dotenv\Dotenv;

@set_time_limit(0);

$_SERVER['DOCUMENT_ROOT'] = __DIR__ . DIRECTORY_SEPARATOR . '..';
$GLOBALS['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'];
/** @var array $autoloadPaths Пути, где искать autoload.php композера. */
$autoloadPaths = ['/../vendor/autoload.php', '/../../current/vendor/autoload.php'];
$autoloadPath = '';

$autoloadPath = $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
foreach ($autoloadPaths as $path) {
if (file_exists($autoloadPath = realpath(__DIR__) . $path)) {
$autoloadPath = realpath(__DIR__) . $path;
break;
}
}

if (!$autoloadPath) {
die('Cannot find composer autoload.php file.' . PHP_EOL);
}

/** @noinspection PhpIncludeInspection */
require_once $autoloadPath;

$dotenv = new Dotenv();

$envFile = realpath(__DIR__.'/../') . '/.env';

if (!file_exists($envFile)) {
die('Cannot find .env file by path: ' . realpath(__DIR__.'/../' . PHP_EOL));
}
$dotenv->load($envFile);

// Попытка определить DOCUMENT_ROOT.
$documentRoot = __DIR__. '/..';
if (array_key_exists('RELATIVE_DOCUMENT_ROOT_PATH', $_ENV)) {
$documentRoot = realpath(__DIR__.'/..') . $_ENV['RELATIVE_DOCUMENT_ROOT_PATH'];
if (!file_exists($documentRoot)) {
die(
'Path to root ' . $_ENV['RELATIVE_DOCUMENT_ROOT_PATH'] . ' from RELATIVE_DOCUMENT_ROOT_PATH variable not exists' . PHP_EOL
);
}
}

$_SERVER['DOCUMENT_ROOT'] = $documentRoot;
$GLOBALS['DOCUMENT_ROOT'] = $documentRoot;

/**
* Загрузить Битрикс.
*/
Expand Down

0 comments on commit 77b066e

Please sign in to comment.