220 lines
6.5 KiB
PHP
220 lines
6.5 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($application) )->fromArray($this->container->get($application));
|
|
}
|
|
else {
|
|
throw new \RuntimeException("Trying to load an application '$application' which have not been configured yet");
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getApplication(string $name) : ? Application
|
|
{
|
|
foreach($this->applications as $app) {
|
|
if ($app->name === $name) {
|
|
return $app;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function getPiceaContext() /*: string|array */
|
|
{
|
|
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 getCronard() : array
|
|
{
|
|
return array_merge(...array_map(fn($app) => $app->cronard ?? [], $this->applications));
|
|
}
|
|
|
|
public function getCLI() : array
|
|
{
|
|
return array_merge(...array_map(fn($app) => $app->cli ?? [], $this->applications));
|
|
}
|
|
|
|
public function getEvents() : array
|
|
{
|
|
return array_merge(...array_map(fn($app) => $app->events ?? [], $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));
|
|
|
|
$this->verifyPathList($list);
|
|
|
|
uasort($list, fn($i1, $i2) => $i1['order'] <=> $i2['order'] );
|
|
|
|
return $list;
|
|
}
|
|
|
|
public function getAssetPaths() : array
|
|
{
|
|
$list = array_merge(...array_map(fn($app) => $app->piceaAssets ?? [], $this->applications));
|
|
|
|
$this->verifyPathList($list);
|
|
|
|
uasort($list, fn($i1, $i2) => $i1['order'] <=> $i2['order'] );
|
|
|
|
return $list;
|
|
}
|
|
|
|
protected function verifyPathList(array $list) : void
|
|
{
|
|
foreach($list as $item) {
|
|
if (! isset($item['order']) ) {
|
|
throw new \RuntimeException(sprintf("An error occured while verifying a path list (%s)", json_encode($item, \JSON_PRETTY_PRINT)));
|
|
}
|
|
}
|
|
}
|
|
|
|
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_replace(
|
|
class_exists(\Mcnd\CLI\CliMiddleware::class) ? require($path . "cli.php") : [],
|
|
class_exists(\Cronard\CronardMiddleware::class) ? require($path . "cronard.php") : [],
|
|
require($path . "email.php"),
|
|
require($path . "event.php"),
|
|
require($path . "http.php"),
|
|
require($path . "language.php"),
|
|
class_exists(\Negundo\Client\NegundoMiddleware::class) ? require($path . "negundo.php") : [],
|
|
require($path . "routes.php"),
|
|
# require($path . "security.php"),
|
|
require($path . "software.php"),
|
|
class_exists(\Picea\Picea::class) ? require($path . "template.php") : [],
|
|
);
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
public static function autoloadConfigFromComposerExtra() : array
|
|
{
|
|
$list = [];
|
|
|
|
foreach(Composer::readComposerLock()['packages'] as $package) {
|
|
foreach($package['extra']['lean']['autoload']['config'] ?? [] as $autoload) {
|
|
$list = array_merge_recursive($list, static::loadFromPackage($package, $autoload));
|
|
}
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
protected static function loadFromPackage(array $package, array|string $autoload) : false|array
|
|
{
|
|
$list = [];
|
|
|
|
if (is_string($autoload)) {
|
|
$vendor = getenv('VENDOR_DIR') ? getenv('VENDOR_PATH') : dirname(__DIR__, 3);
|
|
$file = $vendor . DIRECTORY_SEPARATOR . $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']));
|
|
}
|
|
|
|
return require($file);
|
|
}
|
|
else {
|
|
$func = implode('::', array_merge([ key($autoload) ], $autoload));
|
|
|
|
return call_user_func($func);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} |