瀏覽代碼

added metadata library

Johannes Schmitt 14 年之前
父節點
當前提交
2505ea5f33

+ 2 - 7
Annotation/ExclusionPolicy.php

@@ -22,7 +22,7 @@ use JMS\SerializerBundle\Exception\RuntimeException;
 
 class ExclusionPolicy
 {
-    private $strategy;
+    public $policy;
 
     public function __construct(array $values)
     {
@@ -30,11 +30,6 @@ class ExclusionPolicy
             throw new RuntimeException('"value" must be a string.');
         }
 
-        $this->strategy = strtoupper($values['value']);
-    }
-
-    public function getStrategy()
-    {
-        return $this->strategy;
+        $this->policy = strtoupper($values['value']);
     }
 }

+ 1 - 6
Annotation/SerializedName.php

@@ -22,7 +22,7 @@ use JMS\SerializerBundle\Exception\RuntimeException;
 
 class SerializedName
 {
-    private $name;
+    public $name;
 
     public function __construct(array $values)
     {
@@ -32,9 +32,4 @@ class SerializedName
 
         $this->name = $values['value'];
     }
-
-    public function getName()
-    {
-        return $this->name;
-    }
 }

+ 0 - 4
Annotation/Since.php

@@ -20,8 +20,4 @@ namespace JMS\SerializerBundle\Annotation;
 
 class Since extends Version
 {
-    public function __construct(array $values)
-    {
-        $this->setVersion($values['value']);
-    }
 }

+ 1 - 6
Annotation/Type.php

@@ -22,7 +22,7 @@ use JMS\SerializerBundle\Exception\RuntimeException;
 
 class Type
 {
-    private $name;
+    public $name;
 
     public function __construct(array $values)
     {
@@ -32,9 +32,4 @@ class Type
 
         $this->name = $values['value'];
     }
-
-    public function getName()
-    {
-        return $this->name;
-    }
 }

+ 0 - 4
Annotation/Until.php

@@ -20,8 +20,4 @@ namespace JMS\SerializerBundle\Annotation;
 
 class Until extends Version
 {
-    public function __construct(array $values)
-    {
-        $this->setVersion($values['value']);
-    }
 }

+ 5 - 10
Annotation/Version.php

@@ -20,21 +20,16 @@ namespace JMS\SerializerBundle\Annotation;
 
 use JMS\SerializerBundle\Exception\RuntimeException;
 
-abstract class Version
+class Version
 {
-    private $version;
+    public $version;
 
-    protected function setVersion($version)
+    public function __construct(array $values)
     {
-        if (!is_scalar($version)) {
+        if (!isset($values['value']) || !is_scalar($values['value'])) {
             throw new RuntimeException('$version must be a scalar.');
         }
 
-        $this->version = $version;
-    }
-
-    public function getVersion()
-    {
-        return $this->version;
+        $this->version = $values['value'];
     }
 }

+ 0 - 46
Metadata/ClassHierarchyMetadata.php

@@ -1,46 +0,0 @@
-<?php
-
-/*
- * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-namespace JMS\SerializerBundle\Metadata;
-
-class ClassHierarchyMetadata
-{
-    public $classes = array();
-
-    public function addClass(ClassMetadata $class)
-    {
-        $this->classes[$class->name] = $class;
-    }
-
-    public function getLastModified()
-    {
-        $time = 0;
-
-        foreach ($this->classes as $class) {
-            if (false === $filename = $class->getReflection()->getFilename()) {
-                continue;
-            }
-
-            if ($time < $mtime = filemtime($filename)) {
-                $time = $mtime;
-            }
-        }
-
-        return $time;
-    }
-}

+ 7 - 21
Metadata/ClassMetadata.php

@@ -18,41 +18,27 @@
 
 namespace JMS\SerializerBundle\Metadata;
 
-class ClassMetadata implements \Serializable
+use Metadata\ClassMetadata as BaseClassMetadata;
+
+class ClassMetadata extends BaseClassMetadata
 {
-    public $name;
-    public $reflection;
-    public $properties = array();
     public $exclusionPolicy = 'NONE';
 
-    public function __construct($name)
-    {
-        $this->name = $name;
-        $this->reflection = new \ReflectionClass($name);
-    }
-
-    public function addPropertyMetadata(PropertyMetadata $metadata)
-    {
-        $this->properties[$metadata->name] = $metadata;
-    }
-
     public function serialize()
     {
         return serialize(array(
-            $this->name,
-            $this->properties,
             $this->exclusionPolicy,
+            parent::serialize(),
         ));
     }
 
     public function unserialize($str)
     {
         list(
-            $this->name,
-            $this->properties,
-            $this->exclusionPolicy
+            $this->exclusionPolicy,
+            $parentStr
         ) = unserialize($str);
 
-        $this->reflection = new \ReflectionClass($this->name);
+        parent::unserialize($parentStr);
     }
 }

+ 6 - 5
Metadata/Driver/AnnotationDriver.php

@@ -19,6 +19,7 @@
 namespace JMS\SerializerBundle\Metadata\Driver;
 
 use Annotations\ReaderInterface;
+use Metadata\Driver\DriverInterface;
 use JMS\SerializerBundle\Annotation\Type;
 use JMS\SerializerBundle\Annotation\Exclude;
 use JMS\SerializerBundle\Annotation\Expose;
@@ -43,7 +44,7 @@ class AnnotationDriver implements DriverInterface
         $classMetadata = new ClassMetadata($name = $class->getName());
         foreach ($this->reader->getClassAnnotations($class) as $annot) {
             if ($annot instanceof ExclusionPolicy) {
-                $classMetadata->exclusionPolicy = $annot->getStrategy();
+                $classMetadata->exclusionPolicy = $annot->policy;
             }
         }
 
@@ -55,17 +56,17 @@ class AnnotationDriver implements DriverInterface
             $propertyMetadata = new PropertyMetadata($name, $property->getName());
             foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
                 if ($annot instanceof Since) {
-                    $propertyMetadata->sinceVersion = $annot->getVersion();
+                    $propertyMetadata->sinceVersion = $annot->version;
                 } else if ($annot instanceof Until) {
-                    $propertyMetadata->untilVersion = $annot->getVersion();
+                    $propertyMetadata->untilVersion = $annot->version;
                 } else if ($annot instanceof SerializedName) {
-                    $propertyMetadata->serializedName = $annot->getName();
+                    $propertyMetadata->serializedName = $annot->name;
                 } else if ($annot instanceof Expose) {
                     $propertyMetadata->exposed = true;
                 } else if ($annot instanceof Exclude) {
                     $propertyMetadata->excluded = true;
                 } else if ($annot instanceof Type) {
-                    $propertyMetadata->type = $annot->getName();
+                    $propertyMetadata->type = $annot->name;
                 }
             }
             $classMetadata->addPropertyMetadata($propertyMetadata);

+ 0 - 24
Metadata/Driver/DriverInterface.php

@@ -1,24 +0,0 @@
-<?php
-
-/*
- * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-namespace JMS\SerializerBundle\Metadata\Driver;
-
-interface DriverInterface
-{
-    function loadMetadataForClass(\ReflectionClass $class);
-}

+ 0 - 62
Metadata/MetadataFactory.php

@@ -1,62 +0,0 @@
-<?php
-
-/*
- * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-namespace JMS\SerializerBundle\Metadata;
-
-use JMS\SerializerBundle\Metadata\Driver\DriverInterface;
-
-class MetadataFactory
-{
-    private $driver;
-    private $loadedMetadata = array();
-    private $loadedClassMetadata = array();
-
-    public function __construct(DriverInterface $driver)
-    {
-        $this->driver = $driver;
-    }
-
-    public function getMetadataForClass($className)
-    {
-        if (isset($this->loadedMetadata[$className])) {
-            return $this->loadedMetadata[$className];
-        }
-
-        $metadata = new ClassHierarchyMetadata();
-        foreach ($this->getClassHierarchy($className) as $class) {
-            if (!isset($this->loadedClassMetadata[$name = $class->getName()])) {
-                $this->loadedClassMetadata[$name] = $this->driver->loadMetadataForClass($class);
-            }
-
-            $metadata->addClass($this->loadedClassMetadata[$name]);
-        }
-
-        return $this->loadedMetadata[$className] = $metadata;
-    }
-
-    private function getClassHierarchy($class)
-    {
-        $refl = new \ReflectionClass($class);
-        $classes = array();
-        do {
-            $classes[] = $refl;
-        } while (false !== $refl = $refl->getParentClass());
-
-        return array_reverse($classes, false);
-    }
-}

+ 9 - 24
Metadata/PropertyMetadata.php

@@ -18,57 +18,42 @@
 
 namespace JMS\SerializerBundle\Metadata;
 
-class PropertyMetadata implements \Serializable
+use Metadata\PropertyMetadata as BasePropertyMetadata;
+
+class PropertyMetadata extends BasePropertyMetadata
 {
-    public $class;
-    public $name;
-    public $reflection;
     public $sinceVersion;
     public $untilVersion;
     public $serializedName;
-    public $exposed;
-    public $excluded;
+    public $exposed = false;
+    public $excluded = false;
     public $type;
 
-    public function __construct($class, $name)
-    {
-        $this->class    = $class;
-        $this->name     = $name;
-        $this->exposed  = false;
-        $this->excluded = false;
-
-        $this->reflection = new \ReflectionProperty($class, $name);
-        $this->reflection->setAccessible(true);
-    }
-
     public function serialize()
     {
         return serialize(array(
-            $this->class,
-            $this->name,
             $this->sinceVersion,
             $this->untilVersion,
             $this->serializedName,
             $this->exposed,
             $this->excluded,
             $this->type,
+            parent::serialize(),
         ));
     }
 
     public function unserialize($str)
     {
         list(
-            $this->class,
-            $this->name,
             $this->sinceVersion,
             $this->untilVersion,
             $this->serializedName,
             $this->exposed,
             $this->excluded,
-            $this->type
+            $this->type,
+            $parentStr
         ) = unserialize($str);
 
-        $this->reflection = new \ReflectionProperty($this->class, $this->name);
-        $this->reflection->setAccessible(true);
+        parent::unserialize($parentStr);
     }
 }

+ 1 - 1
Resources/config/services.xml

@@ -7,7 +7,7 @@
     <parameters>
         <parameter key="jms_serializer.metadata.driver.annotation_driver.class">JMS\SerializerBundle\Metadata\Driver\AnnotationDriver</parameter>
         
-        <parameter key="jms_serializer.metadata.metadata_factory.class">JMS\SerializerBundle\Metadata\MetadataFactory</parameter>
+        <parameter key="jms_serializer.metadata.metadata_factory.class">Metadata\MetadataFactory</parameter>
     
         <parameter key="jms_serializer.camel_case_naming_strategy.class">JMS\SerializerBundle\Serializer\Naming\CamelCaseNamingStrategy</parameter>
         <parameter key="jms_serializer.serialized_name_annotation_strategy.class">JMS\SerializerBundle\Serializer\Naming\SerializedNameAnnotationStrategy</parameter>

+ 2 - 2
Serializer/Normalizer/PropertyBasedNormalizer.php

@@ -18,12 +18,12 @@
 
 namespace JMS\SerializerBundle\Serializer\Normalizer;
 
+use Metadata\MetadataFactoryInterface;
 use JMS\SerializerBundle\Annotation\Type;
 use JMS\SerializerBundle\Annotation\ExclusionPolicy;
 use JMS\SerializerBundle\Exception\InvalidArgumentException;
 use JMS\SerializerBundle\Exception\RuntimeException;
 use JMS\SerializerBundle\Exception\UnsupportedException;
-use JMS\SerializerBundle\Metadata\MetadataFactory;
 use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyFactoryInterface;
 use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface;
 use JMS\SerializerBundle\Serializer\InstanceCreatorInterface;
@@ -44,7 +44,7 @@ class PropertyBasedNormalizer extends SerializerAwareNormalizer
     private $exclusionStrategyFactory;
     private $instanceCreator;
 
-    public function __construct(MetadataFactory $metadataFactory, PropertyNamingStrategyInterface $propertyNamingStrategy, InstanceCreatorInterface $instanceCreator, ExclusionStrategyFactoryInterface $exclusionStrategyFactory)
+    public function __construct(MetadataFactoryInterface $metadataFactory, PropertyNamingStrategyInterface $propertyNamingStrategy, InstanceCreatorInterface $instanceCreator, ExclusionStrategyFactoryInterface $exclusionStrategyFactory)
     {
         $this->metadataFactory = $metadataFactory;
         $this->propertyNamingStrategy = $propertyNamingStrategy;

+ 4 - 7
Tests/Serializer/SerializerTest.php

@@ -2,29 +2,26 @@
 
 namespace JMS\SerializerBundle\Tests\Serializer;
 
+use Annotations\Reader;
+use Metadata\MetadataFactory;
 use JMS\SerializerBundle\Serializer\UnserializeInstanceCreator;
-
 use JMS\SerializerBundle\Metadata\Driver\AnnotationDriver;
-
-use JMS\SerializerBundle\Metadata\MetadataFactory;
-
 use JMS\SerializerBundle\Serializer\Exclusion\AllExclusionStrategy;
 use JMS\SerializerBundle\Serializer\Normalizer\PropertyBasedNormalizer;
 use JMS\SerializerBundle\Tests\Fixtures\Comment;
 use JMS\SerializerBundle\Tests\Fixtures\Author;
 use JMS\SerializerBundle\Tests\Fixtures\BlogPost;
 use JMS\SerializerBundle\Serializer\Normalizer\ArrayCollectionNormalizer;
-use Symfony\Component\Serializer\Encoder\JsonEncoder;
-use Symfony\Component\Serializer\Encoder\XmlEncoder;
 use JMS\SerializerBundle\Serializer\Naming\SerializedNameAnnotationStrategy;
 use JMS\SerializerBundle\Serializer\SerializerFactory;
 use JMS\SerializerBundle\Serializer\Exclusion\NoneExclusionStrategy;
 use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyFactory;
 use JMS\SerializerBundle\Serializer\Naming\CamelCaseNamingStrategy;
 use JMS\SerializerBundle\Serializer\Naming\AnnotatedNamingStrategy;
-use Annotations\Reader;
 use JMS\SerializerBundle\Serializer\Normalizer\NativePhpTypeNormalizer;
 use JMS\SerializerBundle\Serializer\Serializer;
+use JMS\SerializerBundle\Serializer\Encoder\JsonEncoder;
+use JMS\SerializerBundle\Serializer\Encoder\XmlEncoder;
 
 class SerializerTest extends \PHPUnit_Framework_TestCase
 {