-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'custom-annotations' of https://github.com/tedslittlerob…
…ot/annotations into 5.0 Signed-off-by: Adam Engebretson <adam@enge.me> Conflicts: src/AnnotationsServiceProvider.php
- Loading branch information
Showing
9 changed files
with
337 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php namespace Collective\Annotations; | ||
|
||
use Collective\Annotations\NamespaceToPathConverterTrait; | ||
use Doctrine\Common\Annotations\AnnotationRegistry; | ||
use Doctrine\Common\Annotations\SimpleAnnotationReader; | ||
use ReflectionClass; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
abstract class AnnotationScanner { | ||
|
||
use NamespaceToPathConverterTrait; | ||
|
||
/** | ||
* Namespaces to check for annotation reader annotation classes. | ||
* | ||
* @var string | ||
*/ | ||
protected $namespaces = []; | ||
|
||
/** | ||
* The paths to scan for annotations. | ||
* | ||
* @var array | ||
*/ | ||
protected $scan = []; | ||
|
||
/** | ||
* Create a new scanner instance. | ||
* | ||
* @param array $scan | ||
* @return void | ||
*/ | ||
public function __construct(array $scan) | ||
{ | ||
$this->scan = $scan; | ||
} | ||
|
||
/** | ||
* Create a new scanner instance. | ||
* | ||
* @param array $scan | ||
* @return static | ||
*/ | ||
public static function create(array $scan) | ||
{ | ||
return new static($scan); | ||
} | ||
|
||
/** | ||
* Get all of the ReflectionClass instances in the scan array. | ||
* | ||
* @return array | ||
*/ | ||
protected function getClassesToScan() | ||
{ | ||
$classes = []; | ||
|
||
foreach ($this->scan as $class) | ||
{ | ||
try | ||
{ | ||
$classes[] = new ReflectionClass($class); | ||
} | ||
catch (Exception $e) | ||
{ | ||
// | ||
} | ||
} | ||
|
||
return $classes; | ||
} | ||
|
||
/** | ||
* Set the classes to scan | ||
* | ||
* @param array $scans | ||
*/ | ||
public function setClassesToScan( array $scans ) | ||
{ | ||
$this->scan = $scans; | ||
} | ||
|
||
/** | ||
* Add an annotation namespace for the SimpleAnnotationReader instance. | ||
* | ||
* If the second parameter is null, it will assume the namespace is PSR-4'd | ||
* inside your app folder. | ||
* | ||
* @param string $namespace | ||
* @param string $path | ||
*/ | ||
public function addAnnotationNamespace($namespace, $path = null) | ||
{ | ||
$this->namespaces[] = $namespace; | ||
|
||
return $this->registerAnnotationsPathWithRegistry( | ||
$path ?: $this->getPathFromNamespace( $namespace ) | ||
); | ||
} | ||
|
||
/** | ||
* Register the annotator files with the annotation registry | ||
* | ||
* @param string $path | ||
* @return $this | ||
*/ | ||
public function registerAnnotationsPathWithRegistry( $path ) | ||
{ | ||
foreach (Finder::create()->files()->in( $path ) as $file) | ||
{ | ||
AnnotationRegistry::registerFile($file->getRealPath()); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get an annotation reader instance. | ||
* | ||
* @return \Doctrine\Common\Annotations\SimpleAnnotationReader | ||
*/ | ||
protected function getReader() | ||
{ | ||
$reader = new SimpleAnnotationReader; | ||
|
||
foreach ($this->namespaces as $namespace) | ||
$reader->addNamespace($namespace); | ||
|
||
return $reader; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.