Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Merge pull request #4 from LaraChimp/master
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
percymamedy authored Sep 2, 2017
2 parents 99a42f9 + c51069e commit 72654f6
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 36 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ The config file ```pine-annotations.php``` has two sections ```autoload_files```

```php
'autoload_files' => [
//
app_path('Annotations/FooAnnotation.php'),
],
```

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.1.x-dev"
"dev-master": "0.2.x-dev"
},
"laravel": {
"providers": [
Expand Down
4 changes: 2 additions & 2 deletions config/pine-annotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

'autoload_files' => [
// app_path('Annotations/FooAnnotation.php')
app_path('Annotations/FooAnnotation.php'),
],

/*
Expand All @@ -29,6 +29,6 @@
*/

'autoload_namespaces' => [
// 'App\Annotations' => app_path('Annotations'),
'App\Annotations',
],
];
32 changes: 32 additions & 0 deletions src/Concerns/NamespaceToPathConvertable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace LaraChimp\PineAnnotations\Concerns;

use Illuminate\Console\DetectsApplicationNamespace;

trait NamespaceToPathConvertable
{
use DetectsApplicationNamespace;

/**
* Convert the given namespace to a file path.
*
* @param string $namespace
* @param string $base
*
* @return string
*/
public function getPathFromNamespace($namespace, $base = null)
{
$appNamespace = $this->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;
}
}
37 changes: 9 additions & 28 deletions src/PineAnnotationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,6 +21,8 @@ public function boot()
$this->publishes([
__DIR__.'/../config/pine-annotations.php' => config_path('pine-annotations.php'),
]);

$this->addAnnotationsToRegistry();
}

/**
Expand All @@ -42,9 +42,6 @@ public function register()

// Register commands.
$this->registerCommands();

// Register annotations Registry.
$this->registerAnnotationsRegistry();
}

/**
Expand Down Expand Up @@ -96,35 +93,19 @@ 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);

// 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'));
}
}
47 changes: 47 additions & 0 deletions src/Support/Reader/AnnotationsReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 72654f6

Please sign in to comment.