Permalink
Browse files

[Annotations] imported new library

  • Loading branch information...
1 parent 2c92a60 commit 1dce868706eceda95f5bb67bf1f7ad9a2d32fe13 @schmittjoh schmittjoh committed May 16, 2011
Showing with 3,481 additions and 10 deletions.
  1. +81 −0 lib/Doctrine/Common/Annotations/Annotation/Annotation.php
  2. +47 −0 lib/Doctrine/Common/Annotations/Annotation/IgnoreAnnotation.php
  3. +20 −0 lib/Doctrine/Common/Annotations/Annotation/ParseAnnotation.php
  4. +34 −0 lib/Doctrine/Common/Annotations/Cache/CacheInterface.php
  5. +128 −0 lib/Doctrine/Common/Annotations/Cache/FileCache.php
  6. +61 −0 lib/Doctrine/Common/Annotations/Cache/InMemoryCache.php
  7. +86 −0 lib/Doctrine/Common/Annotations/CachedReader.php
  8. +343 −0 lib/Doctrine/Common/Annotations/DocLexer.php
  9. +585 −0 lib/Doctrine/Common/Annotations/DocParser.php
  10. +33 −0 lib/Doctrine/Common/Annotations/Exception/Exception.php
  11. +33 −0 lib/Doctrine/Common/Annotations/Exception/SemanticalException.php
  12. +33 −0 lib/Doctrine/Common/Annotations/Exception/SyntaxException.php
  13. +156 −0 lib/Doctrine/Common/Annotations/PhpParser.php
  14. +321 −0 lib/Doctrine/Common/Annotations/Reader.php
  15. +39 −0 lib/Doctrine/Common/Annotations/ReaderInterface.php
  16. +6 −0 phpunit.xml.dist
  17. +372 −0 tests/Doctrine/Tests/Common/Annotations/AbstractReaderTest.php
  18. +15 −0 tests/Doctrine/Tests/Common/Annotations/CachedReaderTest.php
  19. +27 −0 tests/Doctrine/Tests/Common/Annotations/DocLexerTest.php
  20. +336 −0 tests/Doctrine/Tests/Common/Annotations/DocParserTest.php
  21. +73 −0 tests/Doctrine/Tests/Common/Annotations/DoctrineReaderTest.php
  22. +15 −0 tests/Doctrine/Tests/Common/Annotations/Fixtures/Annotation/Route.php
  23. +17 −0 tests/Doctrine/Tests/Common/Annotations/Fixtures/Annotation/Secure.php
  24. +13 −0 tests/Doctrine/Tests/Common/Annotations/Fixtures/Annotation/Template.php
  25. +300 −0 tests/Doctrine/Tests/Common/Annotations/Fixtures/Controller.php
  26. +9 −0 tests/Doctrine/Tests/Common/Annotations/Fixtures/MultipleClassesInFile.php
  27. +10 −0 tests/Doctrine/Tests/Common/Annotations/Fixtures/MultipleImportsInUseStatement.php
  28. +10 −0 tests/Doctrine/Tests/Common/Annotations/Fixtures/NonNamespacedClass.php
  29. +13 −0 tests/Doctrine/Tests/Common/Annotations/Fixtures/TestInterface.php
  30. +156 −0 tests/Doctrine/Tests/Common/Annotations/PerformanceTest.php
  31. +60 −0 tests/Doctrine/Tests/Common/Annotations/PhpParserTest.php
  32. +24 −0 tests/Doctrine/Tests/Common/Annotations/ReaderTest.php
  33. +7 −0 tests/Doctrine/Tests/Common/Annotations/TopLevelAnnotation.php
  34. +18 −10 tests/Doctrine/Tests/TestInit.php
@@ -0,0 +1,81 @@
+<?php
+
+/*
+ * This file is part of the Annotations library.
+ *
+ * Portions of this code were received from the Doctrine Project under the LGPL
+ * license. All other code is (C) 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
+ * and subject to the following license:
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL.
+ */
+
+namespace Doctrine\Common\Annotations\Annotation;
+
+/**
+ * Annotations class
+ *
+ * @author Benjamin Eberlei <kontakt@beberlei.de>
+ * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
+ * @author Jonathan Wage <jonwage@gmail.com>
+ * @author Roman Borschel <roman@code-factory.org>
+ */
+class Annotation
+{
+ /**
+ * Value property. Common among all derived classes.
+ *
+ * @var string
+ */
+ public $value;
+
+ /**
+ * Constructor
+ *
+ * @param array $data Key-value for properties to be defined in this class
+ */
+ public final function __construct(array $data)
+ {
+ foreach ($data as $key => $value) {
+ $this->$key = $value;
+ }
+ }
+
+ /**
+ * Error handler for unknown property accessor in Annotation class.
+ *
+ * @param string $name Unknown property name
+ */
+ public function __get($name)
+ {
+ throw new \BadMethodCallException(
+ sprintf("Unknown property '%s' on annotation '%s'.", $name, get_class($this))
+ );
+ }
+
+ /**
+ * Error handler for unknown property mutator in Annotation class.
+ *
+ * @param string $name Unkown property name
+ * @param mixed $value Property value
+ */
+ public function __set($name, $value)
+ {
+ throw new \BadMethodCallException(
+ sprintf("Unknown property '%s' on annotation '%s'.", $name, get_class($this))
+ );
+ }
+}
@@ -0,0 +1,47 @@
+<?php
+
+/*
+ * This file is part of the Annotations library.
+ *
+ * (C) 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL.
+ */
+
+namespace Doctrine\Common\Annotations\Annotation;
+
+/**
+ * Annotation that can be used to signal to the parser to ignore specific
+ * annotations during the parsing process.
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+final class IgnoreAnnotation
+{
+ public $names;
+
+ public function __construct(array $values)
+ {
+ if (is_string($values['value'])) {
+ $values['value'] = array($values['value']);
+ }
+ if (!is_array($values['value'])) {
+ throw new \RuntimeException(sprintf('@IgnoreAnnotation expects either a string name, or an array of strings, but got %s.', json_encode($values['value'])));
+ }
+
+ $this->names = $values['value'];
+ }
+}
@@ -0,0 +1,20 @@
+<?php
+
+namespace Doctrine\Common\Annotations\Annotation;
+
+class ParseAnnotation
+{
+ public $names;
+
+ public function __construct(array $values)
+ {
+ if (is_string($values['value'])) {
+ $values['value'] = array($values['value']);
+ }
+ if (!is_array($values['value'])) {
+ throw new \RuntimeException(sprintf('@parseAnnotation expects either a string name, or an array of strings, but got %s.', json_encode($values['value'])));
+ }
+
+ $this->names = $values['value'];
+ }
+}
@@ -0,0 +1,34 @@
+<?php
+
+namespace Doctrine\Common\Annotations\Cache;
+
+interface CacheInterface
+{
+ /**
+ * Returns the class annotations
+ *
+ * @param ReflectionClass $class
+ * @return array|null
+ */
+ function getClassAnnotationsFromCache(\ReflectionClass $class);
+
+ /**
+ * Returns the property annotations
+ *
+ * @param ReflectionProperty $property
+ * @return array|null
+ */
+ function getPropertyAnnotationsFromCache(\ReflectionProperty $property);
+
+ /**
+ * Returns the method annotations
+ *
+ * @param ReflectionMethod $method
+ * @return array|null
+ */
+ function getMethodAnnotationsFromCache(\ReflectionMethod $method);
+
+ function putClassAnnotationsInCache(\ReflectionClass $class, array $annotations);
+ function putPropertyAnnotationsInCache(\ReflectionProperty $class, array $annotations);
+ function putMethodAnnotationsInCache(\ReflectionMethod $class, array $annotations);
+}
@@ -0,0 +1,128 @@
+<?php
+
+namespace Doctrine\Common\Annotations\Cache;
+
+class FileCache implements CacheInterface
+{
+ private $dir;
+ private $debug;
+ private $classAnnotations = array();
+ private $propertyAnnotations = array();
+ private $methodAnnotations = array();
+
+ public function __construct($dir, $debug = false)
+ {
+ if (!is_dir($dir)) {
+ throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
+ }
+ if (!is_writable($dir)) {
+ throw new \InvalidArgumentException(sprintf('The directory "%s" is not writable.', $dir));
+ }
+
+ $this->dir = rtrim($dir, '\\/');
+ $this->debug = $debug;
+ }
+
+ public function getClassAnnotationsFromCache(\ReflectionClass $class)
+ {
+ $key = $class->getName();
+
+ if (isset($this->classAnnotations[$key])) {
+ return $this->classAnnotations[$key];
+ }
+
+ $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
+ if (!file_exists($path)) {
+ return null;
+ }
+
+ if ($this->debug
+ && (false !== $filename = $class->getFilename())
+ && filemtime($path) < filemtime($filename)) {
+ unlink($path);
+
+ return null;
+ }
+
+ return $this->classAnnotations[$key] = include $path;
+ }
+
+ public function getPropertyAnnotationsFromCache(\ReflectionProperty $property)
+ {
+ $class = $property->getDeclaringClass();
+ $key = $class->getName().'$'.$property->getName();
+
+ if (isset($this->propertyAnnotations[$key])) {
+ return $this->propertyAnnotations[$key];
+ }
+
+ $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
+ if (!file_exists($path)) {
+ return null;
+ }
+
+ if ($this->debug
+ && (false !== $filename = $class->getFilename())
+ && filemtime($path) < filemtime($filename)) {
+ unlink($path);
+
+ return null;
+ }
+
+ return $this->propertyAnnotations[$key] = include $path;
+ }
+
+ public function getMethodAnnotationsFromCache(\ReflectionMethod $method)
+ {
+ $class = $method->getDeclaringClass();
+ $key = $class->getName().'#'.$method->getName();
+
+ if (isset($this->methodAnnotations[$key])) {
+ return $this->methodAnnotations[$key];
+ }
+
+ $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
+ if (!file_exists($path)) {
+ return null;
+ }
+
+ if ($this->debug
+ && (false !== $filename = $class->getFilename())
+ && filemtime($path) < filemtime($filename)) {
+ unlink($path);
+
+ return null;
+ }
+
+ return $this->methodAnnotations[$key] = include $path;
+ }
+
+ public function putClassAnnotationsInCache(\ReflectionClass $class, array $annotations)
+ {
+ $key = $class->getName();
+
+ $this->classAnnotations[$key] = $annotations;
+ $this->saveCacheFile($this->dir.'/'.strtr($key, '\\', '-').'.cache.php', $annotations);
+ }
+
+ public function putPropertyAnnotationsInCache(\ReflectionProperty $property, array $annotations)
+ {
+ $key = $property->getDeclaringClass()->getName().'$'.$property->getName();
+
+ $this->propertyAnnotations[$key] = $annotations;
+ $this->saveCacheFile($this->dir.'/'.strtr($key, '\\', '-').'.cache.php', $annotations);
+ }
+
+ public function putMethodAnnotationsInCache(\ReflectionMethod $method, array $annotations)
+ {
+ $key = $method->getDeclaringClass()->getName().'#'.$method->getName();
+
+ $this->methodAnnotations[$key] = $annotations;
+ $this->saveCacheFile($this->dir.'/'.strtr($key, '\\', '-').'.cache.php', $annotations);
+ }
+
+ private function saveCacheFile($path, array $annotations)
+ {
+ file_put_contents($path, '<?php return unserialize('.var_export(serialize($annotations), true).');');
+ }
+}
@@ -0,0 +1,61 @@
+<?php
+
+namespace Doctrine\Common\Annotations\Cache;
+
+class InMemoryCache implements CacheInterface
+{
+ private $classAnnotations = array();
+ private $propertyAnnotations = array();
+ private $methodAnnotations = array();
+
+ public function getClassAnnotationsFromCache(\ReflectionClass $class)
+ {
+ $key = $class->getName();
+
+ if (isset($this->classAnnotations[$key])) {
+ return $this->classAnnotations[$key];
+ }
+
+ return null;
+ }
+
+ public function getPropertyAnnotationsFromCache(\ReflectionProperty $property)
+ {
+ $key = $property->getDeclaringClass()->getName().'->'.$property->getName();
+
+ if (isset($this->propertyAnnotations[$key])) {
+ return $this->propertyAnnotations[$key];
+ }
+
+ return null;
+ }
+
+ public function getMethodAnnotationsFromCache(\ReflectionMethod $method)
+ {
+ $key = $method->getDeclaringClass()->getName().'::'.$method->getName();
+
+ if (isset($this->methodAnnotations[$key])) {
+ return $this->methodAnnotations[$key];
+ }
+
+ return null;
+ }
+
+ public function putClassAnnotationsInCache(\ReflectionClass $class, array $annotations)
+ {
+ $key = $class->getName();
+ $this->classAnnotations[$key] = $annotations;
+ }
+
+ public function putPropertyAnnotationsInCache(\ReflectionProperty $property, array $annotations)
+ {
+ $key = $property->getDeclaringClass()->getName().'->'.$property->getName();
+ $this->propertyAnnotations[$key] = $annotations;
+ }
+
+ public function putMethodAnnotationsInCache(\ReflectionMethod $method, array $annotations)
+ {
+ $key = $method->getDeclaringClass()->getName().'::'.$method->getName();
+ $this->methodAnnotations[$key] = $annotations;
+ }
+}
Oops, something went wrong.

0 comments on commit 1dce868

Please sign in to comment.