From d151d2d4b8cdc817bda3b3686e9daa41c34217fe Mon Sep 17 00:00:00 2001 From: Johannes Schmitt Date: Thu, 28 Apr 2011 23:09:08 +0200 Subject: [PATCH] added Annotations library --- UPDATE.md | 77 +++++++++++++++++++ autoload.php.dist | 3 +- .../DoctrineBundle/Resources/config/orm.xml | 7 +- .../DependencyInjection/Configuration.php | 22 +----- .../FrameworkExtension.php | 14 +--- .../Resources/config/validator.xml | 1 - .../Component/Validator/Constraints/Set.php | 22 ------ .../Mapping/Loader/AnnotationLoader.php | 34 ++------ vendors.sh | 3 + 9 files changed, 93 insertions(+), 90 deletions(-) delete mode 100644 src/Symfony/Component/Validator/Constraints/Set.php diff --git a/UPDATE.md b/UPDATE.md index d2181ef418..180e6d7ec5 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -6,6 +6,83 @@ one. It only discusses changes that need to be done when using the "public" API of the framework. If you "hack" the core, you should probably follow the timeline closely anyway. +beta1 to beta2 +-------------- + +* The annotation parsing process has been changed. All annotations which are used + in a class must now be imported (just like you import PHP namespaces with the + "use" statement): + + Before: + + /** + * @orm:Entity + */ + class MyUser + { + /** + * @orm:Id + * @orm:GeneratedValue(strategy = "AUTO") + * @orm:Column(type="integer") + * @var integer + */ + private $id; + + /** + * @orm:Column(type="string", nullable=false) + * @assert:NotBlank + * @var string + */ + private $name; + } + + After: + + /** + * @import("Doctrine\ORM\Mapping\*") + * @import("Symfony\Component\Validator\Constraints\*") + * @ignorePhpDoc + * @Entity + */ + class MyUser + { + /** + * @Id + * @GeneratedValue(strategy="AUTO") + * @Column(type="integer") + * @var integer + */ + private $id; + + /** + * @Column(type="string", nullable=false) + * @NotBlank + * @var string + */ + private $name; + } + +* The config under "framework.validation.annotations" has been removed and was + replaced with a boolean flag "framework.validation.enable_annotations" which + defaults to false. + +* The Set constraint has been removed as it is not required anymore. + + Before: + + /** + * @assert:Set({@assert:Callback(...), @assert:Callback(...)}) + */ + private $foo; + + After: + + /** + * @Callback(...) + * @Callback(...) + */ + private $foo; + PR12 to beta1 ------------- diff --git a/autoload.php.dist b/autoload.php.dist index f160db88c0..2be6b1fed7 100644 --- a/autoload.php.dist +++ b/autoload.php.dist @@ -8,11 +8,12 @@ $loader = new UniversalClassLoader(); $loader->registerNamespaces(array( 'Symfony\\Tests' => __DIR__.'/tests', 'Symfony' => __DIR__.'/src', - 'Doctrine\\Common' => __DIR__.'/vendor/doctrine-common/lib', + 'Doctrine\\Common' => array(__DIR__.'/vendor/annotations/compat-src', __DIR__.'/vendor/doctrine-common/lib'), 'Doctrine\\DBAL' => __DIR__.'/vendor/doctrine-dbal/lib', 'Doctrine' => __DIR__.'/vendor/doctrine/lib', 'Assetic' => __DIR__.'/vendor/assetic/src', 'Monolog' => __DIR__.'/vendor/monolog/src', + 'Annotations' => __DIR__.'/vendor/annotations/src', )); $loader->registerPrefixes(array( 'Swift_' => __DIR__.'/vendor/swiftmailer/lib/classes', diff --git a/src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml b/src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml index 95679c1fe8..e437373e55 100644 --- a/src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml +++ b/src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml @@ -37,12 +37,7 @@ - - - Doctrine\ORM\Mapping\ - orm - - + diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 647043c514..39740c957a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -244,30 +244,10 @@ class Configuration implements ConfigurationInterface ->children() ->arrayNode('validation') ->canBeUnset() - // For XML, namespace is a child of validation, so it must be moved under annotations - ->beforeNormalization() - ->ifTrue(function($v) { return is_array($v) && !empty($v['annotations']) && !empty($v['namespace']); }) - ->then(function($v){ - $v['annotations'] = array('namespace' => $v['namespace']); - unset($v['namespace']); - return $v; - }) - ->end() ->children() ->booleanNode('enabled')->end() ->scalarNode('cache')->end() - ->arrayNode('annotations') - ->canBeUnset() - ->treatNullLike(array()) - ->treatTrueLike(array()) - ->fixXmlConfig('namespace') - ->children() - ->arrayNode('namespaces') - ->useAttributeAsKey('prefix') - ->prototype('scalar')->end() - ->end() - ->end() - ->end() + ->booleanNode('enable_annotations')->defaultFalse()->end() ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index cfb88a40e8..8162493ee8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -484,19 +484,7 @@ class FrameworkExtension extends Extension ->replaceArgument(0, $this->getValidatorYamlMappingFiles($container)) ; - if (isset($config['annotations'])) { - $namespaces = array('assert' => 'Symfony\\Component\\Validator\\Constraints\\'); - // Register prefixes for constraint namespaces - if (!empty($config['annotations']['namespaces'])) { - $namespaces = array_merge($namespaces, $config['annotations']['namespaces']); - } - - // Register annotation loader - $container - ->getDefinition('validator.mapping.loader.annotation_loader') - ->replaceArgument(0, $namespaces) - ; - + if ($config['enable_annotations']) { $loaderChain = $container->getDefinition('validator.mapping.loader.loader_chain'); $arguments = $loaderChain->getArguments(); array_unshift($arguments[0], new Reference('validator.mapping.loader.annotation_loader')); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml index 8a671bc458..401dc5dd80 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml @@ -48,7 +48,6 @@ - diff --git a/src/Symfony/Component/Validator/Constraints/Set.php b/src/Symfony/Component/Validator/Constraints/Set.php deleted file mode 100644 index 00e72e6a79..0000000000 --- a/src/Symfony/Component/Validator/Constraints/Set.php +++ /dev/null @@ -1,22 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Constraints; - -class Set -{ - public $constraints; - - public function __construct(array $constraints) - { - $this->constraints = $constraints['value']; - } -} diff --git a/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php index 87e15b0764..a4d84b78ab 100644 --- a/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php +++ b/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php @@ -11,9 +11,9 @@ namespace Symfony\Component\Validator\Mapping\Loader; +use Annotations\Reader; use Symfony\Component\Validator\Exception\MappingException; use Symfony\Component\Validator\Mapping\ClassMetadata; -use Doctrine\Common\Annotations\AnnotationReader; use Symfony\Component\Validator\Constraints\Set; use Symfony\Component\Validator\Constraints\GroupSequence; use Symfony\Component\Validator\Constraint; @@ -22,18 +22,12 @@ class AnnotationLoader implements LoaderInterface { protected $reader; - public function __construct(array $paths = null) + public function __construct() { - if (null === $paths) { - $paths = array('assert' => 'Symfony\\Component\\Validator\\Constraints\\'); - } - - $this->reader = new AnnotationReader(); + $this->reader = new Reader(); $this->reader->setAutoloadAnnotations(true); - - foreach ($paths as $prefix => $path) { - $this->reader->setAnnotationNamespaceAlias($path, $prefix); - } + $this->reader->setIgnoreNotImportedAnnotations(false); + $this->reader->setIndexByClass(false); } /** @@ -46,11 +40,7 @@ class AnnotationLoader implements LoaderInterface $loaded = false; foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) { - if ($constraint instanceof Set) { - foreach ($constraint->constraints as $constraint) { - $metadata->addConstraint($constraint); - } - } elseif ($constraint instanceof GroupSequence) { + if ($constraint instanceof GroupSequence) { $metadata->setGroupSequence($constraint->groups); } elseif ($constraint instanceof Constraint) { $metadata->addConstraint($constraint); @@ -62,11 +52,7 @@ class AnnotationLoader implements LoaderInterface foreach ($reflClass->getProperties() as $property) { if ($property->getDeclaringClass()->getName() == $className) { foreach ($this->reader->getPropertyAnnotations($property) as $constraint) { - if ($constraint instanceof Set) { - foreach ($constraint->constraints as $constraint) { - $metadata->addPropertyConstraint($property->getName(), $constraint); - } - } elseif ($constraint instanceof Constraint) { + if ($constraint instanceof Constraint) { $metadata->addPropertyConstraint($property->getName(), $constraint); } @@ -81,11 +67,7 @@ class AnnotationLoader implements LoaderInterface // TODO: clean this up $name = lcfirst(substr($method->getName(), 0, 3)=='get' ? substr($method->getName(), 3) : substr($method->getName(), 2)); - if ($constraint instanceof Set) { - foreach ($constraint->constraints as $constraint) { - $metadata->addGetterConstraint($name, $constraint); - } - } elseif ($constraint instanceof Constraint) { + if ($constraint instanceof Constraint) { $metadata->addGetterConstraint($name, $constraint); } diff --git a/vendors.sh b/vendors.sh index 3970001849..7caab2d554 100755 --- a/vendors.sh +++ b/vendors.sh @@ -52,6 +52,9 @@ install_git doctrine-migrations git://github.com/doctrine/migrations.git # Monolog install_git monolog git://github.com/Seldaek/monolog.git +# Annotations +install_git annotations git://github.com/schmittjoh/annotations.git + # Swiftmailer install_git swiftmailer git://github.com/swiftmailer/swiftmailer.git origin/4.1