Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DI] Only rebuild autowiring cache when actually needed #18144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Smart caching system for autowiring to only clear container cache whe…
…n it's *actually* needed
  • Loading branch information
weaverryan committed Mar 29, 2016
commit d0b600aee2c9e43545233d797d0cc7d4d6f95a07
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Config\AutowireServiceResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
Expand Down Expand Up @@ -49,6 +50,39 @@ public function process(ContainerBuilder $container)
$this->ambiguousServiceTypes = array();
}

/**
* Creates a resource to help know if this service has changed.
*
* @param \ReflectionClass $reflectionClass
*
* @return AutowireServiceResource
*/
public static function createResourceForClass(\ReflectionClass $reflectionClass)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't those static methods be extracted to a Factory class?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just seen #18144 (comment)

Perfectly makes sense.

{
$metadata = array();

if ($constructor = $reflectionClass->getConstructor()) {
$metadata['__construct'] = self::getResourceMetadataForMethod($constructor);
}

// todo - when #17608 is merged, could refactor to private function to remove duplication
// of determining valid "setter" methods
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
$name = $reflectionMethod->getName();
if (isset($methodsCalled[$name]) || $reflectionMethod->isStatic() || 1 !== $reflectionMethod->getNumberOfParameters() || 0 !== strpos($name, 'set')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isset($methodsCalled[$name]) will never be set, as $methodsCalled is undefined

continue;
}

$metadata[$name] = self::getResourceMetadataForMethod($reflectionMethod);
}

return new AutowireServiceResource(
$reflectionClass->name,
$reflectionClass->getFileName(),
$metadata
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be on one line.

}

/**
* Wires the given definition.
*
Expand All @@ -63,7 +97,7 @@ private function completeDefinition($id, Definition $definition)
return;
}

$this->container->addClassResource($reflectionClass);
$this->container->addResource(static::createResourceForClass($reflectionClass));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please wrap this call in a isTrackingResources call, to avoid creating the resource when unnecessary (which would add a hard dependency to the config component)


if (!$constructor = $reflectionClass->getConstructor()) {
return;
Expand Down Expand Up @@ -278,4 +312,14 @@ private function addServiceToAmbiguousType($id, $type)
}
$this->ambiguousServiceTypes[$type][] = $id;
}

static private function getResourceMetadataForMethod(\ReflectionMethod $method)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private static

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, see 804b924

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$methodArgumentsMetadata = array();
foreach ($method->getParameters() as $parameter) {
$methodArgumentsMetadata[] = (string) $parameter;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, the argument name doesn't matter (it can be changed safely), only the type hint and the order is important.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And casting to a string probably give us a lot of noise like the default value, ... Why not just explicitly use what make sense for us here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabpot a change in the default value also affects autowiring when the default value must be used

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and casting to string makes it much easier to deal with typehints on non-existent classes on PHP 5.x (as PHP 5 does not have getType and getClass breaks in this case)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed to not cast to a string, but I agree with Stof (last committed can be easily reverted)

}

return $methodArgumentsMetadata;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Config;

use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;

class AutowireServiceResource implements SelfCheckingResourceInterface, \Serializable
{
private $class;
private $filePath;
private $autowiringMetadata = array();

public function __construct($class, $path, array $autowiringMetadata)
{
$this->class = $class;
$this->filePath = $path;
$this->autowiringMetadata = $autowiringMetadata;
}

public function isFresh($timestamp)
{
if (!file_exists($this->filePath)) {
return false;
}

// has the file *not* been modified? Definitely fresh
if (@filemtime($this->filePath) <= $timestamp) {
return true;
}

try {
$reflectionClass = new \ReflectionClass($this->class);
} catch (\ReflectionException $e) {
// the class does not exist anymore!

return false;
}

$newResource = AutowirePass::createResourceForClass($reflectionClass);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return $newResource == $this;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the intermediate variable can be removed

}

public function __toString()
{
return 'service.autowire.'.$this->class;
}

public function serialize()
{
return serialize(array(
$this->class,
$this->filePath,
$this->autowiringMetadata,
));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be on one line

}

public function unserialize($serialized)
{
list(
$this->class,
$this->filePath,
$this->autowiringMetadata
) = unserialize($serialized);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be on one line

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,39 @@ public function testOptionalScalarArgsNotPassedIfLast()
$definition->getArguments()
);
}

/**
* @dataProvider getCreateResourceTests
*/
public function testCreateResourceForClass($className, $isEqual)
{
$startingResource = AutowirePass::createResourceForClass(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new \ReflectionClass(__NAMESPACE__.'\ClassForResource')
);
$newResource = AutowirePass::createResourceForClass(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new \ReflectionClass(__NAMESPACE__.'\\'.$className)
);

// hack so the objects don't differ by the class name
$startingReflObject = new \ReflectionObject($startingResource);
$reflProp = $startingReflObject->getProperty('class');
$reflProp->setAccessible(true);
$reflProp->setValue($startingResource, __NAMESPACE__.'\\'.$className);

if ($isEqual) {
$this->assertEquals($startingResource, $newResource);
} else {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->assertNotEquals($startingResource, $newResource);
}
}

public function getCreateResourceTests()
{
return array(
['IdenticalClassResource', true],
['ClassChangedConstructorArgs', false],
);
}
}

class Foo
Expand Down Expand Up @@ -562,3 +595,26 @@ public function __construct(A $a, $foo = 'default_val', Lille $lille)
{
}
}

/*
* Classes used for testing createResourceForClass
*/
class ClassForResource
{
public function __construct($foo, Bar $bar = null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
}

public function setBar(Bar $bar)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
}
}
class IdenticalClassResource extends ClassForResource
{
}
class ClassChangedConstructorArgs extends ClassForResource
{
public function __construct($foo, Bar $bar, $baz)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Config;

use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
use Symfony\Component\DependencyInjection\Config\AutowireServiceResource;

class AutowireServiceResourceTest extends \PHPUnit_Framework_TestCase
{
/**
* @var AutowireServiceResource
*/
private $resource;
private $file;
private $class;
private $time;

protected function setUp()
{
$this->file = realpath(sys_get_temp_dir()).'/tmp.php';
$this->time = time();
touch($this->file, $this->time);

$this->class = __NAMESPACE__.'\Foo';
$this->resource = new AutowireServiceResource(
$this->class,
$this->file,
array()
);
}

public function testToString()
{
$this->assertSame('service.autowire.'.$this->class, (string) $this->resource);
}

public function testSerializeUnserialize()
{
$unserialized = unserialize(serialize($this->resource));

$this->assertEquals($this->resource, $unserialized);
}

public function testIsFresh()
{
$this->assertTrue($this->resource->isFresh($this->time), '->isFresh() returns true if the resource has not changed in same second');
$this->assertTrue($this->resource->isFresh($this->time + 10), '->isFresh() returns true if the resource has not changed');
$this->assertFalse($this->resource->isFresh($this->time - 86400), '->isFresh() returns false if the resource has been updated');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok it this line, the object is returned instead of false.

}

public function testIsFreshForDeletedResources()
{
unlink($this->file);

$this->assertFalse($this->resource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the resource does not exist');
}

public function testIsNotFreshChangedResource()
{
$oldResource = new AutowireServiceResource(
$this->class,
$this->file,
array('will_be_different')
);

// test with a stale file *and* a resource that *will* be different than the actual
$this->assertFalse($oldResource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the constructor arguments have changed');
}

public function testIsFreshSameConstructorArgs()
{
$oldResource = AutowirePass::createResourceForClass(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new \ReflectionClass(__NAMESPACE__.'\Foo')
);

// test with a stale file *but* the resource will not be changed
$this->assertTrue($oldResource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the constructor arguments have changed');
}

public function testNotFreshIfClassNotFound()
{
$resource = new AutowireServiceResource(
'Some\Non\Existent\Class',
$this->file,
array()
);

$this->assertFalse($resource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the class no longer exists');
}

protected function tearDown()
{
if (!file_exists($this->file)) {
return;
}

unlink($this->file);
}

private function getStaleFileTime()
{
return $this->time - 10;
}
}

class Foo
{
public function __construct($foo)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.