浏览代码

[Framework] added Kernel::getBundleForClass()

Fabien Potencier 14 年之前
父节点
当前提交
54c36030e8
共有 1 个文件被更改,包括 17 次插入0 次删除
  1. 17 0
      src/Symfony/Framework/Kernel.php

+ 17 - 0
src/Symfony/Framework/Kernel.php

@@ -252,6 +252,23 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
         return false;
     }
 
+    /**
+     * Returns the Bundle name for a given class.
+     *
+     * @param string $class A class name
+     *
+     * @return string The Bundle name or null if the class does not belongs to a bundle
+     */
+    public function getBundleForClass($class)
+    {
+        $namespace = substr($class, 0, strrpos($class, '\\'));
+        foreach (array_keys($this->getBundleDirs()) as $prefix) {
+            if (0 === $pos = strpos($namespace, $prefix)) {
+                return substr($namespace, strlen($prefix) + 1, strpos($class, 'Bundle\\') + 7);
+            }
+        }
+    }
+
     public function getName()
     {
         return $this->name;