diff --git a/bundle/Spec/CirclicalAutoWire/Annotations/OtherAnnotation.php b/bundle/Spec/CirclicalAutoWire/Annotations/OtherAnnotation.php new file mode 100644 index 0000000..8197bb3 --- /dev/null +++ b/bundle/Spec/CirclicalAutoWire/Annotations/OtherAnnotation.php @@ -0,0 +1,21 @@ + + * + * @Annotation + * @Target({"METHOD","CLASS"}) + */ +final class OtherAnnotation +{ + /** + * @var string + */ + public $value; + +} diff --git a/bundle/Spec/CirclicalAutoWire/Controller/OtherAnnotationsController.php b/bundle/Spec/CirclicalAutoWire/Controller/OtherAnnotationsController.php new file mode 100644 index 0000000..475a74d --- /dev/null +++ b/bundle/Spec/CirclicalAutoWire/Controller/OtherAnnotationsController.php @@ -0,0 +1,32 @@ +parseController(SameNameBController::class); $this->compile(); } -} \ No newline at end of file + + 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]); + } +} diff --git a/src/CirclicalAutoWire/Model/AnnotatedRoute.php b/src/CirclicalAutoWire/Model/AnnotatedRoute.php index ab90b64..5ad2c60 100644 --- a/src/CirclicalAutoWire/Model/AnnotatedRoute.php +++ b/src/CirclicalAutoWire/Model/AnnotatedRoute.php @@ -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' => [ @@ -85,4 +85,4 @@ private function identifyRoute(string $route): string return Literal::class; } -} \ No newline at end of file +} diff --git a/src/CirclicalAutoWire/Module.php b/src/CirclicalAutoWire/Module.php index 3b5d409..382fe9b 100644 --- a/src/CirclicalAutoWire/Module.php +++ b/src/CirclicalAutoWire/Module.php @@ -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(); @@ -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()); + } } } -} \ No newline at end of file +} diff --git a/src/CirclicalAutoWire/Service/RouterService.php b/src/CirclicalAutoWire/Service/RouterService.php index 6279102..07ba0cb 100644 --- a/src/CirclicalAutoWire/Service/RouterService.php +++ b/src/CirclicalAutoWire/Service/RouterService.php @@ -51,6 +51,7 @@ 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 @@ -58,9 +59,13 @@ public function parseController(string $controllerClass) /** @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); }