Initial commit with Symfony 2.1+Vendors

Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Polonkai Gergely
2012-07-01 09:52:20 +02:00
commit 082a0130c2
5381 changed files with 416709 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<?php
namespace Metadata\Cache;
use Metadata\ClassMetadata;
interface CacheInterface
{
/**
* Loads a class metadata instance from the cache
*
* @param \ReflectionClass $class
*
* @return ClassMetadata
*/
function loadClassMetadataFromCache(\ReflectionClass $class);
/**
* Puts a class metadata instance into the cache
*
* @param ClassMetadata $metadata
*
* @return void
*/
function putClassMetadataInCache(ClassMetadata $metadata);
/**
* Evicts the class metadata for the given class from the cache.
*
* @param \ReflectionClass $class
*
* @return void
*/
function evictClassMetadataFromCache(\ReflectionClass $class);
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Metadata\Cache;
use Metadata\ClassMetadata;
class FileCache implements CacheInterface
{
private $dir;
public function __construct($dir)
{
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, '\\/');
}
public function loadClassMetadataFromCache(\ReflectionClass $class)
{
$path = $this->dir.'/'.strtr($class->getName(), '\\', '-').'.cache.php';
if (!file_exists($path)) {
return null;
}
return include $path;
}
public function putClassMetadataInCache(ClassMetadata $metadata)
{
$path = $this->dir.'/'.strtr($metadata->name, '\\', '-').'.cache.php';
file_put_contents($path, '<?php return unserialize('.var_export(serialize($metadata), true).');');
}
public function evictClassMetadataFromCache(\ReflectionClass $class)
{
$path = $this->dir.'/'.strtr($class->getName(), '\\', '-').'.cache.php';
if (file_exists($path)) {
unlink($path);
}
}
}

View File

@@ -0,0 +1,55 @@
<?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 Metadata;
/**
* Represents the metadata for the entire class hierarchy.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ClassHierarchyMetadata
{
public $classMetadata = array();
public function addClassMetadata(ClassMetadata $metadata)
{
$this->classMetadata[$metadata->name] = $metadata;
}
public function getRootClassMetadata()
{
return reset($this->classMetadata);
}
public function getOutsideClassMetadata()
{
return end($this->classMetadata);
}
public function isFresh($timestamp)
{
foreach ($this->classMetadata as $metadata) {
if (!$metadata->isFresh($timestamp)) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,98 @@
<?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 Metadata;
/**
* Base class for class metadata.
*
* This class is intended to be extended to add your own application specific
* properties, and flags.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ClassMetadata implements \Serializable
{
public $name;
public $reflection;
public $methodMetadata = array();
public $propertyMetadata = array();
public $fileResources = array();
public $createdAt;
public function __construct($name)
{
$this->name = $name;
$this->reflection = new \ReflectionClass($name);
$this->createdAt = time();
}
public function addMethodMetadata(MethodMetadata $metadata)
{
$this->methodMetadata[$metadata->name] = $metadata;
}
public function addPropertyMetadata(PropertyMetadata $metadata)
{
$this->propertyMetadata[$metadata->name] = $metadata;
}
public function isFresh($timestamp = null)
{
if (null === $timestamp) {
$timestamp = $this->createdAt;
}
foreach ($this->fileResources as $filepath) {
if (!file_exists($filepath)) {
return false;
}
if ($timestamp < filemtime($filepath)) {
return false;
}
}
return true;
}
public function serialize()
{
return serialize(array(
$this->name,
$this->methodMetadata,
$this->propertyMetadata,
$this->fileResources,
$this->createdAt,
));
}
public function unserialize($str)
{
list(
$this->name,
$this->methodMetadata,
$this->propertyMetadata,
$this->fileResources,
$this->createdAt
) = unserialize($str);
$this->reflection = new \ReflectionClass($this->name);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace Metadata\Driver;
/**
* Base file driver implementation.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
abstract class AbstractFileDriver implements DriverInterface
{
private $locator;
public function __construct(FileLocatorInterface $locator)
{
$this->locator = $locator;
}
public function loadMetadataForClass(\ReflectionClass $class)
{
if (null === $path = $this->locator->findFileForClass($class, $this->getExtension())) {
return null;
}
return $this->loadMetadataFromFile($class, $path);
}
/**
* Parses the content of the file, and converts it to the desired metadata.
*
* @param string $file
* @return ClassMetadata|null
*/
abstract protected function loadMetadataFromFile(\ReflectionClass $class, $file);
/**
* Returns the extension of the file.
*
* @return string
*/
abstract protected function getExtension();
}

View File

@@ -0,0 +1,40 @@
<?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 Metadata\Driver;
final class DriverChain implements DriverInterface
{
private $drivers;
public function __construct(array $drivers)
{
$this->drivers = $drivers;
}
public function loadMetadataForClass(\ReflectionClass $class)
{
foreach ($this->drivers as $driver) {
if (null !== $metadata = $driver->loadMetadataForClass($class)) {
return $metadata;
}
}
return null;
}
}

View File

@@ -0,0 +1,24 @@
<?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 Metadata\Driver;
interface DriverInterface
{
function loadMetadataForClass(\ReflectionClass $class);
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Metadata\Driver;
class FileLocator implements FileLocatorInterface
{
private $dirs;
public function __construct(array $dirs)
{
$this->dirs = $dirs;
}
public function findFileForClass(\ReflectionClass $class, $extension)
{
foreach ($this->dirs as $prefix => $dir) {
if (0 !== strpos($class->getNamespaceName(), $prefix)) {
continue;
}
$path = $dir.'/'.str_replace('\\', '.', substr($class->getName(), strlen($prefix)+1)).'.'.$extension;
if (file_exists($path)) {
return $path;
}
}
return null;
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace Metadata\Driver;
interface FileLocatorInterface
{
function findFileForClass(\ReflectionClass $class, $extension);
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Metadata\Driver;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LazyLoadingDriver implements DriverInterface
{
private $container;
private $realDriverId;
public function __construct(ContainerInterface $container, $realDriverId)
{
$this->container = $container;
$this->realDriverId = $realDriverId;
}
public function loadMetadataForClass(\ReflectionClass $class)
{
return $this->container->get($this->realDriverId)->loadMetadataForClass($class);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Metadata;
class MergeableClassMetadata extends ClassMetadata implements MergeableInterface
{
public function merge(MergeableInterface $object)
{
if (!$object instanceof MergeableClassMetadata) {
throw new \InvalidArgumentException('$object must be an instance of MergeableClassMetadata.');
}
$this->name = $object->name;
$this->reflection = $object->reflection;
$this->methodMetadata = array_merge($this->methodMetadata, $object->methodMetadata);
$this->propertyMetadata = array_merge($this->propertyMetadata, $object->propertyMetadata);
$this->fileResources = array_merge($this->fileResources, $object->fileResources);
if ($object->createdAt < $this->createdAt) {
$this->createdAt = $object->createdAt;
}
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace Metadata;
interface MergeableInterface
{
function merge(MergeableInterface $object);
}

View File

@@ -0,0 +1,142 @@
<?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 Metadata;
use Metadata\Driver\DriverInterface;
use Metadata\Cache\CacheInterface;
final class MetadataFactory implements MetadataFactoryInterface
{
private $driver;
private $cache;
private $loadedMetadata = array();
private $loadedClassMetadata = array();
private $hierarchyMetadataClass;
private $includeInterfaces = false;
private $debug;
public function __construct(DriverInterface $driver, $hierarchyMetadataClass = 'Metadata\ClassHierarchyMetadata', $debug = false)
{
$this->driver = $driver;
$this->hierarchyMetadataClass = $hierarchyMetadataClass;
$this->debug = $debug;
}
public function setIncludeInterfaces($bool)
{
$this->includeInterfaces = (Boolean) $bool;
}
public function setCache(CacheInterface $cache)
{
$this->cache = $cache;
}
public function getMetadataForClass($className)
{
if (isset($this->loadedMetadata[$className])) {
return $this->loadedMetadata[$className];
}
$metadata = null;
foreach ($this->getClassHierarchy($className) as $class) {
if (isset($this->loadedClassMetadata[$name = $class->getName()])) {
$this->addClassMetadata($metadata, $this->loadedClassMetadata[$name]);
continue;
}
// check the cache
if (null !== $this->cache
&& (null !== $classMetadata = $this->cache->loadClassMetadataFromCache($class))) {
if ($this->debug && !$classMetadata->isFresh()) {
$this->cache->evictClassMetadataFromCache($classMetadata->reflection);
} else {
$this->loadedClassMetadata[$name] = $classMetadata;
$this->addClassMetadata($metadata, $classMetadata);
continue;
}
}
// load from source
if (null !== $classMetadata = $this->driver->loadMetadataForClass($class)) {
$this->loadedClassMetadata[$name] = $classMetadata;
$this->addClassMetadata($metadata, $classMetadata);
if (null !== $this->cache) {
$this->cache->putClassMetadataInCache($classMetadata);
}
continue;
}
}
return $this->loadedMetadata[$className] = $metadata;
}
private function addClassMetadata(&$metadata, $toAdd)
{
if ($toAdd instanceof MergeableInterface) {
if (null === $metadata) {
$metadata = clone $toAdd;
} else {
$metadata->merge($toAdd);
}
} else {
if (null === $metadata) {
$metadata = new $this->hierarchyMetadataClass;
}
$metadata->addClassMetadata($toAdd);
}
}
private function getClassHierarchy($class)
{
$classes = array();
$refl = new \ReflectionClass($class);
do {
$classes[] = $refl;
} while (false !== $refl = $refl->getParentClass());
$classes = array_reverse($classes, false);
if (!$this->includeInterfaces) {
return $classes;
}
$addedInterfaces = array();
$newHierarchy = array();
foreach ($classes as $class) {
foreach ($class->getInterfaces() as $interface) {
if (isset($addedInterfaces[$interface->getName()])) {
continue;
}
$addedInterfaces[$interface->getName()] = true;
$newHierarchy[] = $interface;
}
$newHierarchy[] = $class;
}
return $newHierarchy;
}
}

View File

@@ -0,0 +1,24 @@
<?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 Metadata;
interface MetadataFactoryInterface
{
function getMetadataForClass($className);
}

View File

@@ -0,0 +1,61 @@
<?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 Metadata;
/**
* Base class for method metadata.
*
* This class is intended to be extended to add your application specific
* properties, and flags.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class MethodMetadata implements \Serializable
{
public $class;
public $name;
public $reflection;
public function __construct($class, $name)
{
$this->class = $class;
$this->name = $name;
$this->reflection = new \ReflectionMethod($class, $name);
$this->reflection->setAccessible(true);
}
public function invoke($obj, array $args = array())
{
return $this->reflection->invokeArgs($obj, $args);
}
public function serialize()
{
return serialize(array($this->class, $this->name));
}
public function unserialize($str)
{
list($this->class, $this->name) = unserialize($str);
$this->reflection = new \ReflectionMethod($this->class, $this->name);
$this->reflection->setAccessible(true);
}
}

View File

@@ -0,0 +1,69 @@
<?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 Metadata;
/**
* Base class for property metadata.
*
* This class is intended to be extended to add your application specific
* properties, and flags.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class PropertyMetadata implements \Serializable
{
public $class;
public $name;
public $reflection;
public function __construct($class, $name)
{
$this->class = $class;
$this->name = $name;
$this->reflection = new \ReflectionProperty($class, $name);
$this->reflection->setAccessible(true);
}
public function getValue($obj)
{
return $this->reflection->getValue($obj);
}
public function setValue($obj, $value)
{
$this->reflection->setValue($obj, $value);
}
public function serialize()
{
return serialize(array(
$this->class,
$this->name,
));
}
public function unserialize($str)
{
list($this->class, $this->name) = unserialize($str);
$this->reflection = new \ReflectionProperty($this->class, $this->name);
$this->reflection->setAccessible(true);
}
}

View File

@@ -0,0 +1,24 @@
<?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 Metadata;
final class Version
{
const VERSION = '1.1.1';
}