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

Commit 6d7bedd

Browse filesBrowse files
[Config][DI] Add ComposerResource to track the runtime engine + deps
1 parent caba97a commit 6d7bedd
Copy full SHA for 6d7bedd

File tree

5 files changed

+147
-2
lines changed
Filter options

5 files changed

+147
-2
lines changed
+95Lines 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 $runtimeVersion;
25+
private static $runtimeVendors;
26+
27+
public function __construct()
28+
{
29+
self::refresh();
30+
$this->versions = self::$runtimeVersion;
31+
$this->vendors = self::$runtimeVendors;
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::$runtimeVersion !== $this->versions) {
55+
return false;
56+
}
57+
58+
return self::$runtimeVendors === $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::$runtimeVersion) {
74+
return;
75+
}
76+
77+
self::$runtimeVersion = array();
78+
79+
foreach (get_loaded_extensions() as $ext) {
80+
self::$runtimeVersion[$ext] = phpversion($ext);
81+
}
82+
83+
self::$runtimeVendors = 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::$runtimeVendors[$v] = @filemtime($v.'composer/installed.json');
91+
}
92+
}
93+
}
94+
}
95+
}
+46Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ContainerBuilder.php
+3Lines 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\DirectoryResource;
2930
use Symfony\Component\Config\Resource\FileExistenceResource;
3031
use Symfony\Component\Config\Resource\FileResource;
@@ -563,6 +564,8 @@ public function compile()
563564
$compiler = $this->getCompiler();
564565

565566
if ($this->trackResources) {
567+
$this->addResource(new ComposerResource());
568+
566569
foreach ($compiler->getPassConfig()->getPasses() as $pass) {
567570
$this->addObjectResource($pass);
568571
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ public function testLazyLoadedService()
909909

910910
$classInList = false;
911911
foreach ($resources as $resource) {
912-
if ($resource->getResource() === $reflectionClass->getFileName()) {
912+
if ($resource instanceof FileResource && $resource->getResource() === $reflectionClass->getFileName()) {
913913
$classInList = true;
914914
break;
915915
}

‎src/Symfony/Component/DependencyInjection/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/composer.json
+2-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.