Permalink
Browse files

removed support for PHP 5.3

  • Loading branch information...
dg committed May 20, 2015
1 parent 1cba143 commit 2737a656eaba3e2ecfe9eb396e7e4be4802abce0
@@ -57,8 +57,7 @@ public function getPanel()
{
$container = $this->container;
$registry = $this->getContainerProperty('registry');
$rc = new \ReflectionClass($container);
$file = $rc->getFileName();
$file = (new \ReflectionClass($container))->getFileName();
$tags = [];
$meta = $this->getContainerProperty('meta');
$services = $meta[Container::SERVICES];
@@ -79,8 +78,7 @@ public function getPanel()
private function getContainerProperty($name)
{
$rc = new \ReflectionClass('Nette\DI\Container');
$prop = $rc->getProperty($name);
$prop = (new \ReflectionClass('Nette\DI\Container'))->getProperty($name);
$prop->setAccessible(TRUE);
return $prop->getValue($this->container);
}
@@ -205,8 +205,7 @@ public function generateCode($className, $parentName = NULL)
foreach ($this->extensions as $extension) {
$extension->beforeCompile();
$rc = new \ReflectionClass($extension);
$this->dependencies[] = $rc->getFileName();
$this->dependencies[] = (new \ReflectionClass($extension))->getFileName();
}
$classes = $this->builder->generateClasses($className, $parentName);
@@ -117,8 +117,8 @@ public function hasService($name)
{
$name = isset($this->meta[self::ALIASES][$name]) ? $this->meta[self::ALIASES][$name] : $name;
return isset($this->registry[$name])
|| (method_exists($this, $method = Container::getMethodName($name))
&& ($rm = new \ReflectionMethod($this, $method)) && $rm->getName() === $method);
|| (method_exists($this, $method = self::getMethodName($name))
&& (new \ReflectionMethod($this, $method))->getName() === $method);
}
@@ -146,11 +146,11 @@ public function isCreated($name)
public function createService($name, array $args = [])
{
$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])) {
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.");
}
@@ -49,7 +49,7 @@ class ContainerBuilder extends Nette\Object
private $generatedClasses = [];
/** @var string */
/*private in 5.4*/public $currentService;
private $currentService;
/**
@@ -329,8 +329,7 @@ public function prepareClassList()
}
foreach ($this->classes as $class => $foo) {
$rc = new ReflectionClass($class);
$this->addDependency($rc->getFileName());
$this->addDependency((new ReflectionClass($class))->getFileName());
}
}
@@ -379,7 +378,7 @@ private function resolveImplement(ServiceDefinition $def, $name)
$def->setFactory($def->getClass(), $def->getFactory() ? $def->getFactory()->arguments : []);
}
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) {
$ctorParams[$param->getName()] = $param;
@@ -393,7 +392,7 @@ private function resolveImplement(ServiceDefinition $def, $name)
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.");
}
$def->getFactory()->arguments[$arg->getPosition()] = ContainerBuilder::literal('$' . $arg->getName());
$def->getFactory()->arguments[$arg->getPosition()] = self::literal('$' . $arg->getName());
}
$paramDef = $hint . ' ' . $param->getName();
if ($param->isOptional()) {
@@ -452,7 +451,7 @@ private function resolveEntityClass($entity, $recursive = [])
}
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);
throw new ServiceCreationException(sprintf("Factory '%s' used in service '%s' is not callable.", Nette\Utils\Callback::toString($entity), $name[0]));
@@ -471,7 +470,7 @@ private function resolveEntityClass($entity, $recursive = [])
return $this->definitions[$service]->getImplement() ?: $this->resolveServiceClass($service, $recursive);
} 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);
throw new ServiceCreationException("Class $entity used in service '$name[0]' not found or is not instantiable.");
}
@@ -596,7 +595,7 @@ private function generateService($name)
$entity = $def->getFactory()->getEntity();
$serviceRef = $this->getServiceName($entity);
$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();
$code = '$service = ' . $this->formatStatement($factory) . ";\n";
@@ -694,8 +693,7 @@ public function formatStatement(Statement $statement)
return $this->formatPhp('!?', [$arguments[0]]);
} elseif (is_string($entity)) { // class name
$rc = new ReflectionClass($entity);
if ($constructor = $rc->getConstructor()) {
if ($constructor = (new ReflectionClass($entity))->getConstructor()) {
$this->addDependency($constructor->getFileName());
$arguments = Helpers::autowireArguments($constructor, $arguments, $this);
} elseif ($arguments) {
@@ -715,7 +713,7 @@ public function formatStatement(Statement $statement)
} elseif ($entity[0] instanceof Statement) {
$inner = $this->formatPhp('?', [$entity[0]]);
if (substr($inner, 0, 4) === 'new ') {
$inner = PHP_VERSION_ID < 50400 ? "current(array($inner))" : "($inner)";
$inner = "($inner)";
}
return $this->formatPhp("$inner->?(?*)", [$entity[1], $arguments]);
@@ -751,16 +749,15 @@ public function formatStatement(Statement $statement)
*/
public function formatPhp($statement, $args)
{
$that = $this;
array_walk_recursive($args, function(& $val) use ($that) {
array_walk_recursive($args, function(& $val) {
if ($val instanceof Statement) {
$val = ContainerBuilder::literal($that->formatStatement($val));
$val = self::literal($this->formatStatement($val));
} elseif ($val === $that) {
$val = ContainerBuilder::literal('$this');
} elseif ($val === $this) {
$val = self::literal('$this');
} elseif ($val instanceof ServiceDefinition) {
$val = '@' . current(array_keys($that->getDefinitions(), $val, TRUE));
$val = '@' . current(array_keys($this->getDefinitions(), $val, TRUE));
}
if (!is_string($val)) {
@@ -771,20 +768,20 @@ public function formatPhp($statement, $args)
} elseif (substr($val, 0, 1) === '@') {
$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)) {
$val = $that->getDefinition($name)->getClass() . '::' . $pair[1];
$val = $this->getDefinition($name)->getClass() . '::' . $pair[1];
} else {
if ($name === ContainerBuilder::THIS_CONTAINER) {
if ($name === self::THIS_CONTAINER) {
$val = '$this';
} elseif ($name === $that->currentService) {
} elseif ($name === $this->currentService) {
$val = '$service';
} 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 = ContainerBuilder::literal($val);
$val = self::literal($val);
}
});
return PhpHelpers::formatArgs($statement, $args);
@@ -825,7 +822,7 @@ public function normalizeEntity($entity)
$entity = '@' . current(array_keys($this->definitions, $entity, TRUE));
} 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
}
@@ -113,10 +113,9 @@ protected function generate($class, $generator)
$code = call_user_func_array($generator, [& $compiler]);
$code = $code ?: implode("\n\n\n", $compiler->compile());
$files = $compiler->getDependencies();
$files = $files ? array_combine($files, $files) : []; // workaround for PHP 5.3 array_combine
return [
"<?php\n$code",
serialize(@array_map('filemtime', $files)) // @ - file may not exist
serialize(@array_map('filemtime', array_combine($files, $files))) // @ - file may not exist
];
}
@@ -62,9 +62,7 @@ private function findByType($type)
{
$type = ltrim($type, '\\');
return array_filter($this->getContainerBuilder()->getDefinitions(), function($def) use ($type) {
return $def->getClass() === $type
|| is_subclass_of($def->getClass(), $type)
|| (PHP_VERSION_ID < 50307 && array_key_exists($type, class_implements($def->getClass())));
return $def->getClass() === $type || is_subclass_of($def->getClass(), $type);
});
}
@@ -118,7 +118,7 @@ public static function autowireArguments(\ReflectionFunctionAbstract $method, ar
}
} 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, ...
$res[$num] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL;
$optCount++;
@@ -30,8 +30,7 @@ public static function parseAnnotation(\Reflector $ref, $name)
{
static $ok;
if (!$ok) {
$rc = new \ReflectionMethod(__METHOD__);
if (!$rc->getDocComment()) {
if (!(new \ReflectionMethod(__METHOD__))->getDocComment()) {
throw new Nette\InvalidStateException('You have to enable phpDoc comments in opcode cache.');
}
$ok = TRUE;
@@ -48,11 +47,9 @@ public static function parseAnnotation(\Reflector $ref, $name)
*/
public static function getDeclaringClass(\ReflectionProperty $prop)
{
if (PHP_VERSION_ID >= 50400) {
foreach ($prop->getDeclaringClass()->getTraits() as $trait) {
if ($trait->hasProperty($prop->getName())) {
return self::getDeclaringClass($trait->getProperty($prop->getName()));
}
foreach ($prop->getDeclaringClass()->getTraits() as $trait) {
if ($trait->hasProperty($prop->getName())) {
return self::getDeclaringClass($trait->getProperty($prop->getName()));
}
}
return $prop->getDeclaringClass();
@@ -132,7 +129,7 @@ public static function parseUseStatemenets($code, $forClass = NULL)
case T_CLASS:
case T_INTERFACE:
case PHP_VERSION_ID < 50400 ? -1 : T_TRAIT:
case T_TRAIT:
if ($name = self::fetch($tokens, T_STRING)) {
$class = $namespace . $name;
$classLevel = $level + 1;
@@ -2,7 +2,6 @@
/**
* Test: Nette\DI\CompilerExtension::validateConfig()
* @phpversion 5.4
*/
use Nette\DI,
@@ -2,7 +2,6 @@
/**
* Test: Nette\DI\ContainerBuilder and generated factories errors.
* @phpversion 5.4
*/
use Nette\DI,
@@ -2,7 +2,6 @@
/**
* Test: Nette\DI\Extensions\InjectExtension::getInjectProperties() with traits
* @phpversion 5.4
*/
namespace A
@@ -2,7 +2,6 @@
/**
* Test: Nette\DI\PhpReflection::getDeclaringClass
* @phpversion 5.4
*/
use Nette\DI\PhpReflection,

0 comments on commit 2737a65

Please sign in to comment.