[HttpKernel] deprecated bundle inheritance

This commit is contained in:
Fabien Potencier 2017-09-11 10:56:43 -07:00
parent ee9f4c3dad
commit 89893c1204
8 changed files with 53 additions and 4 deletions

View File

@ -173,6 +173,8 @@ FrameworkBundle
HttpKernel
----------
* Bundle inheritance has been deprecated.
* Relying on convention-based commands discovery has been deprecated and
won't be supported in 4.0. Use PSR-4 based service discovery instead.

View File

@ -493,6 +493,8 @@ HttpFoundation
HttpKernel
----------
* Bundle inheritance has been removed.
* Relying on convention-based commands discovery is not supported anymore.
Use PSR-4 based service discovery instead.

View File

@ -58,7 +58,7 @@ class ControllerNameParser
try {
// this throws an exception if there is no such bundle
$allBundles = $this->kernel->getBundle($bundle, false);
$allBundles = $this->kernel->getBundle($bundle, false, true);
} catch (\InvalidArgumentException $e) {
$message = sprintf(
'The "%s" (from the _controller value "%s") does not exist or is not enabled in your kernel!',
@ -141,7 +141,7 @@ class ControllerNameParser
}
$lev = levenshtein($nonExistentBundleName, $bundleName);
if ($lev <= strlen($nonExistentBundleName) / 3 && ($alternative === null || $lev < $shortest)) {
if ($lev <= strlen($nonExistentBundleName) / 3 && (null === $alternative || $lev < $shortest)) {
$alternative = $bundleName;
$shortest = $lev;
}

View File

@ -56,6 +56,8 @@ interface BundleInterface extends ContainerAwareInterface
* bundle.
*
* @return string The Bundle name it overrides or null if no parent
*
* @deprecated This method is deprecated as of 3.4 and will be removed in 4.0.
*/
public function getParent();

View File

@ -4,6 +4,7 @@ CHANGELOG
3.4.0
-----
* deprecated bundle inheritance
* added `RebootableInterface` and implemented it in `Kernel`
* deprecated commands auto registration
* added `AddCacheClearerPass`

View File

@ -204,8 +204,17 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
/**
* {@inheritdoc}
*/
public function getBundle($name, $first = true)
public function getBundle($name, $first = true/*, $noDeprecation = false */)
{
$noDeprecation = false;
if (func_num_args() >= 3) {
$noDeprecation = func_get_arg(2);
}
if (!$first && !$noDeprecation) {
@trigger_error(sprintf('Passing "false" as the second argument to %s() is deprecated as of 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
}
if (!isset($this->bundleMap[$name])) {
throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled. Maybe you forgot to add it in the registerBundles() method of your %s.php file?', $name, get_class($this)));
}
@ -241,7 +250,7 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
$isResource = 0 === strpos($path, 'Resources') && null !== $dir;
$overridePath = substr($path, 9);
$resourceBundle = null;
$bundles = $this->getBundle($bundleName, false);
$bundles = $this->getBundle($bundleName, false, true);
$files = array();
foreach ($bundles as $bundle) {
@ -468,6 +477,8 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
$this->bundles[$name] = $bundle;
if ($parentName = $bundle->getParent()) {
@trigger_error('Bundle inheritance is deprecated as of 3.4 and will be removed in 4.0.', E_USER_DEPRECATED);
if (isset($directChildren[$parentName])) {
throw new \LogicException(sprintf('Bundle "%s" is directly extended by two bundles "%s" and "%s".', $parentName, $name, $directChildren[$parentName]));
}

View File

@ -60,6 +60,9 @@ interface KernelInterface extends HttpKernelInterface, \Serializable
/**
* Returns a bundle and optionally its descendants by its name.
*
* The second argument is deprecated as of 3.4 and will be removed in 4.0. This method
* will always return an instance of BundleInterface in 4.0.
*
* @param string $name Bundle name
* @param bool $first Whether to return the first bundle only or together with its descendants
*

View File

@ -410,6 +410,9 @@ EOF;
$this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1Bundle/foo.txt'));
}
/**
* @group legacy
*/
public function testLocateResourceReturnsTheFirstThatMatchesWithParent()
{
$parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
@ -426,6 +429,9 @@ EOF;
$this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/bar.txt', $kernel->locateResource('@ParentAABundle/bar.txt'));
}
/**
* @group legacy
*/
public function testLocateResourceReturnsAllMatches()
{
$parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
@ -444,6 +450,9 @@ EOF;
$kernel->locateResource('@Bundle1Bundle/foo.txt', null, false));
}
/**
* @group legacy
*/
public function testLocateResourceReturnsAllMatchesBis()
{
$kernel = $this->getKernel(array('getBundle'));
@ -492,6 +501,9 @@ EOF;
);
}
/**
* @group legacy
*/
public function testLocateResourceReturnsTheDirOneForResourcesAndBundleOnes()
{
$kernel = $this->getKernel(array('getBundle'));
@ -508,6 +520,9 @@ EOF;
);
}
/**
* @group legacy
*/
public function testLocateResourceOverrideBundleAndResourcesFolders()
{
$parent = $this->getBundle(__DIR__.'/Fixtures/BaseBundle', null, 'BaseBundle', 'BaseBundle');
@ -581,6 +596,9 @@ EOF;
);
}
/**
* @group legacy
*/
public function testInitializeBundles()
{
$parent = $this->getBundle(null, null, 'ParentABundle');
@ -599,6 +617,9 @@ EOF;
$this->assertEquals(array($child, $parent), $map['ParentABundle']);
}
/**
* @group legacy
*/
public function testInitializeBundlesSupportInheritanceCascade()
{
$grandparent = $this->getBundle(null, null, 'GrandParentBBundle');
@ -621,6 +642,7 @@ EOF;
}
/**
* @group legacy
* @expectedException \LogicException
* @expectedExceptionMessage Bundle "ChildCBundle" extends bundle "FooBar", which is not registered.
*/
@ -631,6 +653,9 @@ EOF;
$kernel->boot();
}
/**
* @group legacy
*/
public function testInitializeBundlesSupportsArbitraryBundleRegistrationOrder()
{
$grandparent = $this->getBundle(null, null, 'GrandParentCBundle');
@ -653,6 +678,7 @@ EOF;
}
/**
* @group legacy
* @expectedException \LogicException
* @expectedExceptionMessage Bundle "ParentCBundle" is directly extended by two bundles "ChildC2Bundle" and "ChildC1Bundle".
*/
@ -667,6 +693,7 @@ EOF;
}
/**
* @group legacy
* @expectedException \LogicException
* @expectedExceptionMessage Trying to register two bundles with the same name "DuplicateName"
*/
@ -680,6 +707,7 @@ EOF;
}
/**
* @group legacy
* @expectedException \LogicException
* @expectedExceptionMessage Bundle "CircularRefBundle" can not extend itself.
*/