Vendor update && Started using DoctrineMigrations

This commit is contained in:
Polonkai Gergely
2012-07-23 17:09:03 +02:00
parent 7c36f93436
commit bf46316347
1102 changed files with 103189 additions and 7 deletions

View File

@@ -0,0 +1,192 @@
<?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\DiExtraBundle\Generator;
use CG\Generator\Writer;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
/**
* Generates lightweight code for injecting a single definition.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class DefinitionInjectorGenerator
{
private $nameGenerator;
private $inlinedDefinitions;
public function __construct()
{
$this->nameGenerator = new NameGenerator();
$this->inlinedDefinitions = new \SplObjectStorage();
}
public function generate(Definition $def)
{
$writer = new Writer();
$writer
->writeln('<?php')
->writeln('/**')
->writeln(' * This code has been auto-generated by the JMSDiExtraBundle.')
->writeln(' *')
->writeln(' * Manual changes to it will be lost.')
->writeln(' */')
->writeln('return function($container) {')
->indent()
;
if ($file = $def->getFile()) {
$writer->writeln('require_once '.var_export($file, true).';');
require_once $file;
}
foreach ($this->getInlineDefinitions($def) as $inlineDef) {
$name = $this->nameGenerator->nextName();
$this->inlinedDefinitions[$inlineDef] = $name;
$writer->writeln('$'.$name.' = new \\'.$inlineDef->getClass().$this->dumpArguments($inlineDef->getArguments()).';');
}
$writer->writeln('$instance = new \\'.$def->getClass().$this->dumpArguments($def->getArguments()).';');
foreach ($def->getMethodCalls() as $call) {
list($method, $arguments) = $call;
$writer->writeln('$instance->'.$method.$this->dumpArguments($arguments).';');
}
$ref = new \ReflectionClass($def->getClass());
foreach ($def->getProperties() as $property => $value) {
$refProperty = $this->getReflectionProperty($ref, $property);
if ($refProperty->isPublic()) {
$writer->writeln('$instance->'.$property.' = '.$this->dumpValue($value).';');
} else {
$writer
->writeln(sprintf("\$refProperty = new \ReflectionProperty(%s, %s);", var_export($refProperty->getDeclaringClass()->getName(), true), var_export($property, true)))
->writeln('$refProperty->setAccessible(true);')
->writeln('$refProperty->setValue($instance, '.$this->dumpValue($value).');')
;
}
}
if (method_exists($def, 'getInitMethod') && $def->getInitMethod()) {
$writer->writeln('$instance->'.$def->getInitMethod().'();');
}
$writer
->writeln('return $instance;')
->outdent()
->writeln('};')
;
return $writer->getContent();
}
private function getReflectionProperty($ref, $property)
{
$origClass = $ref->getName();
while (!$ref->hasProperty($property) && false !== $ref = $ref->getParentClass());
if (!$ref->hasProperty($property)) {
throw new \RuntimeException(sprintf('Could not find property "%s" anywhere in class "%s" or one of its parents.', $property, $origName));
}
return $ref->getProperty($property);
}
private function getInlineDefinitions(Definition $def)
{
$defs = new \SplObjectStorage();
$this->getDefinitionsFromArray($def->getArguments(), $defs);
$this->getDefinitionsFromArray($def->getMethodCalls(), $defs);
$this->getDefinitionsFromArray($def->getProperties(), $defs);
return $defs;
}
private function getDefinitionsFromArray(array $a, \SplObjectStorage $defs)
{
foreach ($a as $k => $v) {
if ($v instanceof Definition) {
$defs->attach($v);
} else if (is_array($v)) {
$this->getDefinitionsFromArray($v, $defs);
}
}
}
private function dumpArguments(array $arguments)
{
$code = '(';
$first = true;
foreach ($arguments as $argument) {
if (!$first) {
$code .= ', ';
}
$first = false;
$code .= $this->dumpValue($argument);
}
return $code.')';
}
private function dumpValue($value)
{
if (is_array($value)) {
$code = 'array(';
$first = true;
foreach ($value as $k => $v) {
if (!$first) {
$code .= ', ';
}
$first = false;
$code .= sprintf('%s => %s', var_export($k, true), $this->dumpValue($v));
}
return $code.')';
} else if ($value instanceof Reference) {
if ('service_container' === (string) $value) {
return '$container';
}
return sprintf('$container->get(%s, %d)', var_export((string) $value, true), $value->getInvalidBehavior());
} else if ($value instanceof Parameter) {
return sprintf('$container->getParameter(%s)', var_export((string) $value, true));
} else if (is_scalar($value) || null === $value) {
// we do not support embedded parameters
if (is_string($value) && '%' === $value[0] && '%' !== $value[1]) {
return sprintf('$container->getParameter(%s)', var_export(substr($value, 1, -1), true));
}
return var_export($value, true);
} else if ($value instanceof Definition) {
return sprintf('$%s', $this->inlinedDefinitions[$value]);
}
throw new \RuntimeException(sprintf('Found unsupported value of type %s during definition injector generation: "%s"', gettype($value), json_encode($value)));
}
}

View File

@@ -0,0 +1,119 @@
<?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\DiExtraBundle\Generator;
use Metadata\ClassHierarchyMetadata;
use CG\Generator\PhpParameter;
use CG\Generator\PhpMethod;
use CG\Generator\PhpProperty;
use CG\Generator\PhpClass;
use CG\Proxy\GeneratorInterface;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
class LookupMethodClassGenerator implements GeneratorInterface
{
const PREFIX = '__jmsDiExtra_';
private $metadata;
private $requiredFile;
public function __construct(ClassHierarchyMetadata $metadata)
{
$this->metadata = $metadata;
}
public function setRequiredFile($file)
{
$this->requiredFile = $file;
}
public function generate(\ReflectionClass $class, PhpClass $genClass)
{
if (!empty($this->requiredFile)) {
$genClass->addRequiredFile($this->requiredFile);
}
$genClass->setProperty(PhpProperty::create()
->setName(self::PREFIX.'container')
->setVisibility('private')
);
$genClass->setMethod(PhpMethod::create()
->setName(self::PREFIX.'setContainer')
->addParameter(PhpParameter::create()
->setName('container')
->setType('Symfony\Component\DependencyInjection\ContainerInterface')
)
->setBody('$this->'.self::PREFIX.'container = $container;')
);
$genClass->addInterfaceName('JMS\DiExtraBundle\DependencyInjection\LookupMethodClassInterface');
$genClass->setMethod(PhpMethod::create()
->setName(self::PREFIX.'getOriginalClassName')
->setFinal(true)
->setBody('return '.var_export($class->name, true).';')
);
foreach ($this->getLookupMethods() as $name => $value) {
$genClass->setMethod(PhpMethod::fromReflection($class->getMethod($name))
->setAbstract(false)
->setBody('return '.$this->dumpValue($value).';')
->setDocblock(null)
);
}
}
private function getLookupMethods()
{
$outerClass = $this->metadata->getOutsideClassMetadata()->reflection;
$lookupMethods = array();
foreach ($this->metadata->classMetadata as $classMetadata) {
if (!$classMetadata->lookupMethods) {
continue;
}
foreach ($classMetadata->lookupMethods as $name => $value) {
// check if method has been overridden
if ($outerClass->getMethod($name)->class !== $classMetadata->reflection->name) {
continue;
}
$lookupMethods[$name] = $value;
}
}
return $lookupMethods;
}
private function dumpValue($value)
{
if ($value instanceof Parameter) {
return '$this->'.self::PREFIX.'container->getParameter('.var_export((string) $value, true).')';
} else if ($value instanceof Reference) {
return '$this->'.self::PREFIX.'container->get('.var_export((string) $value, true).', '.var_export($value->getInvalidBehavior(), true).')';
} else if (is_string($value) && '%' === $value[0]) {
return '$this->'.self::PREFIX.'container->getParameter('.var_export(substr($value, 1, -1), true).')';
} else if (is_array($value) || is_scalar($value) || null === $value) {
return var_export($value, true);
}
throw new \RuntimeException(sprintf('Invalid value for lookup method: %s', json_encode($value)));
}
}

View File

@@ -0,0 +1,79 @@
<?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\DiExtraBundle\Generator;
class NameGenerator
{
private $count = 0;
private $firstChars = 'abcdefghijklmnopqrstuvwxyz';
private $firstCharsLength = 26;
private $nonFirstChars = 'abcdefghijklmnopqrstuvwxyz0123456789_';
private $nonFirstCharsLength = 37;
private $reservedNames = array();
public function addReservedName($name)
{
$this->reservedNames[$name] = true;
}
public function setFirstChars($chars)
{
$this->firstChars = $chars;
$this->firstCharsLength = strlen($chars);
}
public function setNonFirstChars($chars)
{
$this->nonFirstChars = $chars;
$this->nonFirstCharsLength = strlen($chars);
}
public function reset()
{
$this->count = 0;
}
public function nextName()
{
while (true) {
$name = '';
$i = $this->count;
if ('' === $name) {
$name .= $this->firstChars[$i%$this->firstCharsLength];
$i = intval($i/$this->firstCharsLength);
}
while ($i > 0) {
$i -= 1;
$name .= $this->nonFirstChars[$i%$this->nonFirstCharsLength];
$i = intval($i/$this->nonFirstCharsLength);
}
$this->count += 1;
// check that the name is not reserved
if (isset($this->reservedNames[$name])) {
continue;
}
return $name;
}
}
}