Skip to content

Commit

Permalink
refactored the way initialisation is done
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc André Audet committed Jan 30, 2018
1 parent 525776f commit c16c044
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 27 deletions.
26 changes: 26 additions & 0 deletions src/Context.php
Expand Up @@ -67,6 +67,32 @@ public function register(string $providerClass): self
return $this;
}

public function route(): self
{
/** @var Events $events */
$events = $this->getSingletons()->get(Events::class);

/** @var Router $router */
$router = $this->getSingletons()->get(Router::class);

foreach ($this->getModulesManager()->getAllModules() as [$vendor_name, $module_name, $path]) {
$events->loadPath($path);
$router->loadPath($path);
}

$events->fire('Cervo/System/Before');

$route = $router->dispatch();

$events->fire('Cervo/Route/Before');
(new ControllerReflection($this, $route))();
$events->fire('Cervo/Route/After');

$events->fire('Cervo/System/After');

return $this;
}

public function getConfig(): BaseConfig
{
return $this->config;
Expand Down
38 changes: 11 additions & 27 deletions src/Core.php
Expand Up @@ -18,6 +18,8 @@
namespace Cervo;

use Cervo\Config\BaseConfig;
use Cervo\Exceptions\ControllerReflection\AlreadyInitialisedException;


/**
* Core class for Cervo.
Expand All @@ -26,39 +28,21 @@
*/
final class Core
{
private $context = null;
private static $isInit = false;
private static $context = null;

public function __construct(?BaseConfig $config = null)
public static function init(?BaseConfig $config = null)
{
$this->context = new Context($config);
}

public function init()
{
/** @var Events $events */
$events = $this->context->getSingletons()->get(Events::class);

/** @var Router $router */
$router = $this->context->getSingletons()->get(Router::class);

foreach ($this->context->getModulesManager()->getAllModules() as [$vendor_name, $module_name, $path]) {
$events->loadPath($path);
$router->loadPath($path);
if (self::$isInit === true) {
throw new AlreadyInitialisedException;
}

$events->fire('Cervo/System/Before');

$route = $router->dispatch();

$events->fire('Cervo/Route/Before');
(new ControllerReflection($this->context, $route))();
$events->fire('Cervo/Route/After');

$events->fire('Cervo/System/After');
self::$isInit = true;
self::$context = new Context($config);
}

public function getContext()
public static function getContext()
{
return $this->context;
return self::$context;
}
}
25 changes: 25 additions & 0 deletions src/Exceptions/Core/AlreadyInitialisedException.php
@@ -0,0 +1,25 @@
<?php

/**
* This file is part of the Cervo package.
*
* Copyright (c) 2010-2018 Nevraxe inc. & Marc André Audet <maudet@nevraxe.com>.
*
* @package Cervo
* @author Marc André Audet <maaudet@nevraxe.com>
* @copyright 2010 - 2018 Nevraxe inc. & Marc André Audet
* @license See LICENSE.md BSD-2-Clauses
* @link https://github.com/Nevraxe/Cervo
* @since 5.0.0
*/

declare(strict_types=1);

namespace Cervo\Exceptions\ControllerReflection;

use Cervo\Exceptions\CoreException;

class AlreadyInitialisedException extends CoreException
{

}
23 changes: 23 additions & 0 deletions src/Exceptions/CoreException.php
@@ -0,0 +1,23 @@
<?php

/**
* This file is part of the Cervo package.
*
* Copyright (c) 2010-2018 Nevraxe inc. & Marc André Audet <maudet@nevraxe.com>.
*
* @package Cervo
* @author Marc André Audet <maaudet@nevraxe.com>
* @copyright 2010 - 2018 Nevraxe inc. & Marc André Audet
* @license See LICENSE.md BSD-2-Clauses
* @link https://github.com/Nevraxe/Cervo
* @since 5.0.0
*/

declare(strict_types=1);

namespace Cervo\Exceptions;

class CoreException extends \RuntimeException
{

}

0 comments on commit c16c044

Please sign in to comment.