[Bundle] Make getPath() less error prone by allowing both backward and forward slashes

This commit is contained in:
Victor Berchet 2011-01-29 16:28:49 +01:00 committed by Fabien Potencier
parent 2a97f89897
commit 3ed47114d6
29 changed files with 126 additions and 70 deletions

View File

@ -31,8 +31,8 @@ class CompatAssetsBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
}

View File

@ -43,8 +43,8 @@ class DoctrineBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -17,8 +17,8 @@ class AnnotationsBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -17,8 +17,8 @@ class AnnotationsBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -17,8 +17,8 @@ class XmlBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -17,8 +17,8 @@ class YamlBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -32,8 +32,8 @@ class DoctrineMigrationsBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -47,8 +47,8 @@ class DoctrineMongoDBBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -17,8 +17,8 @@ class AnnotationsBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -17,8 +17,8 @@ class XmlBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -17,8 +17,8 @@ class YamlBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -64,7 +64,7 @@ class TemplatePathsCacheWarmer extends CacheWarmer
$prefix = '/Resources/views'; $prefix = '/Resources/views';
$templates = array(); $templates = array();
foreach ($this->kernel->getBundles() as $name => $bundle) { foreach ($this->kernel->getBundles() as $name => $bundle) {
if (!is_dir($dir = $bundle->getPath().$prefix)) { if (!is_dir($dir = $bundle->getNormalizedPath().$prefix)) {
continue; continue;
} }

View File

@ -92,8 +92,8 @@ class FrameworkBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -39,8 +39,8 @@ class FabpotFooBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -31,8 +31,8 @@ class FooBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -31,8 +31,8 @@ class SensioCmsFooBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -31,8 +31,8 @@ class SensioFooBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -42,8 +42,8 @@ class SecurityBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -31,8 +31,8 @@ class SwiftmailerBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -40,8 +40,8 @@ class TwigBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -31,8 +31,8 @@ class WebProfilerBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -40,8 +40,8 @@ class ZendBundle extends Bundle
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() protected function getPath()
{ {
return strtr(__DIR__, '\\', '/'); return __DIR__;
} }
} }

View File

@ -27,23 +27,21 @@ abstract class Bundle extends ContainerAware implements BundleInterface
protected $name; protected $name;
/** /**
* Boots the Bundle. * {@inheritDoc}
*/ */
public function boot() public function boot()
{ {
} }
/** /**
* Shutdowns the Bundle. * {@inheritDoc}
*/ */
public function shutdown() public function shutdown()
{ {
} }
/** /**
* Returns the bundle parent name. * {@inheritDoc}
*
* @return string The Bundle parent name it overrides or null if no parent
*/ */
public function getParent() public function getParent()
{ {
@ -51,9 +49,7 @@ abstract class Bundle extends ContainerAware implements BundleInterface
} }
/** /**
* Returns the bundle name (the class short name). * {@inheritDoc}
*
* @return string The Bundle name
*/ */
final public function getName() final public function getName()
{ {
@ -67,6 +63,14 @@ abstract class Bundle extends ContainerAware implements BundleInterface
return $this->name = false === $pos ? $name : substr($name, $pos + 1); return $this->name = false === $pos ? $name : substr($name, $pos + 1);
} }
/**
* {@inheritDoc}
*/
final public function getNormalizedPath()
{
return strtr($this->getPath(), '\\', '/');
}
/** /**
* Finds and registers Dependency Injection Container extensions. * Finds and registers Dependency Injection Container extensions.
* *
@ -79,7 +83,7 @@ abstract class Bundle extends ContainerAware implements BundleInterface
*/ */
public function registerExtensions(ContainerBuilder $container) public function registerExtensions(ContainerBuilder $container)
{ {
if (!$dir = realpath($this->getPath().'/DependencyInjection')) { if (!$dir = realpath($this->getNormalizedPath().'/DependencyInjection')) {
return; return;
} }
@ -106,7 +110,7 @@ abstract class Bundle extends ContainerAware implements BundleInterface
*/ */
public function registerCommands(Application $application) public function registerCommands(Application $application)
{ {
if (!$dir = realpath($this->getPath().'/Command')) { if (!$dir = realpath($this->getNormalizedPath().'/Command')) {
return; return;
} }
@ -121,4 +125,11 @@ abstract class Bundle extends ContainerAware implements BundleInterface
} }
} }
} }
/**
* Gets the Bundle directory path.
*
* @return string The Bundle absolute path
*/
abstract protected function getPath();
} }

View File

@ -29,9 +29,9 @@ interface BundleInterface
function shutdown(); function shutdown();
/** /**
* Returns the bundle parent class. * Returns the bundle parent name.
* *
* @return string The Bundle parent class name it overrides or null if no parent * @return string The Bundle parent name it overrides or null if no parent
*/ */
function getParent(); function getParent();
@ -56,5 +56,5 @@ interface BundleInterface
* *
* @return string The Bundle absolute path * @return string The Bundle absolute path
*/ */
function getPath(); function getNormalizedPath();
} }

View File

@ -237,7 +237,7 @@ abstract class Kernel implements KernelInterface
} }
foreach ($this->getBundle($bundle, false) as $bundle) { foreach ($this->getBundle($bundle, false) as $bundle) {
if (file_exists($file = $bundle->getPath().'/'.$path)) { if (file_exists($file = $bundle->getNormalizedPath().'/'.$path)) {
if ($first) { if ($first) {
return $file; return $file;
} }

View File

@ -228,7 +228,7 @@ interface BundleInterface
function getParent(); function getParent();
function getName(); function getName();
function getNamespace(); function getNamespace();
function getPath(); function getNormalizedPath();
} }
} }
namespace Symfony\Component\HttpKernel\Bundle namespace Symfony\Component\HttpKernel\Bundle
@ -255,12 +255,17 @@ abstract class Bundle extends ContainerAware implements BundleInterface
if (null !== $this->name) { if (null !== $this->name) {
return $this->name; return $this->name;
} }
$pos = strrpos(get_class($this), '\\'); $name = get_class($this);
return $this->name = substr(get_class($this), $pos ? $pos + 1 : 0); $pos = strrpos($name, '\\');
return $this->name = false === $pos ? $name : substr($name, $pos + 1);
}
final public function getNormalizedPath()
{
return strtr($this->getPath(), '\\', '/');
} }
public function registerExtensions(ContainerBuilder $container) public function registerExtensions(ContainerBuilder $container)
{ {
if (!$dir = realpath($this->getPath().'/DependencyInjection')) { if (!$dir = realpath($this->getNormalizedPath().'/DependencyInjection')) {
return; return;
} }
$finder = new Finder(); $finder = new Finder();
@ -273,7 +278,7 @@ abstract class Bundle extends ContainerAware implements BundleInterface
} }
public function registerCommands(Application $application) public function registerCommands(Application $application)
{ {
if (!$dir = realpath($this->getPath().'/Command')) { if (!$dir = realpath($this->getNormalizedPath().'/Command')) {
return; return;
} }
$finder = new Finder(); $finder = new Finder();
@ -286,6 +291,7 @@ abstract class Bundle extends ContainerAware implements BundleInterface
} }
} }
} }
abstract protected function getPath();
} }
} }
namespace Symfony\Component\HttpKernel\Debug namespace Symfony\Component\HttpKernel\Debug
@ -571,7 +577,7 @@ abstract class Kernel implements KernelInterface
$files[] = $file; $files[] = $file;
} }
foreach ($this->getBundle($bundle, false) as $bundle) { foreach ($this->getBundle($bundle, false) as $bundle) {
if (file_exists($file = $bundle->getPath().'/'.$path)) { if (file_exists($file = $bundle->getNormalizedPath().'/'.$path)) {
if ($first) { if ($first) {
return $file; return $file;
} }
@ -1868,7 +1874,7 @@ class UniversalClassLoader
public function loadClass($class) public function loadClass($class)
{ {
$class = ltrim($class, '\\'); $class = ltrim($class, '\\');
if (false !== ($pos = strripos($class, '\\'))) { if (false !== ($pos = strrpos($class, '\\'))) {
$namespace = substr($class, 0, $pos); $namespace = substr($class, 0, $pos);
foreach ($this->namespaces as $ns => $dirs) { foreach ($this->namespaces as $ns => $dirs) {
foreach ($dirs as $dir) { foreach ($dirs as $dir) {

View File

@ -151,7 +151,7 @@ abstract class Kernel implements KernelInterface
$files[] = $file; $files[] = $file;
} }
foreach ($this->getBundle($bundle, false) as $bundle) { foreach ($this->getBundle($bundle, false) as $bundle) {
if (file_exists($file = $bundle->getPath().'/'.$path)) { if (file_exists($file = $bundle->getNormalizedPath().'/'.$path)) {
if ($first) { if ($first) {
return $file; return $file;
} }
@ -401,6 +401,7 @@ class HttpCache implements HttpKernelInterface
protected $kernel; protected $kernel;
protected $traces; protected $traces;
protected $store; protected $store;
protected $request;
protected $esi; protected $esi;
public function __construct(HttpKernelInterface $kernel, StoreInterface $store, Esi $esi = null, array $options = array()) public function __construct(HttpKernelInterface $kernel, StoreInterface $store, Esi $esi = null, array $options = array())
{ {
@ -430,10 +431,15 @@ class HttpCache implements HttpKernelInterface
} }
return implode('; ', $log); return implode('; ', $log);
} }
public function getRequest()
{
return $this->request;
}
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{ {
if (HttpKernelInterface::MASTER_REQUEST === $type) { if (HttpKernelInterface::MASTER_REQUEST === $type) {
$this->traces = array(); $this->traces = array();
$this->request = $request;
} }
$path = $request->getPathInfo(); $path = $request->getPathInfo();
if ($qs = $request->getQueryString()) { if ($qs = $request->getQueryString()) {
@ -2432,7 +2438,7 @@ class UniversalClassLoader
public function loadClass($class) public function loadClass($class)
{ {
$class = ltrim($class, '\\'); $class = ltrim($class, '\\');
if (false !== ($pos = strripos($class, '\\'))) { if (false !== ($pos = strrpos($class, '\\'))) {
$namespace = substr($class, 0, $pos); $namespace = substr($class, 0, $pos);
foreach ($this->namespaces as $ns => $dirs) { foreach ($this->namespaces as $ns => $dirs) {
foreach ($dirs as $dir) { foreach ($dirs as $dir) {

View File

@ -0,0 +1,33 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\HttpKernel\Bundle;
class BundleTest extends \PHPUnit_Framework_TestCase
{
public function testGetNormalizedPathReturnsANormalizedPath()
{
$bundle = $this
->getMockBuilder('Symfony\Component\HttpKernel\Bundle\Bundle')
->setMethods(array('getPath'))
->disableOriginalConstructor()
->getMockForAbstractClass()
;
$bundle
->expects($this->once())
->method('getPath')
->will($this->returnValue('path\\to\\foo\\bar'))
;
$this->assertEquals('path/to/foo/bar', $bundle->getNormalizedPath());
}
}

View File

@ -255,7 +255,7 @@ class KernelTest extends \PHPUnit_Framework_TestCase
{ {
$bundle = $this $bundle = $this
->getMockBuilder('Symfony\Tests\Component\HttpKernel\BundleForTest') ->getMockBuilder('Symfony\Tests\Component\HttpKernel\BundleForTest')
->setMethods(array('getPath', 'getParent', 'getName')) ->setMethods(array('getNormalizedPath', 'getParent', 'getName'))
->disableOriginalConstructor() ->disableOriginalConstructor()
; ;
@ -273,8 +273,8 @@ class KernelTest extends \PHPUnit_Framework_TestCase
$bundle $bundle
->expects($this->any()) ->expects($this->any())
->method('getPath') ->method('getNormalizedPath')
->will($this->returnValue($dir)) ->will($this->returnValue(strtr($dir, '\\', '/')))
; ;
$bundle $bundle