Skip to content

[Config] Delegate creation of ConfigCache instances to a factory. #14178

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 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
13 changes: 13 additions & 0 deletions UPGRADE-2.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ Router
but in 2.7 you would get an error if `bar` parameter
doesn't exist or unexpected result otherwise.

* The `getMatcherDumperInstance()` and `getGeneratorDumperInstance()` methods in the
`Symfony\Component\Routing\Router` have been changed from `protected` to `public`.
If you override these methods in a subclass, you will need to change your
methods to `public` as well. Note however that this is a temporary change needed for
PHP 5.3 compatibility only. It will be reverted in Symfony 3.0.

Form
----

Expand Down Expand Up @@ -515,3 +521,10 @@ PropertyAccess

new UnexpectedTypeException($value, $path, $pathIndex);
```

Config
------

* The `__toString()` method of the `\Symfony\Component\Config\ConfigCache` is marked as
Copy link
Member

Choose a reason for hiding this comment

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

deprecations don't belong in the UPGRADE file, instead it should be added in the CHANGELOG file of the Config component and the UPGRADE-3.0.md file should document that this method was removed from the interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure about deprecations? There are a lot of similar comments in UPGRADE-2.7 already.

deprecated in favor of the new `getPath()` method.

6 changes: 6 additions & 0 deletions src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

2.7.0
-----

* added `ConfigCacheInterface`, `ConfigCacheFactoryInterface` and a basic `ConfigCacheFactory`
implementation to delegate creation of ConfigCache instances

2.2.0
-----

Expand Down
17 changes: 14 additions & 3 deletions src/Symfony/Component/Config/ConfigCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ConfigCache
class ConfigCache implements ConfigCacheInterface
{
private $debug;
private $file;

/**
* Constructor.
*
* @param string $file The absolute cache path
* @param bool $debug Whether debugging is enabled or not
*/
Expand All @@ -44,8 +42,21 @@ public function __construct($file, $debug)
* Gets the cache file path.
*
* @return string The cache file path
* @deprecated since 2.7, to be removed in 3.0. Use getPath() instead.
*/
public function __toString()
{
Copy link
Member

Choose a reason for hiding this comment

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

You need to add a deprecation notice (look for trigger_error() in Symfony 2.7 for some examples).

trigger_error('ConfigCache::__toString() is deprecated since version 2.7 and will be removed in 3.0. Use the getPath() method instead.', E_USER_DEPRECATED);

return $this->file;
}

/**
* Gets the cache file path.
*
* @return string The cache file path
*/
public function getPath()
{
return $this->file;
}
Expand Down
48 changes: 48 additions & 0 deletions src/Symfony/Component/Config/ConfigCacheFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Config;

/**
* Basic implementation for ConfigCacheFactoryInterface
* that will simply create an instance of ConfigCache.
*
* @author Matthias Pigulla <mp@webfactory.de>
*/
class ConfigCacheFactory implements ConfigCacheFactoryInterface
{
/**
* @var bool Debug flag passed to the ConfigCache
*/
private $debug;

/**
* @param bool $debug The debug flag to pass to ConfigCache
*/
public function __construct($debug)
{
$this->debug = $debug;
}

/**
* {@inheritdoc}
*/
public function cache($file, $callback)
{
$cache = new ConfigCache($file, $this->debug);

if (!$cache->isFresh()) {
call_user_func($callback, $cache);
}

return $cache;
}
}
32 changes: 32 additions & 0 deletions src/Symfony/Component/Config/ConfigCacheFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\Config;

/**
* Interface for a ConfigCache factory. This factory creates
* an instance of ConfigCacheInterface and initializes the
* cache if necessary.
*
* @author Matthias Pigulla <mp@webfactory.de>
*/
interface ConfigCacheFactoryInterface
{
/**
* Creates a cache instance and (re-)initializes it if necessary.
*
* @param string $file The absolute cache file path
* @param callable $callable The callable to be executed when the cache needs to be filled (i. e. is not fresh). The cache will be passed as the only parameter to this callback
*
* @return ConfigCacheInterface $configCache The cache instance
*/
public function cache($file, $callable);
Copy link
Contributor

Choose a reason for hiding this comment

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

we need to change file argument name here and to make it more generic.

Copy link
Member

Choose a reason for hiding this comment

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

what about $resource?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know... I did not intend to change the way ConfigCache works with this PR, so...?

}
49 changes: 49 additions & 0 deletions src/Symfony/Component/Config/ConfigCacheInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Config;

use Symfony\Component\Config\Resource\ResourceInterface;

/**
* Interface for ConfigCache
*
* @author Matthias Pigulla <mp@webfactory.de>
*/
interface ConfigCacheInterface
{
/**
* Gets the cache file path.
*
* @return string The cache file path
*/
public function getPath();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer load instead of getPath.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ATM this only returns the path (replacing the __toString() method), but I agree that at some later point it might be useful to have methods to load or even include/eval things.


/**
* Checks if the cache is still fresh.
*
* This check should take the metadata passed to the write() method into consideration.
*
* @return bool Whether the cache is still fresh.
*/
public function isFresh();

/**
* Writes the given content into the cache file. Metadata will be stored
* independently and can be used to check cache freshness at a later time.
*
* @param string $content The content to write into the cache
* @param ResourceInterface[]|null $metadata An array of ResourceInterface instances
*
* @throws \RuntimeException When the cache file cannot be written
*/
public function write($content, array $metadata = null);
Copy link
Contributor

Choose a reason for hiding this comment

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

same here for $content

}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/Tests/ConfigCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testToString()
{
$cache = new ConfigCache($this->cacheFile, true);

$this->assertSame($this->cacheFile, (string) $cache);
$this->assertSame($this->cacheFile, $cache->getPath());
}

public function testCacheIsNotFreshIfFileDoesNotExist()
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function getBundle($name, $first = true)
}

/**
* {@inheritDoc}
* {@inheritdoc}
*
* @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle
*/
Expand Down Expand Up @@ -546,7 +546,7 @@ protected function initializeContainer()
$fresh = false;
}

require_once $cache;
require_once $cache->getPath();

$this->container = new $class();
$this->container->set('kernel', $this);
Expand Down Expand Up @@ -695,7 +695,7 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
$dumper->setProxyDumper(new ProxyDumper());
}

$content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass, 'file' => (string) $cache));
$content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass, 'file' => $cache->getPath()));
if (!$this->debug) {
$content = static::stripComments($content);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"symfony/phpunit-bridge": "~2.7|~3.0.0",
"symfony/browser-kit": "~2.3|~3.0.0",
"symfony/class-loader": "~2.1|~3.0.0",
"symfony/config": "~2.0,>=2.0.5|~3.0.0",
"symfony/config": "~2.7",
"symfony/console": "~2.3|~3.0.0",
"symfony/css-selector": "~2.0,>=2.0.5|~3.0.0",
"symfony/dependency-injection": "~2.2|~3.0.0",
Expand All @@ -40,6 +40,9 @@
"symfony/translation": "~2.0,>=2.0.5|~3.0.0",
"symfony/var-dumper": "~2.6|~3.0.0"
},
"conflict": {
"symfony/config": "<2.7"
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this line? The requirement should been enough, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The require-dev does not work if symfony/config is followed as a transitive dependency during dep=... builds. So conflict avoids making symfony/config a hard requirement, but makes sure we get at least 2.7 if we get it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fabpot Do you prefer a PR with tests "all green", or shall I keep changes to a minimum at the cost of having dep= tests pass only once the change has been merged up to the master branch?

I think this is a special case here because changes are made across components that are then tested independently.

Copy link
Member

Choose a reason for hiding this comment

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

ok, I thought config was a hard requirement, makes sense then.

},
"suggest": {
"symfony/browser-kit": "",
"symfony/class-loader": "",
Expand Down
Loading