Skip to content

Commit

Permalink
removed support for PHP 5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 25, 2015
1 parent 1cba143 commit 2737a65
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 52 deletions.
6 changes: 2 additions & 4 deletions src/Bridges/DITracy/ContainerPanel.php
Expand Up @@ -57,8 +57,7 @@ public function getPanel()
{ {
$container = $this->container; $container = $this->container;
$registry = $this->getContainerProperty('registry'); $registry = $this->getContainerProperty('registry');
$rc = new \ReflectionClass($container); $file = (new \ReflectionClass($container))->getFileName();
$file = $rc->getFileName();
$tags = []; $tags = [];
$meta = $this->getContainerProperty('meta'); $meta = $this->getContainerProperty('meta');
$services = $meta[Container::SERVICES]; $services = $meta[Container::SERVICES];
Expand All @@ -79,8 +78,7 @@ public function getPanel()


private function getContainerProperty($name) private function getContainerProperty($name)
{ {
$rc = new \ReflectionClass('Nette\DI\Container'); $prop = (new \ReflectionClass('Nette\DI\Container'))->getProperty($name);
$prop = $rc->getProperty($name);
$prop->setAccessible(TRUE); $prop->setAccessible(TRUE);
return $prop->getValue($this->container); return $prop->getValue($this->container);
} }
Expand Down
3 changes: 1 addition & 2 deletions src/DI/Compiler.php
Expand Up @@ -205,8 +205,7 @@ public function generateCode($className, $parentName = NULL)


foreach ($this->extensions as $extension) { foreach ($this->extensions as $extension) {
$extension->beforeCompile(); $extension->beforeCompile();
$rc = new \ReflectionClass($extension); $this->dependencies[] = (new \ReflectionClass($extension))->getFileName();
$this->dependencies[] = $rc->getFileName();
} }


$classes = $this->builder->generateClasses($className, $parentName); $classes = $this->builder->generateClasses($className, $parentName);
Expand Down
8 changes: 4 additions & 4 deletions src/DI/Container.php
Expand Up @@ -117,8 +117,8 @@ public function hasService($name)
{ {
$name = isset($this->meta[self::ALIASES][$name]) ? $this->meta[self::ALIASES][$name] : $name; $name = isset($this->meta[self::ALIASES][$name]) ? $this->meta[self::ALIASES][$name] : $name;
return isset($this->registry[$name]) return isset($this->registry[$name])
|| (method_exists($this, $method = Container::getMethodName($name)) || (method_exists($this, $method = self::getMethodName($name))
&& ($rm = new \ReflectionMethod($this, $method)) && $rm->getName() === $method); && (new \ReflectionMethod($this, $method))->getName() === $method);
} }




Expand Down Expand Up @@ -146,11 +146,11 @@ public function isCreated($name)
public function createService($name, array $args = []) public function createService($name, array $args = [])
{ {
$name = isset($this->meta[self::ALIASES][$name]) ? $this->meta[self::ALIASES][$name] : $name; $name = isset($this->meta[self::ALIASES][$name]) ? $this->meta[self::ALIASES][$name] : $name;
$method = Container::getMethodName($name); $method = self::getMethodName($name);
if (isset($this->creating[$name])) { if (isset($this->creating[$name])) {
throw new Nette\InvalidStateException(sprintf('Circular reference detected for services: %s.', implode(', ', array_keys($this->creating)))); throw new Nette\InvalidStateException(sprintf('Circular reference detected for services: %s.', implode(', ', array_keys($this->creating))));


} elseif (!method_exists($this, $method) || !($rm = new \ReflectionMethod($this, $method)) || $rm->getName() !== $method) { } elseif (!method_exists($this, $method) || (new \ReflectionMethod($this, $method))->getName() !== $method) {
throw new MissingServiceException("Service '$name' not found."); throw new MissingServiceException("Service '$name' not found.");
} }


Expand Down
45 changes: 21 additions & 24 deletions src/DI/ContainerBuilder.php
Expand Up @@ -49,7 +49,7 @@ class ContainerBuilder extends Nette\Object
private $generatedClasses = []; private $generatedClasses = [];


/** @var string */ /** @var string */
/*private in 5.4*/public $currentService; private $currentService;




/** /**
Expand Down Expand Up @@ -329,8 +329,7 @@ public function prepareClassList()
} }


foreach ($this->classes as $class => $foo) { foreach ($this->classes as $class => $foo) {
$rc = new ReflectionClass($class); $this->addDependency((new ReflectionClass($class))->getFileName());
$this->addDependency($rc->getFileName());
} }
} }


Expand Down Expand Up @@ -379,7 +378,7 @@ private function resolveImplement(ServiceDefinition $def, $name)
$def->setFactory($def->getClass(), $def->getFactory() ? $def->getFactory()->arguments : []); $def->setFactory($def->getClass(), $def->getFactory() ? $def->getFactory()->arguments : []);
} }
if (($class = $this->resolveEntityClass($def->getFactory(), [$name => 1])) if (($class = $this->resolveEntityClass($def->getFactory(), [$name => 1]))
&& ($rc = new ReflectionClass($class)) && ($ctor = $rc->getConstructor()) && ($ctor = (new ReflectionClass($class))->getConstructor())
) { ) {
foreach ($ctor->getParameters() as $param) { foreach ($ctor->getParameters() as $param) {
$ctorParams[$param->getName()] = $param; $ctorParams[$param->getName()] = $param;
Expand All @@ -393,7 +392,7 @@ private function resolveImplement(ServiceDefinition $def, $name)
if ($hint !== ($arg->isArray() ? 'array' : PhpReflection::getPropertyType($arg))) { if ($hint !== ($arg->isArray() ? 'array' : PhpReflection::getPropertyType($arg))) {
throw new ServiceCreationException("Type hint for \${$param->getName()} in $interface::$methodName() doesn't match type hint in $class constructor."); throw new ServiceCreationException("Type hint for \${$param->getName()} in $interface::$methodName() doesn't match type hint in $class constructor.");
} }
$def->getFactory()->arguments[$arg->getPosition()] = ContainerBuilder::literal('$' . $arg->getName()); $def->getFactory()->arguments[$arg->getPosition()] = self::literal('$' . $arg->getName());
} }
$paramDef = $hint . ' ' . $param->getName(); $paramDef = $hint . ' ' . $param->getName();
if ($param->isOptional()) { if ($param->isOptional()) {
Expand Down Expand Up @@ -452,7 +451,7 @@ private function resolveEntityClass($entity, $recursive = [])
} }


if (isset($e) || ($refClass && (!$reflection->isPublic() if (isset($e) || ($refClass && (!$reflection->isPublic()
|| (PHP_VERSION_ID >= 50400 && $refClass->isTrait() && !$reflection->isStatic()) || ($refClass->isTrait() && !$reflection->isStatic())
))) { ))) {
$name = array_slice(array_keys($recursive), -1); $name = array_slice(array_keys($recursive), -1);
throw new ServiceCreationException(sprintf("Factory '%s' used in service '%s' is not callable.", Nette\Utils\Callback::toString($entity), $name[0])); throw new ServiceCreationException(sprintf("Factory '%s' used in service '%s' is not callable.", Nette\Utils\Callback::toString($entity), $name[0]));
Expand All @@ -471,7 +470,7 @@ private function resolveEntityClass($entity, $recursive = [])
return $this->definitions[$service]->getImplement() ?: $this->resolveServiceClass($service, $recursive); return $this->definitions[$service]->getImplement() ?: $this->resolveServiceClass($service, $recursive);


} elseif (is_string($entity)) { } elseif (is_string($entity)) {
if (!class_exists($entity) || !($rc = new ReflectionClass($entity)) || !$rc->isInstantiable()) { if (!class_exists($entity) || !(new ReflectionClass($entity))->isInstantiable()) {
$name = array_slice(array_keys($recursive), -1); $name = array_slice(array_keys($recursive), -1);
throw new ServiceCreationException("Class $entity used in service '$name[0]' not found or is not instantiable."); throw new ServiceCreationException("Class $entity used in service '$name[0]' not found or is not instantiable.");
} }
Expand Down Expand Up @@ -596,7 +595,7 @@ private function generateService($name)
$entity = $def->getFactory()->getEntity(); $entity = $def->getFactory()->getEntity();
$serviceRef = $this->getServiceName($entity); $serviceRef = $this->getServiceName($entity);
$factory = $serviceRef && !$def->getFactory()->arguments && !$def->getSetup() && $def->getImplementType() !== 'create' $factory = $serviceRef && !$def->getFactory()->arguments && !$def->getSetup() && $def->getImplementType() !== 'create'
? new Statement(['@' . ContainerBuilder::THIS_CONTAINER, 'getService'], [$serviceRef]) ? new Statement(['@' . self::THIS_CONTAINER, 'getService'], [$serviceRef])
: $def->getFactory(); : $def->getFactory();


$code = '$service = ' . $this->formatStatement($factory) . ";\n"; $code = '$service = ' . $this->formatStatement($factory) . ";\n";
Expand Down Expand Up @@ -694,8 +693,7 @@ public function formatStatement(Statement $statement)
return $this->formatPhp('!?', [$arguments[0]]); return $this->formatPhp('!?', [$arguments[0]]);


} elseif (is_string($entity)) { // class name } elseif (is_string($entity)) { // class name
$rc = new ReflectionClass($entity); if ($constructor = (new ReflectionClass($entity))->getConstructor()) {
if ($constructor = $rc->getConstructor()) {
$this->addDependency($constructor->getFileName()); $this->addDependency($constructor->getFileName());
$arguments = Helpers::autowireArguments($constructor, $arguments, $this); $arguments = Helpers::autowireArguments($constructor, $arguments, $this);
} elseif ($arguments) { } elseif ($arguments) {
Expand All @@ -715,7 +713,7 @@ public function formatStatement(Statement $statement)
} elseif ($entity[0] instanceof Statement) { } elseif ($entity[0] instanceof Statement) {
$inner = $this->formatPhp('?', [$entity[0]]); $inner = $this->formatPhp('?', [$entity[0]]);
if (substr($inner, 0, 4) === 'new ') { if (substr($inner, 0, 4) === 'new ') {
$inner = PHP_VERSION_ID < 50400 ? "current(array($inner))" : "($inner)"; $inner = "($inner)";
} }
return $this->formatPhp("$inner->?(?*)", [$entity[1], $arguments]); return $this->formatPhp("$inner->?(?*)", [$entity[1], $arguments]);


Expand Down Expand Up @@ -751,16 +749,15 @@ public function formatStatement(Statement $statement)
*/ */
public function formatPhp($statement, $args) public function formatPhp($statement, $args)
{ {
$that = $this; array_walk_recursive($args, function(& $val) {
array_walk_recursive($args, function(& $val) use ($that) {
if ($val instanceof Statement) { if ($val instanceof Statement) {
$val = ContainerBuilder::literal($that->formatStatement($val)); $val = self::literal($this->formatStatement($val));


} elseif ($val === $that) { } elseif ($val === $this) {
$val = ContainerBuilder::literal('$this'); $val = self::literal('$this');


} elseif ($val instanceof ServiceDefinition) { } elseif ($val instanceof ServiceDefinition) {
$val = '@' . current(array_keys($that->getDefinitions(), $val, TRUE)); $val = '@' . current(array_keys($this->getDefinitions(), $val, TRUE));
} }


if (!is_string($val)) { if (!is_string($val)) {
Expand All @@ -771,20 +768,20 @@ public function formatPhp($statement, $args)


} elseif (substr($val, 0, 1) === '@') { } elseif (substr($val, 0, 1) === '@') {
$pair = explode('::', $val, 2); $pair = explode('::', $val, 2);
$name = $that->getServiceName($pair[0]); $name = $this->getServiceName($pair[0]);
if (isset($pair[1]) && preg_match('#^[A-Z][A-Z0-9_]*\z#', $pair[1], $m)) { if (isset($pair[1]) && preg_match('#^[A-Z][A-Z0-9_]*\z#', $pair[1], $m)) {
$val = $that->getDefinition($name)->getClass() . '::' . $pair[1]; $val = $this->getDefinition($name)->getClass() . '::' . $pair[1];
} else { } else {
if ($name === ContainerBuilder::THIS_CONTAINER) { if ($name === self::THIS_CONTAINER) {
$val = '$this'; $val = '$this';
} elseif ($name === $that->currentService) { } elseif ($name === $this->currentService) {
$val = '$service'; $val = '$service';
} else { } else {
$val = $that->formatStatement(new Statement(['@' . ContainerBuilder::THIS_CONTAINER, 'getService'], [$name])); $val = $this->formatStatement(new Statement(['@' . self::THIS_CONTAINER, 'getService'], [$name]));
} }
$val .= (isset($pair[1]) ? PhpHelpers::formatArgs('->?', [$pair[1]]) : ''); $val .= (isset($pair[1]) ? PhpHelpers::formatArgs('->?', [$pair[1]]) : '');
} }
$val = ContainerBuilder::literal($val); $val = self::literal($val);
} }
}); });
return PhpHelpers::formatArgs($statement, $args); return PhpHelpers::formatArgs($statement, $args);
Expand Down Expand Up @@ -825,7 +822,7 @@ public function normalizeEntity($entity)
$entity = '@' . current(array_keys($this->definitions, $entity, TRUE)); $entity = '@' . current(array_keys($this->definitions, $entity, TRUE));


} elseif (is_array($entity) && $entity[0] === $this) { // [$this, ...] -> [@container, ...] } elseif (is_array($entity) && $entity[0] === $this) { // [$this, ...] -> [@container, ...]
$entity[0] = '@' . ContainerBuilder::THIS_CONTAINER; $entity[0] = '@' . self::THIS_CONTAINER;
} }
return $entity; // Class, @service, [Class, member], [@service, member], [, globalFunc], Statement return $entity; // Class, @service, [Class, member], [@service, member], [, globalFunc], Statement
} }
Expand Down
3 changes: 1 addition & 2 deletions src/DI/ContainerLoader.php
Expand Up @@ -113,10 +113,9 @@ protected function generate($class, $generator)
$code = call_user_func_array($generator, [& $compiler]); $code = call_user_func_array($generator, [& $compiler]);
$code = $code ?: implode("\n\n\n", $compiler->compile()); $code = $code ?: implode("\n\n\n", $compiler->compile());
$files = $compiler->getDependencies(); $files = $compiler->getDependencies();
$files = $files ? array_combine($files, $files) : []; // workaround for PHP 5.3 array_combine
return [ return [
"<?php\n$code", "<?php\n$code",
serialize(@array_map('filemtime', $files)) // @ - file may not exist serialize(@array_map('filemtime', array_combine($files, $files))) // @ - file may not exist
]; ];
} }


Expand Down
4 changes: 1 addition & 3 deletions src/DI/Extensions/DecoratorExtension.php
Expand Up @@ -62,9 +62,7 @@ private function findByType($type)
{ {
$type = ltrim($type, '\\'); $type = ltrim($type, '\\');
return array_filter($this->getContainerBuilder()->getDefinitions(), function($def) use ($type) { return array_filter($this->getContainerBuilder()->getDefinitions(), function($def) use ($type) {
return $def->getClass() === $type return $def->getClass() === $type || is_subclass_of($def->getClass(), $type);
|| is_subclass_of($def->getClass(), $type)
|| (PHP_VERSION_ID < 50307 && array_key_exists($type, class_implements($def->getClass())));
}); });
} }


Expand Down
2 changes: 1 addition & 1 deletion src/DI/Helpers.php
Expand Up @@ -118,7 +118,7 @@ public static function autowireArguments(\ReflectionFunctionAbstract $method, ar
} }


} elseif ($parameter->isOptional() || $parameter->isDefaultValueAvailable()) { } elseif ($parameter->isOptional() || $parameter->isDefaultValueAvailable()) {
// !optional + defaultAvailable = func($a = NULL, $b) since 5.3.17 & 5.4.7 // !optional + defaultAvailable = func($a = NULL, $b) since 5.4.7
// optional + !defaultAvailable = i.e. Exception::__construct, mysqli::mysqli, ... // optional + !defaultAvailable = i.e. Exception::__construct, mysqli::mysqli, ...
$res[$num] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL; $res[$num] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL;
$optCount++; $optCount++;
Expand Down
13 changes: 5 additions & 8 deletions src/DI/PhpReflection.php
Expand Up @@ -30,8 +30,7 @@ public static function parseAnnotation(\Reflector $ref, $name)
{ {
static $ok; static $ok;
if (!$ok) { if (!$ok) {
$rc = new \ReflectionMethod(__METHOD__); if (!(new \ReflectionMethod(__METHOD__))->getDocComment()) {
if (!$rc->getDocComment()) {
throw new Nette\InvalidStateException('You have to enable phpDoc comments in opcode cache.'); throw new Nette\InvalidStateException('You have to enable phpDoc comments in opcode cache.');
} }
$ok = TRUE; $ok = TRUE;
Expand All @@ -48,11 +47,9 @@ public static function parseAnnotation(\Reflector $ref, $name)
*/ */
public static function getDeclaringClass(\ReflectionProperty $prop) public static function getDeclaringClass(\ReflectionProperty $prop)
{ {
if (PHP_VERSION_ID >= 50400) { foreach ($prop->getDeclaringClass()->getTraits() as $trait) {
foreach ($prop->getDeclaringClass()->getTraits() as $trait) { if ($trait->hasProperty($prop->getName())) {
if ($trait->hasProperty($prop->getName())) { return self::getDeclaringClass($trait->getProperty($prop->getName()));
return self::getDeclaringClass($trait->getProperty($prop->getName()));
}
} }
} }
return $prop->getDeclaringClass(); return $prop->getDeclaringClass();
Expand Down Expand Up @@ -132,7 +129,7 @@ public static function parseUseStatemenets($code, $forClass = NULL)


case T_CLASS: case T_CLASS:
case T_INTERFACE: case T_INTERFACE:
case PHP_VERSION_ID < 50400 ? -1 : T_TRAIT: case T_TRAIT:
if ($name = self::fetch($tokens, T_STRING)) { if ($name = self::fetch($tokens, T_STRING)) {
$class = $namespace . $name; $class = $namespace . $name;
$classLevel = $level + 1; $classLevel = $level + 1;
Expand Down
1 change: 0 additions & 1 deletion tests/DI/CompilerExtension.validateConfig.phpt
Expand Up @@ -2,7 +2,6 @@


/** /**
* Test: Nette\DI\CompilerExtension::validateConfig() * Test: Nette\DI\CompilerExtension::validateConfig()
* @phpversion 5.4
*/ */


use Nette\DI, use Nette\DI,
Expand Down
1 change: 0 additions & 1 deletion tests/DI/ContainerBuilder.factory.error2.phpt
Expand Up @@ -2,7 +2,6 @@


/** /**
* Test: Nette\DI\ContainerBuilder and generated factories errors. * Test: Nette\DI\ContainerBuilder and generated factories errors.
* @phpversion 5.4
*/ */


use Nette\DI, use Nette\DI,
Expand Down
1 change: 0 additions & 1 deletion tests/DI/InjectExtension.getInjectProperties().traits.phpt
Expand Up @@ -2,7 +2,6 @@


/** /**
* Test: Nette\DI\Extensions\InjectExtension::getInjectProperties() with traits * Test: Nette\DI\Extensions\InjectExtension::getInjectProperties() with traits
* @phpversion 5.4
*/ */


namespace A namespace A
Expand Down
1 change: 0 additions & 1 deletion tests/DI/PhpReflection.getDeclaringClass.phpt
Expand Up @@ -2,7 +2,6 @@


/** /**
* Test: Nette\DI\PhpReflection::getDeclaringClass * Test: Nette\DI\PhpReflection::getDeclaringClass
* @phpversion 5.4
*/ */


use Nette\DI\PhpReflection, use Nette\DI\PhpReflection,
Expand Down

0 comments on commit 2737a65

Please sign in to comment.