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

View File

@ -161,33 +161,20 @@ class Lean
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
{
$list = [];
foreach(Composer::readComposerLock()['packages'] as $package) {
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;
}
@ -197,35 +184,26 @@ class Lean
foreach(Composer::readComposerLock()['packages'] as $package) {
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;
}
protected static function pathFromPackage(array $package, array|string $autoload) : string
{
$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
protected static function loadFromPackage(?array $package, array|string $autoload, string $path) : false|array
{
$list = [];
if (is_string($autoload)) {
$file = static::pathFromPackage($package, $autoload);
$file = $path . DIRECTORY_SEPARATOR . ($package ? $package['name'] . DIRECTORY_SEPARATOR : "") . $autoload;
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);
@ -238,4 +216,4 @@ class Lean
return false;
}
}
}

View File

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