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 #3 from LaraChimp/master
Browse files Browse the repository at this point in the history
Autoload config and Docs update
  • Loading branch information
percymamedy authored Aug 31, 2017
2 parents dd095bf + 3247981 commit 99a42f9
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 15 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,44 @@ You can also register the ```Reader``` alias which helps in reading object's ann
]
```

You can now publish config using the command:

```bash
$ php artisan vendor:publish
```

This will create the file ```pine-annotations.php``` in your config directory.

### Registering your annotations in the AnnotationRegistry

The config file ```pine-annotations.php``` has two sections ```autoload_files``` and ```autoload_namespaces```.

#### autoload_files

```php
'autoload_files' => [
//
],
```

In this array you may include the file paths of all your annotations classes. These will get registered in the ```AnnotationRegistry```. You
can include as many files as you need.

#### autoload_namespaces

```php
'autoload_namespaces' => [
'App\Annotations' => app_path('Annotations'),
],
```

This array contains the namespaces and paths 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.
> Otherwise AnnotationsReader won't be able to parse them.
### The AnnotationsReader

To create an instance of the ```AnnotationsReader```, use the Laravel's IOC to either inject or create it via the
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"php": ">=5.6.0",
"doctrine/annotations": "~1.4",
"doctrine/cache": "^1.6",
"illuminate/support": "~5.0"
"illuminate/support": "~5.0",
"symfony/finder": "^3.3"
},
"require-dev": {
"orchestra/testbench": "~3.0",
Expand Down
34 changes: 34 additions & 0 deletions config/pine-annotations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Autoload Files.
|--------------------------------------------------------------------------
|
| You may add files which you want pine annotations to autoload. Annotations
| in these files will be valid for the Reader to parse. Feel free
| to add as many files as you need.
|
*/

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

/*
|--------------------------------------------------------------------------
| Autoload Namespaces
|--------------------------------------------------------------------------
|
| You may add namespaces here which you want pine annotations to autoload.
| Annotations in these namespaces will be valid for the Reader to
| parse. Feel free to add as many files as you need.
|
*/

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

namespace LaraChimp\PineAnnotations\Console;

use Illuminate\Console\GeneratorCommand;

class MakeAnnotationCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:annotation';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new annotation class';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Annotation';

/**
* Get the stub file for the generator.
*
* @return string
*/
public function getStub()
{
return __DIR__.'/stubs/annotation_all.stub';
}
}
18 changes: 18 additions & 0 deletions src/Console/stubs/annotation_all.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace DummyNamespace;

use Doctrine\Common\Annotations\Annotation\Target;

/**
* DummyClass annotation.
*
* @Annotation
* @Target("ALL")
*/
class DummyClass
{
//
}


83 changes: 69 additions & 14 deletions src/PineAnnotationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@

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;

class PineAnnotationsServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
* Perform post-registration booting of services.
*
* @var bool
* @return void
*/
protected $defer = true;
public function boot()
{
$this->publishes([
__DIR__.'/../config/pine-annotations.php' => config_path('pine-annotations.php'),
]);
}

/**
* Register the application services.
Expand All @@ -25,20 +32,19 @@ class PineAnnotationsServiceProvider extends ServiceProvider
*/
public function register()
{
// Merge Configs.
$this->mergeConfigFrom(
__DIR__.'/../config/pine-annotations.php', 'pine-annotations'
);

// Register annotations reader.
$this->registerReader();
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
AnnotationsReader::class,
];
// Register commands.
$this->registerCommands();

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

/**
Expand Down Expand Up @@ -72,4 +78,53 @@ protected function createAndReturnCachedReader()
(bool) config('app.debug')
);
}

/**
* Registers PineAnnotations Commands.
*
* @return void
*/
protected function registerCommands()
{
// Make sure we are running in console
// then we registers commands.
if ($this->app->runningInConsole()) {
$this->commands([
Console\MakeAnnotationCommand::class,
]);
}
}

/**
* Register autoload files and namespaces used
* to load annotations from.
*
* @return void
*/
protected function registerAnnotationsRegistry()
{
// Register files autoload.
collect(config('pine-annotations.autoload_files'))->each(function ($filePath) {
$this->registerFileAnnotation($filePath);
});

// 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);
}
}

0 comments on commit 99a42f9

Please sign in to comment.