Skip to content

Commit 77b066e

Browse files
committed
Поиски DOCUMENT_ROOT в CLI
1 parent 71a1408 commit 77b066e

File tree

3 files changed

+85
-9
lines changed

3 files changed

+85
-9
lines changed

Diff for: .env.example

+9
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,12 @@ LOG_FILE_PATH=/home/bitrix/logs/app.log
1717

1818
# При использовании Symfony Messanger
1919
#MESSENGER_TRANSPORT_DSN=amqp://guest:guest@localhost:5672/%2f/messages
20+
21+
# Насильное переопределение переменных в $_SERVER (для CLI)
22+
#HTTPS=off
23+
#HTTP_HOST=bitrix-example.loc
24+
#SERVER_NAME=bitrix-example.loc
25+
26+
# Путь к DOCUMENT_ROOT относительно корня сайта (для CLI)
27+
# Если не задано, то будет считать путем '/bin/..'
28+
RELATIVE_DOCUMENT_ROOT_PATH="/sites/s1"

Diff for: bin/console

+39-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
1+
#!/usr/bin/env php
12
<?php
23

34
use Prokl\FrameworkExtensionBundle\Services\Bitrix\LoaderBitrix;
45
use Prokl\CustomFrameworkExtensionsBundle\Services\Console\ConsoleCommandConfigurator;
6+
use Symfony\Component\Dotenv\Dotenv;
57

68
@set_time_limit(0);
79

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

11-
$autoloadPath = $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
14+
foreach ($autoloadPaths as $path) {
15+
if (file_exists($autoloadPath = realpath(__DIR__) . $path)) {
16+
$autoloadPath = realpath(__DIR__) . $path;
17+
break;
18+
}
19+
}
20+
21+
if (!$autoloadPath) {
22+
die('Cannot find composer autoload.php file.' . PHP_EOL);
23+
}
1224

13-
/** @noinspection PhpIncludeInspection */
1425
require_once $autoloadPath;
1526

27+
$dotenv = new Dotenv();
28+
29+
$envFile = realpath(__DIR__.'/../') . '/.env';
30+
31+
if (!file_exists($envFile)) {
32+
die('Cannot find .env file by path: ' . realpath(__DIR__.'/../' . PHP_EOL));
33+
}
34+
$dotenv->load($envFile);
35+
36+
// Попытка определить DOCUMENT_ROOT.
37+
$documentRoot = __DIR__. '/..';
38+
if (array_key_exists('RELATIVE_DOCUMENT_ROOT_PATH', $_ENV)) {
39+
$documentRoot = realpath(__DIR__.'/..') . $_ENV['RELATIVE_DOCUMENT_ROOT_PATH'];
40+
if (!file_exists($documentRoot)) {
41+
die(
42+
'Path to root ' . $_ENV['RELATIVE_DOCUMENT_ROOT_PATH'] . ' from RELATIVE_DOCUMENT_ROOT_PATH variable not exists' . PHP_EOL
43+
);
44+
}
45+
}
46+
47+
$_SERVER['DOCUMENT_ROOT'] = $documentRoot;
48+
$GLOBALS['DOCUMENT_ROOT'] = $documentRoot;
49+
1650
/**
1751
* Загрузить Битрикс.
1852
*/
@@ -21,7 +55,7 @@ $loaderBitrix->setDocumentRoot($_SERVER['DOCUMENT_ROOT']);
2155
$loaderBitrix->initializeBitrix();
2256

2357
if (!$loaderBitrix->isBitrixLoaded()) {
24-
exit('Bitrix not initialized.');
58+
exit('Bitrix not initialized.' . PHP_EOL);
2559
}
2660

2761
if (!container()->has('console.command.manager')) {

Diff for: bin/module

+37-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,50 @@ use ProklUng\ModuleGenerator\Utils\ConsoleCommandConfigurator;
66
use ProklUng\ModuleGenerator\Utils\LoaderBitrix;
77
use Symfony\Component\Console\Application;
88
use Symfony\Component\Filesystem\Filesystem;
9+
use Symfony\Component\Dotenv\Dotenv;
910

1011
@set_time_limit(0);
1112

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

15-
$autoloadPath = $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
17+
foreach ($autoloadPaths as $path) {
18+
if (file_exists($autoloadPath = realpath(__DIR__) . $path)) {
19+
$autoloadPath = realpath(__DIR__) . $path;
20+
break;
21+
}
22+
}
23+
24+
if (!$autoloadPath) {
25+
die('Cannot find composer autoload.php file.' . PHP_EOL);
26+
}
1627

17-
/** @noinspection PhpIncludeInspection */
1828
require_once $autoloadPath;
1929

30+
$dotenv = new Dotenv();
31+
32+
$envFile = realpath(__DIR__.'/../') . '/.env';
33+
34+
if (!file_exists($envFile)) {
35+
die('Cannot find .env file by path: ' . realpath(__DIR__.'/../' . PHP_EOL));
36+
}
37+
$dotenv->load($envFile);
38+
39+
// Попытка определить DOCUMENT_ROOT.
40+
$documentRoot = __DIR__. '/..';
41+
if (array_key_exists('RELATIVE_DOCUMENT_ROOT_PATH', $_ENV)) {
42+
$documentRoot = realpath(__DIR__.'/..') . $_ENV['RELATIVE_DOCUMENT_ROOT_PATH'];
43+
if (!file_exists($documentRoot)) {
44+
die(
45+
'Path to root ' . $_ENV['RELATIVE_DOCUMENT_ROOT_PATH'] . ' from RELATIVE_DOCUMENT_ROOT_PATH variable not exists' . PHP_EOL
46+
);
47+
}
48+
}
49+
50+
$_SERVER['DOCUMENT_ROOT'] = $documentRoot;
51+
$GLOBALS['DOCUMENT_ROOT'] = $documentRoot;
52+
2053
/**
2154
* Загрузить Битрикс.
2255
*/

0 commit comments

Comments
 (0)