Skip to content

Commit

Permalink
core handlers+before detect route
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Surkov committed Aug 29, 2021
1 parent f105a52 commit 6e5d7b5
Show file tree
Hide file tree
Showing 22 changed files with 257 additions and 101 deletions.
2 changes: 1 addition & 1 deletion src/Apps/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function getId(): string
*/
public function getAppName(): string
{
return $this->has('name')?$this->get('name'):ucfirst($this->getId());
return $this->has('name')?$this->get('name'):\ucfirst($this->getId());
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/Apps/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ public function getAppName(): string
return $this['config']->getAppName();
}

public function getAppTitle(): string
{
return $this['config']->getAppName();
}

public function getRoutingProvider(): ?string
{
return $this['config']->getRoutingProvider();
Expand Down
5 changes: 0 additions & 5 deletions src/Apps/AppsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,4 @@ public function all(): array
{
return $this->apps_config;
}

public function __sleep()
{
return ['apps_config', 'apps_plugins'];
}
}
11 changes: 6 additions & 5 deletions src/Apps/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Symbiotic\Apps;


use Symbiotic\Packages\PackagesRepositoryInterface;
use Symbiotic\Core\AbstractBootstrap;
use Symbiotic\Routing\AppsRoutesRepository;
Expand All @@ -15,14 +14,16 @@ class Bootstrap extends AbstractBootstrap
public function bootstrap(CoreInterface $app): void
{

$this->cached($app, AppsRepositoryInterface::class, function ($app) {
// todo: занимает 3.5 мс при 700 пакетах, можно в файл писать, будет 2.5
$app->singleton( AppsRepositoryInterface::class, function ($app) {
$apps_repository = new AppsRepository();
foreach ($app[PackagesRepositoryInterface::class]->getPackages() as $config) {
$app = isset($config['app']) ? $config['app'] : null;
if (is_array($app)) {
$apps_repository->addApp($app);
$app_c = isset($config['app']) ? $config['app'] : null;
if (is_array($app_c)) {
$apps_repository->addApp($app_c);
}
}

return $apps_repository;
}, 'apps');

Expand Down
11 changes: 6 additions & 5 deletions src/Container/ServiceContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Symbiotic\Container;


interface ServiceContainerInterface
{
/**
* Register a service provider with the application.
*
* @param ServiceProviderInterface|string $provider
* @param \Symbiotic\Core\ServiceProviderInterface|string $provider
* @param bool $force
* @return ServiceProviderInterface
* @return \Symbiotic\Core\ServiceProviderInterface
*/
public function register($provider, $force = false);

Expand All @@ -24,15 +25,15 @@ public function boot();
/**
* Get the registered service provider instance if it exists.
*
* @param ServiceProviderInterface|string $provider
* @return ServiceProviderInterface|null
* @param \Symbiotic\Core\ServiceProviderInterface|string $provider
* @return \Symbiotic\Core\ServiceProviderInterface|null
*/
public function getProvider($provider);

/**
* Get the registered service provider instances if any exist.
*
* @param ServiceProviderInterface|string $provider
* @param \Symbiotic\Core\ServiceProviderInterface|string $provider
* @return array
*/
public function getProviders($provider);
Expand Down
20 changes: 10 additions & 10 deletions src/Container/ServiceContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public function setDeferred(array $services)
/**
* Register a service provider with the application.
*
* @param ServiceProviderInterface|string $provider
* @param \Symbiotic\Core\ServiceProviderInterface|string $provider
* @param bool $force
* @return ServiceProviderInterface
* @return \Symbiotic\Core\ServiceProviderInterface
*/
public function register($provider, $force = false)
{
/**
* @var ServiceProviderInterface $provider
* @var \Symbiotic\Core\ServiceProviderInterface $provider
*/
if (($registered = $this->getProvider($provider)) && !$force) {
return $registered;
Expand Down Expand Up @@ -136,13 +136,13 @@ public function boot()
/**
* Boot the given service provider.
*
* @param ServiceProvider $provider
* @param \Symbiotic\Core\ServiceProviderInterface $provider
* @return mixed
*/
protected function bootProvider(/*allow use all classes ServiceProviderInterface*/ $provider)
{
if (method_exists($provider, 'boot')) {
return $this->dependencyInjectionContainer->call([$provider, 'boot']);
return $provider->boot();
}
}

Expand All @@ -151,7 +151,7 @@ protected function bootProvider(/*allow use all classes ServiceProviderInterface
* Resolve a service provider instance from the class name.
*
* @param string $provider
* @return ServiceProvider
* @return \Symbiotic\Core\ServiceProviderInterface
*/
public function resolveProvider($provider)
{
Expand All @@ -162,7 +162,7 @@ public function resolveProvider($provider)
/**
* Mark the given provider as registered.
*
* @param ServiceProvider $provider
* @param \Symbiotic\Core\ServiceProviderInterface $provider
* @return void
*/
protected function markAsRegistered($provider)
Expand All @@ -175,8 +175,8 @@ protected function markAsRegistered($provider)
/**
* Get the registered service provider instance if it exists.
*
* @param ServiceProvider|string $provider
* @return ServiceProvider|null
* @param \Symbiotic\Core\ServiceProviderInterface|string $provider
* @return \Symbiotic\Core\ServiceProviderInterface|null
*/
public function getProvider($provider)
{
Expand All @@ -197,7 +197,7 @@ protected function getClass(string|object $provider)
/**
* Get the registered service provider instances if any exist.
*
* @param ServiceProvider|string $provider
* @param \Symbiotic\Core\ServiceProviderInterface|string $provider
* @return array
*/
public function getProviders($provider)
Expand Down
4 changes: 1 addition & 3 deletions src/Core/Bootstrap/ProvidersBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Symbiotic\Core\Bootstrap;

use Symbiotic\Core\AbstractBootstrap;
use Symbiotic\Core\ProvidersRepository;

use Symbiotic\Core\{AbstractBootstrap, ProvidersRepository};
use function _DS\config;

class ProvidersBootstrap extends AbstractBootstrap
Expand Down
100 changes: 93 additions & 7 deletions src/Core/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Symbiotic\Container\{DIContainerInterface, ArrayAccessTrait, SingletonTrait, Container, ServiceContainerTrait};
use Symbiotic\Core\Bootstrap\{BootBootstrap, CoreBootstrap, ProvidersBootstrap};


/**
* Class Core
* @package Symbiotic/Core
Expand Down Expand Up @@ -46,11 +45,26 @@ class Core extends Container implements CoreInterface
BootBootstrap::class,
];


/**
* используется для загрузки других скриптов после неуспешной отработки фреймворка
* @var \Closure[]|array
* @used-by Core::runNext()
*/
protected array $then = [];

/**
* Массив ключей разрешенных сервисов для кеширования
* @var array|string[]
* используется после успешной отработки фреймворка
* @var \Closure[]|array
* @used-by Core::runComplete()
* @see Core::onComplete()
*/
protected array $allow_cached = [];
protected array $complete = [];

/**
* @var \CLosure[]|array
*/
protected array $before_handle = [];

public function __construct(array $config = [])
{
Expand Down Expand Up @@ -102,9 +116,19 @@ public function runBootstrap($class): void
}
}

public function addRunner(RunnerInterface $runner): void
public function addRunner(RunnerInterface $runner, $priority = null, string $name = null): void
{
$this->runners[] = $runner;
/**
* Для подмены
*/
if(is_null($name)) {
$name = \get_class($runner);
}
/// todo: надо сделать добавление с приоритетом
/**
* [id => [object,priority],,,,]
*/
$this->runners[$name] = $runner;
}

public function run(): void
Expand All @@ -116,12 +140,74 @@ public function run(): void
*/
$runner = new $runner($this);
if ($runner->isHandle()) {
$runner->run();
$result = $runner->run();
if($result) {
$this->runComplete();
exit;
} else {
$this->runNext();
}
break;
}
}
}

public function beforeHandle(\Closure $loader): void
{
$this->before_handle = $loader;
}

/**
* событие перед обработкой
* используется для инклюда файлов необъодимых для отработки контроллера или команды
* @used-by \Symbiotic\Http\Kernel\HttpRunner::run()
*/
public function runBefore(): void
{
$before = $this->before_handle;
if (is_callable($before)) {
$this->call($before);
}
}

public function onComplete(\Closure $complete): void
{
$this->complete[] = $complete;
}

/**
* событие завершения работы
*/
public function runComplete(): void
{
foreach ($this->complete as $v) {
$this->call($v);
}
}

/**
* Используется для загрузки других скриптов после неуспешной отработки фреймворка
* Замыкание должно вернуть true, чтобы прервать цепочку после себя
*
* @param \Closure $then
*/
public function then(\Closure $then): void
{
$this->then[] = $then;
}

/**
* Запускает отработку скриптов после фреймворка
* @used-by Core::run()
*/
public function runNext(): void
{
foreach ($this->then as $v) {
if ($this->call($v)) {
return;
}
}
}

/**
* Get the base path of the Laravel installation.
Expand Down
44 changes: 43 additions & 1 deletion src/Core/CoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,49 @@ public function bootstrap(): void;

public function runBootstrap($class): void;

public function addRunner(RunnerInterface $runner): void;
public function addRunner(RunnerInterface $runner, $priority = null, string $name = null): void;

public function run(): void;

/**
* Если будет определено приложение для обработки запроса
* то выполнится функция в которой можно заинклюдить файлы
*
* @param \Closure $loader
*/
public function beforeHandle(\Closure $loader):void;

/**
* Запуск лоадера перед обработкой
*
* @used-by RunnerInterface::run()
*/
public function runBefore():void;

/**
* Запуск события после успешной отработки фреймворка
*
* @used-by RunnerInterface::run()
*/
public function runComplete():void;


/**
* @used-by CoreInterface::runComplete()
* @param \Closure $complete
*/
public function onComplete(\Closure $complete):void;
/**
* Используется для загрузки других скриптов после неуспешной отработки фреймворка
*
* @param \Closure $loader
*/
public function then(\Closure $loader):void;

/**
* Запускает отработку скриптов после фреймворка
*
* @used-by Core::run()
*/
public function runNext():void;
}
8 changes: 3 additions & 5 deletions src/Core/ProvidersRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

class ProvidersRepository
{
const EXCLUDE = 0;
const ACTIVE = 1;
const DEFER = 2;
const EXCLUDE = 1;
const ACTIVE = 3;
const DEFER = 5;
/**
* @var array
* [class => bool (active flag),... ]
Expand Down Expand Up @@ -87,7 +87,6 @@ public function load(ServiceContainerInterface $app, array $force_providers = []
$this->exclude(isset($config['providers_exclude']) ? (array)$config['providers_exclude'] : []);
}
}

$this->exclude($force_exclude);
foreach ($force_providers as $v) {
$this->providers[ltrim($v, '\\')] = self::ACTIVE;
Expand All @@ -100,7 +99,6 @@ public function load(ServiceContainerInterface $app, array $force_providers = []
$app->register($provider);
}
}

$app->setDeferred($this->defer_services);
}

Expand Down
Loading

0 comments on commit 6e5d7b5

Please sign in to comment.