Skip to content

Commit

Permalink
ContainerBuilder::getClassList: optimized performance [Closes #108]
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 authored and dg committed May 7, 2016
1 parent ceb472a commit 839f00c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/DI/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class ContainerBuilder
/** @var array for auto-wiring */
private $classList = FALSE;

/** @var bool */
private $classListNeedsRefresh = TRUE;

/** @var string[] of classes excluded from auto-wiring */
private $excludedClasses = [];

Expand All @@ -59,14 +62,21 @@ class ContainerBuilder
*/
public function addDefinition($name, ServiceDefinition $definition = NULL)
{
$this->classListNeedsRefresh = TRUE;
if (!is_string($name) || !$name) { // builder is not ready for falsy names such as '0'
throw new Nette\InvalidArgumentException(sprintf('Service name must be a non-empty string, %s given.', gettype($name)));
}
$name = isset($this->aliases[$name]) ? $this->aliases[$name] : $name;
if (isset($this->definitions[$name])) {
throw new Nette\InvalidStateException("Service '$name' has already been added.");
}
return $this->definitions[$name] = $definition ?: new ServiceDefinition;
if (!$definition) {
$definition = new ServiceDefinition;
}
$definition->setNotifier(function () {
$this->classListNeedsRefresh = TRUE;
});
return $this->definitions[$name] = $definition;
}


Expand All @@ -77,6 +87,7 @@ public function addDefinition($name, ServiceDefinition $definition = NULL)
*/
public function removeDefinition($name)
{
$this->classListNeedsRefresh = TRUE;
$name = isset($this->aliases[$name]) ? $this->aliases[$name] : $name;
unset($this->definitions[$name]);
}
Expand Down Expand Up @@ -258,10 +269,9 @@ public function findByTag($tag)

private function getClassList()
{
static $prev;
if ($this->classList !== FALSE && $prev !== serialize($this->definitions)) {
if ($this->classList !== FALSE && $this->classListNeedsRefresh) {
$this->prepareClassList();
$prev = serialize($this->definitions);
$this->classListNeedsRefresh = FALSE;
}
return $this->classList ?: [];
}
Expand Down
18 changes: 18 additions & 0 deletions src/DI/ServiceDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ class ServiceDefinition
/** @var string|NULL create | get */
private $implementType;

/** @var callable */
private $notifier = 'pi'; // = noop


/**
* @return self
*/
public function setClass($class, array $args = [])
{
call_user_func($this->notifier);
$this->class = $class ? ltrim($class, '\\') : NULL;
if ($args) {
$this->setFactory($class, $args);
Expand All @@ -76,6 +80,7 @@ public function getClass()
*/
public function setFactory($factory, array $args = [])
{
call_user_func($this->notifier);
$this->factory = $factory instanceof Statement ? $factory : new Statement($factory, $args);
return $this;
}
Expand Down Expand Up @@ -210,6 +215,7 @@ public function getTag($tag)
*/
public function setAutowired($state = TRUE)
{
call_user_func($this->notifier);
$this->autowired = (bool) $state;
return $this;
}
Expand Down Expand Up @@ -250,6 +256,7 @@ public function isDynamic()
*/
public function setImplement($interface)
{
call_user_func($this->notifier);
$this->implement = ltrim($interface, '\\');
return $this;
}
Expand Down Expand Up @@ -303,10 +310,21 @@ public function getInject()
}


/**
* @internal
*/
public function setNotifier(callable $notifier)
{
$this->notifier = $notifier;
return $this;
}


public function __clone()
{
$this->factory = unserialize(serialize($this->factory));
$this->setup = unserialize(serialize($this->setup));
$this->notifier = NULL;
}

}

0 comments on commit 839f00c

Please sign in to comment.