Sfoglia il codice sorgente

[Foundation] normalized app name for use in a class name

Fabien Potencier 15 anni fa
parent
commit
1a3790a636

+ 6 - 1
src/Symfony/Foundation/Kernel.php

@@ -217,6 +217,11 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
         return $this->name;
     }
 
+    public function getSafeName()
+    {
+        return preg_replace('/[^a-zA-Z0-9_]+/', '', $this->name);
+    }
+
     public function getEnvironment()
     {
         return $this->environment;
@@ -254,7 +259,7 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
 
     protected function initializeContainer()
     {
-        $class = $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
+        $class = $this->getSafeName().ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
         $location = $this->getCacheDir().'/'.$class;
         $reload = $this->debug ? $this->needsReload($class, $location) : false;
 

+ 54 - 0
tests/Symfony/Tests/Foundation/KernelTest.php

@@ -0,0 +1,54 @@
+<?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\Foundation;
+
+use Symfony\Foundation\Kernel;
+
+class KernelTest extends \PHPUnit_Framework_TestCase
+{
+    public function testGetSafeName()
+    {
+        $kernel = new KernelForTest('dev', true, '-foo-');
+
+        $this->assertEquals('foo', $kernel->getSafeName());
+    }
+}
+
+class KernelForTest extends Kernel
+{
+    public function __construct($environment, $debug, $name)
+    {
+        parent::__construct($environment, $debug);
+
+        $this->name = $name;
+    }
+
+    public function registerRootDir()
+    {
+    }
+
+    public function registerBundles()
+    {
+    }
+
+    public function registerBundleDirs()
+    {
+    }
+
+    public function registerContainerConfiguration()
+    {
+    }
+
+    public function registerRoutes()
+    {
+    }
+}