Skip to content

Commit

Permalink
add change get root path
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Oct 16, 2023
1 parent f15f82d commit 5a81812
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 50 deletions.
65 changes: 37 additions & 28 deletions src/Bin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ArrayAccess\TrayDigita\Kernel\Decorator;
use ArrayAccess\TrayDigita\Util\Filter\Consolidation;
use Composer\Autoload\ClassLoader;
use Composer\InstalledVersions;
use Exception;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
Expand Down Expand Up @@ -101,16 +102,26 @@ final public static function run()

if (class_exists(ClassLoader::class)) {
$classLoaderExists = true;
$exists = false;
$ref = new ReflectionClass(ClassLoader::class);
$vendor = $vendor?:dirname($ref->getFileName(), 2);
$v = $vendor;
$c = 3;
do {
$v = dirname($v);
} while (--$c > 0 && !($exists = file_exists($v . '/composer.json')));
$root = $exists ? $v : dirname($vendor);
$vendor = substr($vendor, strlen($root) + 1);
if (class_exists(InstalledVersions::class)) {
$package = InstalledVersions::getRootPackage()['install_path']??null;
$package = is_string($package) ? realpath($package) : null;
if ($package && is_dir($package)) {
$root = $package;
}
}

if (empty($root)) {
$exists = false;
$ref = new ReflectionClass(ClassLoader::class);
$vendor = $vendor ?: dirname($ref->getFileName(), 2);
$v = $vendor;
$c = 3;
do {
$v = dirname($v);
} while (--$c > 0 && !($exists = file_exists($v . '/composer.json')));
$root = $exists ? $v : dirname($vendor);
$vendor = substr($vendor, strlen($root) + 1);
}
} else {
$root = dirname(__DIR__);
$vendor = 'vendor';
Expand All @@ -128,36 +139,34 @@ final public static function run()
}

$vendorDir = $root . DIRECTORY_SEPARATOR . $vendor;
define('TD_ROOT_COMPOSER_DIR', $root);
if (!file_exists("$vendorDir/autoload.php")) {
echo "\n\033[0;31mComposer vendor directory is not exist!\033[0m\n";
if (!is_file(TD_ROOT_COMPOSER_DIR . '/composer.json')) {
echo "\033[0;31mComposer file\033[0m `composer.json` \033[0;31mis not"
." exists! Please check you application.\033[0m\n";
if (!$classLoaderExists) {
if (!file_exists("$vendorDir/autoload.php")) {
echo "\n\033[0;31mComposer vendor directory is not exist!\033[0m\n";
if (!is_file(TD_ROOT_COMPOSER_DIR . '/composer.json')) {
echo "\033[0;31mComposer file\033[0m `composer.json` \033[0;31mis not"
. " exists! Please check you application.\033[0m\n";
echo "\n";
exit(255);
}
echo "\n";
echo "\033[0;34mPlease install dependencies via \033[0m\033[0;32m`composer install`\033[0m\n";
echo "\n";
exit(255);
}
echo "\n";
echo "\033[0;34mPlease install dependencies via \033[0m\033[0;32m`composer install`\033[0m\n";
echo "\n";
exit(255);
}

if (!$classLoaderExists) {
// INCLUDE COMPOSER AUTOLOADER
require "$root/$vendor/autoload.php";
require "$vendorDir/autoload.php";
}

define('TD_ROOT_COMPOSER_DIR', $root);
// move to root
chdir($root);
# TD_APP_DIRECTORY='app' php bin/console
if (!defined('TD_APP_DIRECTORY')) {
$appDir = getenv('TD_APP_DIRECTORY')?: null;
if ($appDir && is_string($appDir)) {
$appDir = realpath($appDir) ?: (
realpath($cwd . DIRECTORY_SEPARATOR . $appDir) ?: (
realpath(TD_ROOT_COMPOSER_DIR . DIRECTORY_SEPARATOR . $appDir) ?: null
)
realpath($cwd . DIRECTORY_SEPARATOR . $appDir) ?: (
realpath(TD_ROOT_COMPOSER_DIR . DIRECTORY_SEPARATOR . $appDir) ?: null
)
);
}

Expand Down
26 changes: 18 additions & 8 deletions src/HttpKernel/Traits/HttpKernelInitTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ArrayAccess\TrayDigita\Util\Parser\DotEnv;
use Closure;
use Composer\Autoload\ClassLoader;
use Composer\InstalledVersions;
use DateTimeZone;
use ReflectionClass;
use Symfony\Component\Yaml\Yaml;
Expand Down Expand Up @@ -127,14 +128,23 @@ final public function init() : static
$this->hasInit = true;
$root = null;
if (class_exists(ClassLoader::class)) {
$ref = new ReflectionClass(ClassLoader::class);
$vendor = dirname($ref->getFileName(), 2);
$v = $vendor;
$c = 3;
do {
$v = dirname($v);
} while (--$c > 0 && !($exists = file_exists($v . '/composer.json')));
$root = ($exists??false) ? $v : dirname($vendor);
if (class_exists(InstalledVersions::class)) {
$package = InstalledVersions::getRootPackage()['install_path']??null;
$package = is_string($package) ? realpath($package) : null;
if ($package && is_dir($package)) {
$root = $package;
}
}
if (empty($root)) {
$ref = new ReflectionClass(ClassLoader::class);
$vendor = dirname($ref->getFileName(), 2);
$v = $vendor;
$c = 3;
do {
$v = dirname($v);
} while (--$c > 0 && !($exists = file_exists($v . '/composer.json')));
$root = ($exists ?? false) ? $v : dirname($vendor);
}
} elseif (!is_dir(dirname(TD_APP_DIRECTORY) .'/vendor')) {
$v = TD_APP_DIRECTORY;
$c = 3;
Expand Down
38 changes: 24 additions & 14 deletions src/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ArrayAccess\TrayDigita\Kernel\Kernel;
use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper;
use Composer\Autoload\ClassLoader;
use Composer\InstalledVersions;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
Expand Down Expand Up @@ -116,14 +117,23 @@ final public static function serve()
$classLoaderExists = true;
$exists = false;
$ref = new ReflectionClass(ClassLoader::class);
$vendor = $vendor?:dirname($ref->getFileName(), 2);
$v = $vendor;
$c = 3;
do {
$v = dirname($v);
} while (--$c > 0 && !($exists = file_exists($v . '/composer.json')));
$root = $exists ? $v : dirname($vendor);
$vendor = substr($vendor, strlen($root) + 1);
if (class_exists(InstalledVersions::class)) {
$package = InstalledVersions::getRootPackage()['install_path']??null;
$package = is_string($package) ? realpath($package) : null;
if ($package && is_dir($package)) {
$root = $package;
}
}
if (empty($root)) {
$vendor = $vendor ?: dirname($ref->getFileName(), 2);
$v = $vendor;
$c = 3;
do {
$v = dirname($v);
} while (--$c > 0 && !($exists = file_exists($v . '/composer.json')));
$root = $exists ? $v : dirname($vendor);
$vendor = substr($vendor, strlen($root) + 1);
}
} else {
$root = dirname(__DIR__);
$vendor = 'vendor';
Expand All @@ -138,13 +148,13 @@ final public static function serve()
}
}
}
}

// CHECK COMPOSER AUTOLOADER
if (!file_exists("$root/$vendor/autoload.php")) {
throw new RuntimeException(
"Composer autoloader is not exists"
);
// CHECK COMPOSER AUTOLOADER
if (!file_exists("$root/$vendor/autoload.php")) {
throw new RuntimeException(
"Composer autoloader is not exists"
);
}
}

$publicFile = null;
Expand Down

0 comments on commit 5a81812

Please sign in to comment.