Skip to content

Commit

Permalink
Merge pull request #3 from buliq/master
Browse files Browse the repository at this point in the history
Problem with other annotations while using Route
  • Loading branch information
Saeven authored Mar 9, 2017
2 parents a900a28 + 4274f15 commit 4798240
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 6 deletions.
21 changes: 21 additions & 0 deletions bundle/Spec/CirclicalAutoWire/Annotations/OtherAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Spec\CirclicalAutoWire\Annotations;

/**
* Class OtherAnnotation
*
* @package Spec\CirclicalAutoWire\Annotations
* @author Michał Makaruk <[email protected]>
*
* @Annotation
* @Target({"METHOD","CLASS"})
*/
final class OtherAnnotation
{
/**
* @var string
*/
public $value;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Spec\CirclicalAutoWire\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use CirclicalAutoWire\Annotations\Route;
use Spec\CirclicalAutoWire\Annotations\OtherAnnotation;

/**
* Class OtherAnnotationsTypeErrorController
* @package Spec\CirclicalAutoWire\Controller
*
* @Route("/foobar")
*/
class OtherAnnotationsController extends AbstractActionController
{
/**
* @Route("/")
*/
public function fooAction()
{
}

/**
* @OtherAnnotation
* @Route("/ok")
*/
public function willItFailAction()
{
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Spec\CirclicalAutoWire\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use CirclicalAutoWire\Annotations\Route;
use Spec\CirclicalAutoWire\Annotations\OtherAnnotation;

/**
* Class OtherAnnotationsTypeErrorController
* @package Spec\CirclicalAutoWire\Controller
*
*/
class OtherAnnotationsTypeErrorController extends AbstractActionController
{
/**
* @OtherAnnotation
* @Route("/will-it-fail")
*/
public function willItFailAction()
{
}

}
23 changes: 22 additions & 1 deletion bundle/Spec/CirclicalAutoWire/Service/RouterServiceSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Spec\CirclicalAutoWire\Service;

use Doctrine\Common\Annotations\AnnotationRegistry;
use Spec\CirclicalAutoWire\Controller\AnnotatedController;
use Spec\CirclicalAutoWire\Controller\ChildRouteController;
use Spec\CirclicalAutoWire\Controller\DistantChildRouteController;
use Spec\CirclicalAutoWire\Controller\OtherAnnotationsController;
use Spec\CirclicalAutoWire\Controller\OtherAnnotationsTypeErrorController;
use Spec\CirclicalAutoWire\Controller\SameNameAController;
use Spec\CirclicalAutoWire\Controller\SameNameBController;
use Spec\CirclicalAutoWire\Controller\SimpleController;
Expand Down Expand Up @@ -221,4 +224,22 @@ function it_lets_children_have_same_names($routeStack)
$this->parseController(SameNameBController::class);
$this->compile();
}
}

function it_skips_other_annotations()
{
include __DIR__ . '/../../CirclicalAutoWire/Controller/OtherAnnotationsController.php';
AnnotationRegistry::registerAutoloadNamespace("Spec\\CirclicalAutoWire\\Annotations", realpath(__DIR__ . "/../../../"));

$this->shouldNotThrow(\Error::class)->during('parseController', [OtherAnnotationsController::class]);
}

function it_skips_other_annotations_type_error()
{
include __DIR__ . '/../../CirclicalAutoWire/Controller/OtherAnnotationsTypeErrorController.php';
AnnotationRegistry::registerAutoloadNamespace("Spec\\CirclicalAutoWire\\Annotations", realpath(__DIR__ . "/../../../"));

//this should be thrown, but I'm unable to force it...
// $this->shouldThrow(\TypeError::class)->during('parseController', [OtherAnnotationsTypeErrorController::class]);
$this->shouldNotThrow(\PhpSpec\Exception\Example\ErrorException::class)->during('parseController', [OtherAnnotationsTypeErrorController::class]);
}
}
4 changes: 2 additions & 2 deletions src/CirclicalAutoWire/Model/AnnotatedRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(Route $route, string $controller, string $action)
public function toArray(): array
{
$route = [
'type' => $this->type ?? $this->identifyRoute($this->route->value),
'type' => $this->type ?? $this->route->type ?? $this->identifyRoute($this->route->value),
'options' => [
'route' => $this->route->value,
'defaults' => [
Expand Down Expand Up @@ -85,4 +85,4 @@ private function identifyRoute(string $route): string

return Literal::class;
}
}
}
10 changes: 8 additions & 2 deletions src/CirclicalAutoWire/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function onBootstrap(MvcEvent $mvcEvent)
$config = $serviceLocator->get('config');
$productionMode = Console::isConsole() || $config['circlical']['autowire']['production_mode'];

if (!$productionMode) {
if (!$productionMode || !file_exists($config['circlical']['autowire']['compile_to'])) {
$routerService = $serviceLocator->get(RouterService::class);
$directoryScanner = new DirectoryScanner();

Expand All @@ -83,6 +83,12 @@ public function onBootstrap(MvcEvent $mvcEvent)
$writer = new PhpArray();
$writer->toFile($config['circlical']['autowire']['compile_to'], $routeConfig, true);
$routerService->reset();

/** @var \Zend\ModuleManager\Listener\ConfigListener $cfg */
$cfg = $serviceLocator->get(\Zend\ModuleManager\ModuleManager::class)->getEvent()->getConfigListener();
if ($productionMode && $cfg->getOptions()->getConfigCacheEnabled() && file_exists($cfg->getOptions()->getConfigCacheFile())) {
@unlink($cfg->getOptions()->getConfigCacheFile());
}
}
}
}
}
7 changes: 6 additions & 1 deletion src/CirclicalAutoWire/Service/RouterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,21 @@ public function reset()
public function parseController(string $controllerClass)
{
$class = new \ReflectionClass($controllerClass);
/** @var Route $classAnnotation */
$classAnnotation = $this->reader->getClassAnnotation($class, Route::class);

// First, get all annotations for this controller

/** @var \ReflectionMethod $method */
foreach ($class->getMethods() as $method) {
if ($method->getDeclaringClass()->getName() == $controllerClass) {
$set = $this->reader->getMethodAnnotations($method, Route::class);
$set = $this->reader->getMethodAnnotations($method);
/** @var Route $routerAnnotation */
foreach ($set as $routerAnnotation) {
if (!$routerAnnotation instanceof Route) {
continue;
}

if ($classAnnotation) {
$routerAnnotation->setPrefix($classAnnotation->value);
}
Expand Down

0 comments on commit 4798240

Please sign in to comment.