- Fixed the Composer.json autoload ; now config are included inside the definition
This commit is contained in:
parent
ffe66e522c
commit
959a182d16
|
@ -29,7 +29,7 @@ class Kernel {
|
||||||
|
|
||||||
public int $errorReporting = E_ALL & ~E_USER_DEPRECATED & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE;
|
public int $errorReporting = E_ALL & ~E_USER_DEPRECATED & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE;
|
||||||
|
|
||||||
public string $definitionFilePath;
|
public array $definitionFilePaths;
|
||||||
|
|
||||||
public string $errorLogPath;
|
public string $errorLogPath;
|
||||||
|
|
||||||
|
@ -122,13 +122,22 @@ class Kernel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# $containerBuilder->useAnnotations(false);
|
foreach(Lean::getDefinitionsPathsFromComposer() as $path) {
|
||||||
|
$containerBuilder->addDefinitions($path);
|
||||||
if ($this->definitionFilePath ?? false) {
|
|
||||||
$containerBuilder->addDefinitions(Lean::autoloadDefinitionsFromComposerExtra());
|
|
||||||
$containerBuilder->addDefinitions(require($this->definitionFilePath));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach($this->definitionFilePaths ?? [] as $path) {
|
||||||
|
$containerBuilder->addDefinitions($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
$containerBuilder->addDefinitions([
|
||||||
|
'config' => function () {
|
||||||
|
return array_merge_recursive(
|
||||||
|
Lean::autoloadConfigFromComposerExtra(), []
|
||||||
|
);
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
$this->container = $containerBuilder->build();
|
$this->container = $containerBuilder->build();
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
63
src/Lean.php
63
src/Lean.php
|
@ -139,11 +139,11 @@ class Lean
|
||||||
switch($reader) {
|
switch($reader) {
|
||||||
case "php":
|
case "php":
|
||||||
$list = array_merge(...array_map(fn($app) => $app->tellPhp ?? [], $this->applications));
|
$list = array_merge(...array_map(fn($app) => $app->tellPhp ?? [], $this->applications));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "json":
|
case "json":
|
||||||
$list = array_merge(...array_map(fn($app) => $app->tellJson ?? [], $this->applications));
|
$list = array_merge(...array_map(fn($app) => $app->tellJson ?? [], $this->applications));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $list ?? false ) {
|
if ( $list ?? false ) {
|
||||||
|
@ -161,20 +161,33 @@ class Lean
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getDefinitionsPathsFromComposer() : array
|
||||||
|
{
|
||||||
|
$list = [];
|
||||||
|
|
||||||
|
foreach(Composer::readComposerLock()['packages'] as $package) {
|
||||||
|
foreach($package['extra']['lean']['autoload']['definitions'] ?? [] as $autoload) {
|
||||||
|
$list[] = static::pathFromPackage($package, $autoload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach(Composer::readComposerJson()['extra']['lean']['autoload']['definitions'] ?? [] as $autoload) {
|
||||||
|
$list[] = getenv('PROJECT_PATH') . DIRECTORY_SEPARATOR . $autoload;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $list;
|
||||||
|
}
|
||||||
|
|
||||||
public static function autoloadDefinitionsFromComposerExtra() : array
|
public static function autoloadDefinitionsFromComposerExtra() : array
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
|
|
||||||
foreach(Composer::readComposerLock()['packages'] as $package) {
|
foreach(Composer::readComposerLock()['packages'] as $package) {
|
||||||
foreach($package['extra']['lean']['autoload']['definitions'] ?? [] as $autoload) {
|
foreach($package['extra']['lean']['autoload']['definitions'] ?? [] as $autoload) {
|
||||||
$list = array_replace($list, static::loadFromPackage($package, $autoload, getenv('VENDOR_DIR') ?: dirname(__DIR__, 3)));
|
$list = array_replace($list, static::loadFromPackage($package, $autoload));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach(Composer::readComposerJson()['extra']['lean']['autoload']['definitions'] ?? [] as $autoload) {
|
|
||||||
$list = array_replace($list, static::loadFromPackage(null, $autoload, getenv('PROJECT_PATH') ?: dirname(__DIR__, 5)));
|
|
||||||
}
|
|
||||||
|
|
||||||
return $list;
|
return $list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,36 +197,50 @@ class Lean
|
||||||
|
|
||||||
foreach(Composer::readComposerLock()['packages'] as $package) {
|
foreach(Composer::readComposerLock()['packages'] as $package) {
|
||||||
foreach($package['extra']['lean']['autoload']['config'] ?? [] as $autoload) {
|
foreach($package['extra']['lean']['autoload']['config'] ?? [] as $autoload) {
|
||||||
$list = array_merge_recursive($list, static::loadFromPackage($package, $autoload, getenv('VENDOR_DIR') ?: dirname(__DIR__, 3)));
|
$list = array_merge_recursive($list, static::loadFromPackage($package, $autoload));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach(Composer::readComposerJson()['extra']['lean']['autoload']['config'] ?? [] as $autoload) {
|
foreach(Composer::readComposerJson()['extra']['lean']['autoload']['config'] ?? [] as $autoload) {
|
||||||
$list = array_merge_recursive($list, static::loadFromPackage(null, $autoload, getenv('PROJECT_PATH') ?: dirname(__DIR__, 5)));
|
$list = array_merge_recursive($list, static::loadFromPackage(null, $autoload));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $list;
|
return $list;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function loadFromPackage(?array $package, array|string $autoload, string $path) : false|array
|
protected static function pathFromPackage(?array $package, array|string $autoload) : string
|
||||||
|
{
|
||||||
|
if ($package === null) {
|
||||||
|
$filepath = getenv('PROJECT_PATH') . DIRECTORY_SEPARATOR . $autoload;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$vendor = getenv('VENDOR_DIR') ? getenv('VENDOR_PATH') : dirname(__DIR__, 3);
|
||||||
|
$filepath = $vendor . DIRECTORY_SEPARATOR . $package['name'] . DIRECTORY_SEPARATOR . $autoload;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! file_exists($filepath)) {
|
||||||
|
throw new \InvalidArgumentException("Given definition filepath do not exists '$filepath'");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $filepath;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function loadFromPackage(? array $package, array|string $autoload) : false|array
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
|
|
||||||
if (is_string($autoload)) {
|
if (is_string($autoload)) {
|
||||||
$file = $path . DIRECTORY_SEPARATOR . ($package ? $package['name'] . DIRECTORY_SEPARATOR : "") . $autoload;
|
$file = static::pathFromPackage($package, $autoload);
|
||||||
|
|
||||||
if ( ! file_exists($file) ) {
|
if ( ! file_exists($file) ) {
|
||||||
throw new \InvalidArgumentException(sprintf("Given autoload file `%s` from package `%s` was not found or is unreachable", $file, $package['name']));
|
throw new \InvalidArgumentException(sprintf("Given autoload file `%s` from package `%s` was not found or is unreachable", $autoload, $package['name']));
|
||||||
}
|
}
|
||||||
|
|
||||||
return require($file);
|
return require($file);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
$func = implode('::', array_merge([ key($autoload) ], $autoload));
|
|
||||||
|
|
||||||
return call_user_func($func);
|
$func = implode('::', array_merge([ key($autoload) ], $autoload));
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return call_user_func($func);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue