Skip to content

Commit

Permalink
Changed the get() static function in Core to be a shortcut for the Si…
Browse files Browse the repository at this point in the history
…ngletons get function

Fixed a bug in the router when it calls the middlewares
  • Loading branch information
maaudet-ca committed Jun 28, 2019
1 parent 7f67c66 commit 5b21fd2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/.phpstorm.meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace PHPSTORM_META
{
override(\Cervo\Singletons::get(0), map([
'' => '@'
]));

override(\Cervo\Core::get(0), map([
'' => '@'
]));
}
22 changes: 18 additions & 4 deletions src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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


/**
Expand All @@ -31,7 +32,7 @@ final class Core
/** @var bool */
private $isInit = false;

/** @var Context|null */
/** @var Context */
private $context = null;

/** @var Context|null */
Expand All @@ -40,14 +41,25 @@ final class Core
public function __construct(?BaseConfig $config = null)
{
$this->context = new Context($config);
self::$global_context = $this->context;
}

public static function get(): ?Context
public static function getCurrentContext(): ?Context
{
return self::$global_context;
}

/**
* Fetch an object from the Singletons registry, or instantialise it.
*
* @param string $className The name of the class to get as Singleton
*
* @return SingletonInterface
*/
public static function get(string $className): SingletonInterface
{
return self::$global_context->getSingletons()->get($className);
}

public function start()
{
if ($this->isInit === true) {
Expand All @@ -56,6 +68,8 @@ public function start()

$this->isInit = true;

self::$global_context = $this->context;

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

Expand All @@ -66,7 +80,7 @@ public function start()
(new ControllerReflection($this->getContext(), $router->dispatch()))();
}

public function getContext(): ?Context
public function getContext(): Context
{
return $this->context;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ private function getDispatcher(): Dispatcher\GroupCountBased

if ($this->cacheExists) {

/** @noinspection PhpIncludeInspection */
if (!is_array($dispatchData = require $this->cacheFilePath)) {
throw new InvalidRouterCacheException;
}
Expand Down Expand Up @@ -409,7 +410,6 @@ private function getBaseUri(): string
private function handleMiddlewares(array $middlewares, Route $route): void
{
foreach ($middlewares as $middleware) {

if (is_array($middleware)) {

$this->handleMiddlewares($middleware, $route);
Expand All @@ -420,7 +420,7 @@ private function handleMiddlewares(array $middlewares, Route $route): void
throw new InvalidMiddlewareException;
}

if (!(new $middleware)($route)()) {
if (!(new $middleware($route))()) {
throw new RouteMiddlewareFailedException;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Singletons.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public function __construct(Context $context)
*
* @param string $className The name of the class to get as Singleton
*
* @return SingletonInterface|Context
* @return SingletonInterface
*/
public function get(string $className)
public function get(string $className): SingletonInterface
{
if (isset($this->objects[$className]) && is_object($this->objects[$className])) {
return $this->objects[$className];
Expand Down
8 changes: 6 additions & 2 deletions src/Utils/ClassUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

namespace Cervo\Utils;

use ReflectionClass;
use ReflectionException;


/**
* Cervo provider interface.
*
Expand All @@ -36,10 +40,10 @@ public static function implements(string $class, string $interface): bool
{
try {

$reflection = new \ReflectionClass($class);
$reflection = new ReflectionClass($class);
return $reflection->implementsInterface($interface);

} catch (\ReflectionException $e) {
} catch (ReflectionException $e) {
return false;
}
}
Expand Down

0 comments on commit 5b21fd2

Please sign in to comment.