From 00d6320b71f97ecbb03635db8995f139382f8ce2 Mon Sep 17 00:00:00 2001 From: Percy Mamedy Date: Sat, 2 Sep 2017 18:12:59 +0400 Subject: [PATCH 1/2] Improvements - Added annotation registration in reader. - Code clean up. - Readme updated. --- README.md | 24 ++++++++--- composer.json | 2 +- config/pine-annotations.php | 4 +- src/Concerns/NamespaceToPathConvertable.php | 32 ++++++++++++++ src/PineAnnotationsServiceProvider.php | 27 +++--------- src/Support/Reader/AnnotationsReader.php | 47 +++++++++++++++++++++ 6 files changed, 106 insertions(+), 30 deletions(-) create mode 100644 src/Concerns/NamespaceToPathConvertable.php diff --git a/README.md b/README.md index fd64c78..fd5df7f 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ The config file ```pine-annotations.php``` has two sections ```autoload_files``` ```php 'autoload_files' => [ - // + app_path('Annotations/FooAnnotation.php'), ], ``` @@ -78,15 +78,29 @@ can include as many files as you need. ```php 'autoload_namespaces' => [ - 'App\Annotations' => app_path('Annotations'), + 'App\Annotations', ], ``` -This array contains the namespaces and paths of your annotations classes. This is useful if you want to register all annotations classes +This array contains the namespaces of your annotations classes. This is useful if you want to register all annotations classes of a whole namespace in a single go. -> It is imperative that you register either one of those arrays -> when creating your annotations classes. +#### Using the Reader to manually add entries to the Registry. + +Alternatively you may use the methods ```addFilesToRegistry()``` and ```addNamespacesToRegistry()``` to perform manual entries of +files and namespaces to the annotation registry. + +```php +// Adding files manually to the Registry. +AnnotationsReader::addFilesToRegistry($filesPaths); +``` + +```php +// Adding namespaces manually to the Registry. +AnnotationsReader::addNamespacesToRegistry($namespaces); +``` + +> It is imperative that you register your annotations classes in one way or the other with the Reader. > Otherwise AnnotationsReader won't be able to parse them. ### The AnnotationsReader diff --git a/composer.json b/composer.json index eb4f0d1..e57a9fd 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ }, "extra": { "branch-alias": { - "dev-master": "0.1.x-dev" + "dev-master": "0.2.x-dev" }, "laravel": { "providers": [ diff --git a/config/pine-annotations.php b/config/pine-annotations.php index 7eb1df5..7ea7f8f 100644 --- a/config/pine-annotations.php +++ b/config/pine-annotations.php @@ -14,7 +14,7 @@ */ 'autoload_files' => [ - // app_path('Annotations/FooAnnotation.php') + app_path('Annotations/FooAnnotation.php'), ], /* @@ -29,6 +29,6 @@ */ 'autoload_namespaces' => [ - // 'App\Annotations' => app_path('Annotations'), + 'App\Annotations', ], ]; diff --git a/src/Concerns/NamespaceToPathConvertable.php b/src/Concerns/NamespaceToPathConvertable.php new file mode 100644 index 0000000..e915c21 --- /dev/null +++ b/src/Concerns/NamespaceToPathConvertable.php @@ -0,0 +1,32 @@ +getAppNamespace(); + + // Remove the app namespace from the namespace if it is there + if (substr($namespace, 0, strlen($appNamespace)) == $appNamespace) { + $namespace = substr($namespace, strlen($appNamespace)); + } + $path = str_replace('\\', '/', trim($namespace, ' \\')); + + // trim and return the path + return ($base ?: app_path()).'/'.$path; + } +} diff --git a/src/PineAnnotationsServiceProvider.php b/src/PineAnnotationsServiceProvider.php index 8e2d4c4..34264e5 100644 --- a/src/PineAnnotationsServiceProvider.php +++ b/src/PineAnnotationsServiceProvider.php @@ -2,12 +2,10 @@ namespace LaraChimp\PineAnnotations; -use Symfony\Component\Finder\Finder; use Doctrine\Common\Annotations\Reader; use Illuminate\Support\ServiceProvider; use Doctrine\Common\Annotations\CachedReader; use Doctrine\Common\Annotations\AnnotationReader; -use Doctrine\Common\Annotations\AnnotationRegistry; use LaraChimp\PineAnnotations\Support\Reader\AnnotationsReader; use LaraChimp\PineAnnotations\Doctrine\Cache\LaravelCacheDriver; @@ -103,28 +101,13 @@ protected function registerCommands() */ protected function registerAnnotationsRegistry() { + // Creates annotation Reader. + $reader = $this->app->make(AnnotationsReader::class); + // Register files autoload. - collect(config('pine-annotations.autoload_files'))->each(function ($filePath) { - $this->registerFileAnnotation($filePath); - }); + $reader->addFilesToRegistry(config('pine-annotations.autoload_files')); // Register namespaces autoload. - collect(config('pine-annotations.autoload_namespaces'))->each(function ($dirs) { - foreach (Finder::create()->files()->name('*.php')->in($dirs) as $file) { - $this->registerFileAnnotation($file->getRealPath()); - } - }); - } - - /** - * Registers a file in th Annotation registry. - * - * @param string $filePath - * - * @return void - */ - protected function registerFileAnnotation($filePath) - { - AnnotationRegistry::registerFile($filePath); + $reader->addNamespacesToRegistry(config('pine-annotations.autoload_namespaces')); } } diff --git a/src/Support/Reader/AnnotationsReader.php b/src/Support/Reader/AnnotationsReader.php index 3b8f1e1..cb6c045 100644 --- a/src/Support/Reader/AnnotationsReader.php +++ b/src/Support/Reader/AnnotationsReader.php @@ -7,10 +7,15 @@ use ReflectionProperty; use Illuminate\Support\Str; use InvalidArgumentException; +use Symfony\Component\Finder\Finder; use Doctrine\Common\Annotations\Reader; +use Doctrine\Common\Annotations\AnnotationRegistry; +use LaraChimp\PineAnnotations\Concerns\NamespaceToPathConvertable; class AnnotationsReader { + use NamespaceToPathConvertable; + /** * The Reader instance. * @@ -112,6 +117,48 @@ public function read($argument) return $this->readAllAnnotationsFor($reflection); } + /** + * Adds files containing annotations to the registry. + * + * @param array|string $filePaths + * + * @return self + */ + public function addFilesToRegistry($filePaths) + { + collect((array) $filePaths)->each(function ($filePath) { + if (file_exists($filePath)) { + AnnotationRegistry::registerFile($filePath); + } + }); + + return $this; + } + + /** + * Adds namespaces containing annotations to the registry. + * + * @param array|string $namespaces + * + * @return $this + */ + public function addNamespacesToRegistry($namespaces) + { + collect((array) $namespaces)->each(function ($namespace) { + // Get path from namespace. + $path = $this->getPathFromNamespace($namespace); + + if (file_exists($path)) { + // Register each annotations file found in the namespace. + foreach (Finder::create()->files()->name('*.php')->in($path) as $file) { + $this->addFilesToRegistry($file->getRealPath()); + } + } + }); + + return $this; + } + /** * Reads all the annotations for the given target. * From c51069ed10f82d8c336c1c7566dc1cc0de1685f8 Mon Sep 17 00:00:00 2001 From: Percy Mamedy Date: Sat, 2 Sep 2017 18:27:34 +0400 Subject: [PATCH 2/2] Move addition to registry in boot of Provider. --- src/PineAnnotationsServiceProvider.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/PineAnnotationsServiceProvider.php b/src/PineAnnotationsServiceProvider.php index 34264e5..d669c38 100644 --- a/src/PineAnnotationsServiceProvider.php +++ b/src/PineAnnotationsServiceProvider.php @@ -21,6 +21,8 @@ public function boot() $this->publishes([ __DIR__.'/../config/pine-annotations.php' => config_path('pine-annotations.php'), ]); + + $this->addAnnotationsToRegistry(); } /** @@ -40,9 +42,6 @@ public function register() // Register commands. $this->registerCommands(); - - // Register annotations Registry. - $this->registerAnnotationsRegistry(); } /** @@ -94,12 +93,11 @@ protected function registerCommands() } /** - * Register autoload files and namespaces used - * to load annotations from. + * Add files and namespaces to the annotations registry. * * @return void */ - protected function registerAnnotationsRegistry() + protected function addAnnotationsToRegistry() { // Creates annotation Reader. $reader = $this->app->make(AnnotationsReader::class);