lean/src/Lean.php

117 lines
3.1 KiB
PHP

<?php
namespace Lean;
use Psr\Container\ContainerInterface;
class Lean
{
const DEFAULT_PICEA_CONTEXT = __NAMESPACE__;
protected ContainerInterface $container;
public array $applications = [];
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->loadApplications();
}
protected function loadApplications() : void
{
$list = $this->container->get('config')['lean']['autoload'] ?? [];
if (! $list ) {
throw new \Exception("You must provide at least one application to autoload within your config file ( 'lean' => 'autoload' => [] )");
}
foreach(array_filter($list) as $application) {
if ( $this->container->has($application) ) {
$this->applications[] = ( new Application() )->fromArray($this->container->get($application));
}
else {
throw new \RuntimeException("Trying to load an application '$application' which have not been configured yet");
}
}
}
public function getPiceaContext() : string
{
foreach(array_reverse($this->applications) as $apps) {
if ( $apps->piceaContext ?? null ) {
return $apps->piceaContext;
}
}
return static::DEFAULT_PICEA_CONTEXT;
}
public function getPiceaExtensions() : array
{
$list = [];
foreach(array_reverse($this->applications) as $apps) {
if ( $apps->piceaExtensions ?? null ) {
$list = array_merge($list, $apps->piceaExtensions);
}
}
return $list;
}
public function getRoutable() : array
{
return array_merge(...array_map(fn($app) => $app->routes ?? [], $this->applications));
}
public function getEntities() : array
{
return array_merge(...array_map(fn($app) => $app->entities ?? [], $this->applications));
}
public function getViewPaths() : array
{
$list = array_merge(...array_map(fn($app) => $app->views ?? [], $this->applications));
uasort($list, fn($i1, $i2) => $i1['order'] <=> $i2['order'] );
return $list;
}
public function getI18n(string $reader) : ? array
{
switch($reader) {
case "php":
$list = array_merge(...array_map(fn($app) => $app->tellPhp ?? [], $this->applications));
break;
case "json":
$list = array_merge(...array_map(fn($app) => $app->tellJson ?? [], $this->applications));
break;
}
if ( $list ?? false ) {
uasort($list, fn($i1, $i2) => $i2['order'] <=> $i1['order']);
return array_map(fn($item) => $item['path'], $list);
}
return null;
}
public static function definitions() : array
{
$path = dirname(__DIR__) . "/meta/definitions/";
return array_merge(
require($path . "http.php"),
require($path . "language.php"),
require($path . "routes.php"),
require($path . "software.php"),
require($path . "template.php"),
);
}
}