composer package updates
[openemr.git] / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping / Driver / AnnotationDriver.php
blobdeb82c0c97922bc996206c1c51decf89191b9c81
1 <?php
2 /*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common\Persistence\Mapping\Driver;
22 use Doctrine\Common\Annotations\AnnotationReader;
23 use Doctrine\Common\Persistence\Mapping\MappingException;
25 /**
26 * The AnnotationDriver reads the mapping metadata from docblock annotations.
28 * @since 2.2
29 * @author Benjamin Eberlei <kontakt@beberlei.de>
30 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
31 * @author Jonathan H. Wage <jonwage@gmail.com>
32 * @author Roman Borschel <roman@code-factory.org>
34 abstract class AnnotationDriver implements MappingDriver
36 /**
37 * The AnnotationReader.
39 * @var AnnotationReader
41 protected $reader;
43 /**
44 * The paths where to look for mapping files.
46 * @var array
48 protected $paths = [];
50 /**
51 * The paths excluded from path where to look for mapping files.
53 * @var array
55 protected $excludePaths = [];
57 /**
58 * The file extension of mapping documents.
60 * @var string
62 protected $fileExtension = '.php';
64 /**
65 * Cache for AnnotationDriver#getAllClassNames().
67 * @var array|null
69 protected $classNames;
71 /**
72 * Name of the entity annotations as keys.
74 * @var array
76 protected $entityAnnotationClasses = [];
78 /**
79 * Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
80 * docblock annotations.
82 * @param AnnotationReader $reader The AnnotationReader to use, duck-typed.
83 * @param string|array|null $paths One or multiple paths where mapping classes can be found.
85 public function __construct($reader, $paths = null)
87 $this->reader = $reader;
88 if ($paths) {
89 $this->addPaths((array) $paths);
93 /**
94 * Appends lookup paths to metadata driver.
96 * @param array $paths
98 * @return void
100 public function addPaths(array $paths)
102 $this->paths = array_unique(array_merge($this->paths, $paths));
106 * Retrieves the defined metadata lookup paths.
108 * @return array
110 public function getPaths()
112 return $this->paths;
116 * Append exclude lookup paths to metadata driver.
118 * @param array $paths
120 public function addExcludePaths(array $paths)
122 $this->excludePaths = array_unique(array_merge($this->excludePaths, $paths));
126 * Retrieve the defined metadata lookup exclude paths.
128 * @return array
130 public function getExcludePaths()
132 return $this->excludePaths;
136 * Retrieve the current annotation reader
138 * @return AnnotationReader
140 public function getReader()
142 return $this->reader;
146 * Gets the file extension used to look for mapping files under.
148 * @return string
150 public function getFileExtension()
152 return $this->fileExtension;
156 * Sets the file extension used to look for mapping files under.
158 * @param string $fileExtension The file extension to set.
160 * @return void
162 public function setFileExtension($fileExtension)
164 $this->fileExtension = $fileExtension;
168 * Returns whether the class with the specified name is transient. Only non-transient
169 * classes, that is entities and mapped superclasses, should have their metadata loaded.
171 * A class is non-transient if it is annotated with an annotation
172 * from the {@see AnnotationDriver::entityAnnotationClasses}.
174 * @param string $className
176 * @return boolean
178 public function isTransient($className)
180 $classAnnotations = $this->reader->getClassAnnotations(new \ReflectionClass($className));
182 foreach ($classAnnotations as $annot) {
183 if (isset($this->entityAnnotationClasses[get_class($annot)])) {
184 return false;
187 return true;
191 * {@inheritDoc}
193 public function getAllClassNames()
195 if ($this->classNames !== null) {
196 return $this->classNames;
199 if (!$this->paths) {
200 throw MappingException::pathRequired();
203 $classes = [];
204 $includedFiles = [];
206 foreach ($this->paths as $path) {
207 if ( ! is_dir($path)) {
208 throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
211 $iterator = new \RegexIterator(
212 new \RecursiveIteratorIterator(
213 new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
214 \RecursiveIteratorIterator::LEAVES_ONLY
216 '/^.+' . preg_quote($this->fileExtension) . '$/i',
217 \RecursiveRegexIterator::GET_MATCH
220 foreach ($iterator as $file) {
221 $sourceFile = $file[0];
223 if ( ! preg_match('(^phar:)i', $sourceFile)) {
224 $sourceFile = realpath($sourceFile);
227 foreach ($this->excludePaths as $excludePath) {
228 $exclude = str_replace('\\', '/', realpath($excludePath));
229 $current = str_replace('\\', '/', $sourceFile);
231 if (strpos($current, $exclude) !== false) {
232 continue 2;
236 require_once $sourceFile;
238 $includedFiles[] = $sourceFile;
242 $declared = get_declared_classes();
244 foreach ($declared as $className) {
245 $rc = new \ReflectionClass($className);
246 $sourceFile = $rc->getFileName();
247 if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) {
248 $classes[] = $className;
252 $this->classNames = $classes;
254 return $classes;