- WIP on Kernel loading and Lean's autoload. Fixed some breadcrumb inconsistencies also

This commit is contained in:
Dave M. 2024-09-25 14:37:45 -04:00
parent 45ffc6e128
commit 730ce7474e
3 changed files with 46 additions and 66 deletions

View File

@ -31,8 +31,6 @@ class Kernel {
public string $definitionFilePath; public string $definitionFilePath;
public array $definitionFilePaths = [];
public string $errorLogPath; public string $errorLogPath;
public string $projectPath; public string $projectPath;
@ -126,12 +124,11 @@ class Kernel {
} }
} }
if ($this->definitionFilePath ?? false) { # $containerBuilder->useAnnotations(false);
$containerBuilder->addDefinitions($this->definitionFilePath);
}
foreach($this->definitionFilePaths ?? [] as $path) { if ($this->definitionFilePath ?? false) {
$containerBuilder->addDefinitions($path); $containerBuilder->addDefinitions(Lean::autoloadDefinitionsFromComposerExtra());
$containerBuilder->addDefinitions(require($this->definitionFilePath));
} }
$this->container = $containerBuilder->build(); $this->container = $containerBuilder->build();

View File

@ -161,33 +161,20 @@ 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)); $list = array_replace($list, static::loadFromPackage($package, $autoload, getenv('VENDOR_DIR') ?: dirname(__DIR__, 3)));
} }
} }
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;
} }
@ -197,35 +184,26 @@ 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)); $list = array_merge_recursive($list, static::loadFromPackage($package, $autoload, getenv('VENDOR_DIR') ?: dirname(__DIR__, 3)));
} }
} }
foreach(Composer::readComposerJson()['extra']['lean']['autoload']['config'] ?? [] as $autoload) {
$list = array_merge_recursive($list, static::loadFromPackage(null, $autoload, getenv('PROJECT_PATH') ?: dirname(__DIR__, 5)));
}
return $list; return $list;
} }
protected static function pathFromPackage(array $package, array|string $autoload) : string protected static function loadFromPackage(?array $package, array|string $autoload, string $path) : false|array
{
$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 = static::pathFromPackage($package, $autoload); $file = $path . DIRECTORY_SEPARATOR . ($package ? $package['name'] . DIRECTORY_SEPARATOR : "") . $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", $autoload, $package['name'])); throw new \InvalidArgumentException(sprintf("Given autoload file `%s` from package `%s` was not found or is unreachable", $file, $package['name']));
} }
return require($file); return require($file);
@ -238,4 +216,4 @@ class Lean
return false; return false;
} }
} }

View File

@ -1,28 +1,33 @@
<ol class="breadcrumb "> {% if isset($this->breadcrumb) %}
<li class="breadcrumb-item"><a href="{% url '' %}"><i class="fas fa-fw fa-tachometer-alt"></i> Tableau de bord</a></li> {% php $routeTree = $this->breadcrumb->getRouteTree($this->session->get('lean.route')) %}
{% foreach $this->breadcrumb ?? [] as $crumb %} {% if count($routeTree) %}
<li class="breadcrumb-item {{ ( $crumb['active'] ?? false ) ? 'active' : '' }}"> <ol class="breadcrumb mb-4 px-3 py-2">
{% if ! ($crumb['active'] ?? false) %} {% foreach $routeTree as $index => $crumb %}
{? $last = $crumb ?} <li class="breadcrumb-item {% if $crumb->current %}active{% endif %}">
{% if ! $crumb->current %}
{% php $last = $crumb %}
<a href="{% url $crumb['url'] %}"> <a href="{% route $crumb->routeAnnotation->name %}">
{% if $crumb['icon'] ?? false %} {% if $crumb->icon ?? false %}
<i class="fa fa-{{ $crumb['icon'] }}"></i> <i class="fa fa-{{ $crumb->icon }}"></i>
{% endif %} {% endif %}
{{ $crumb['title'] }} <span>{{ lang($crumb->lang, get_defined_vars()) }}</span>
</a> </a>
{% else %} {% else %}
{% if $crumb['icon'] ?? false %} {% if $crumb->icon ?? false %}
<i class="fa fa-{{ $crumb['icon'] }}"></i> <i class="fa fa-{{ $crumb->icon }}"></i>
{% endif %} {% endif %}
<span>{{ $crumb['title'] }}</span>
{% endif %}
</li>
{% endforeach %}
{% if $last ?? false %} <span>{{ lang($crumb->lang, get_defined_vars()) }}</span>
<li class="breadcrumb-last-item ml-auto"><a href="{{ url( $last['url']) }}"><i class="fas fa-chevron-circle-left"></i> Retour</a></li> {% endif %}
</li>
{% endforeach %}
{# if $last ?? false %}
<li class="breadcrumb-last-item ml-auto"><a href="{{ ( $last['route'] ?? false ) ? $last['route'] : url($last['url']) }}"><i class="fas fa-chevron-circle-left"></i> Retour</a></li>
{% endif #}
</ol>
{% endif %} {% endif %}
</ol> {% endif %}