8000 [Config][DI] Add ComposerResource to track the runtime engine + deps · symfony/symfony@c71d9ee · GitHub
[go: up one dir, main page]

Skip to content

Commit c71d9ee

Browse files
[Config][DI] Add ComposerResource to track the runtime engine + deps
1 parent 46daa35 commit c71d9ee

File tree

5 files changed

+147
-2
lines changed

5 files changed

+147
-2
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Config\Resource;
13+
14+
/**
15+
* ComposerResource tracks the PHP version and composer dependencies.
16+
*
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
class ComposerResource implements SelfCheckingResourceInterface, \Serializable
20+
{
21+
private $versions;
22+
private $vendors;
23+
24+
private static $globalVersions;
25+
private static $globalVendors;
26+
27+
public function __construct()
28+
{
29+
self::refresh();
30+
$this->versions = self::$globalVersions;
31+
$this->vendors = self::$globalVendors;
32+
}
33+
34+
public function getVendors()
35+
{
36+
return array_keys($this->vendors);
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function __toString()
43+
{
44+
return __CLASS__;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function isFresh($timestamp)
51+
{
52+
self::refresh();
53+
54+
if (self::$globalVersions !== $this->versions) {
55+
return false;
56+
}
57+
58+
return self::$globalVendors === $this->vendors;
59+
}
60+
61+
public function serialize()
62+
{
63+
return serialize(array($this->versions, $this->vendors));
64+
}
65+
66+
public function unserialize($serialized)
67+
{
68+
list($this->versions, $this->vendors) = unserialize($serialized);
69+
}
70+
71+
private static function refresh()
72+
{
73+
if (null !== self::$globalVersions) {
74+
return;
75+
}
76+
77+
self::$globalVersions = array();
78+
79+
foreach (get_loaded_extensions() as $ext) {
80+
self::$globalVersions[$ext] = phpversion($ext);
81+
}
82+
83+
self::$globalVendors = array();
84+
85+
foreach (get_declared_classes() as $class) {
86+
if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
87+
$r = new \ReflectionClass($class);
88+
$v = dirname(dirname($r->getFileName())).DIRECTORY_SEPARATOR;
89+
if (file_exists($v.'composer/installed.json')) {
90+
self::$globalVendors[$v] = @filemtime($v);
91+
}
92+
}
93+
}
94+
}
95+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Config\Tests\Resource;
13+
14+
use Composer\Autoload\ClassLoader;
15+
use Symfony\Component\Config\Resource\ComposerResource;
16+
17+
class ComposerResourceTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testGetVendor()
20+
{
21+
$res = new ComposerResource();
22+
23+
$r = new \ReflectionClass(ClassLoader::class);
24+
$found = false;
25+
26+
foreach ($res->getVendors() as $vendor) {
27+
if ($vendor && 0 === strpos($r->getFileName(), $vendor)) {
28+
$found = true;
29+
break;
30+
}
31+
}
32+
33+
$this->assertTrue($found);
34+
}
35+
36+
public function testSerializeUnserialize()
37+
{
38+
$res = new ComposerResource();
39+
$ser = unserialize(serialize($res));
40+
41+
$this->assertTrue($res->isFresh(0));
42+
$this->assertTrue($ser->isFresh(0));
43+
44+
$this->assertEquals($res, $ser);
45+
}
46+
}

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2626
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2727
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
28+
use Symfony\Component\Config\Resource\ComposerResource;
2829
use Symfony\Component\Config\Resource\FileResource;
2930
use Symfony\Component\Config\Resource\ResourceInterface;
3031
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;
@@ -561,6 +562,8 @@ public function compile()
561562
$compiler = $this->getCompiler();
562563

563564
if ($this->trackResources) {
565+
$this->addResource(new ComposerResource());
566+
564567
foreach ($compiler->getPassConfig()->getPasses() as $pass) {
565568
$this->addObjectResource($pass);
566569
}

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ public function testLazyLoadedService()
888888

889889
$classInList = false;
890890
foreach ($resources as $resource) {
891-
if ($resource->getResource() === $reflectionClass->getFileName()) {
891+
if ($resource instanceof FileResource && $resource->getResource() === $reflectionClass->getFileName()) {
892892
$classInList = true;
893893
break;
894894
}

src/Symfony/Component/DependencyInjection/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"require-dev": {
2222
"symfony/yaml": "~3.2",
23-
"symfony/config": "~2.8|~3.0",
23+
"symfony/config": "~3.3",
2424
"symfony/expression-language": "~2.8|~3.0"
2525
},
2626
"suggest": {
@@ -30,6 +30,7 @@
3030
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them"
3131
},
3232
"conflict": {
33+
"symfony/config": "~3.3",
3334
"symfony/yaml": "<3.2"
3435
},
3536
"autoload": {

0 commit comments

Comments
 (0)
0