瀏覽代碼

Inject the MenuFactory service in the Admin class

Thomas Rabaix 13 年之前
父節點
當前提交
1e46436098
共有 2 個文件被更改,包括 27 次插入7 次删除
  1. 23 4
      Admin/Admin.php
  2. 4 3
      DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php

+ 23 - 4
Admin/Admin.php

@@ -331,6 +331,11 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
     protected $menu;
 
+    /**
+     * @var \Knp\Menu\MenuFactory
+     */
+    protected $menuFactory;
+
     protected $loaded = array(
         'view_fields'   => false,
         'view_groups'   => false,
@@ -1099,8 +1104,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
         $this->loaded['side_menu'] = true;
 
-        $menuFactory = new MenuFactory();
-        $menu = $menuFactory->createItem('root');
+        $menu = $this->menuFactory->createItem('root');
 
         $this->configureSideMenu($menu, $action, $childAdmin);
 
@@ -1650,8 +1654,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         }
 
         if (!$menu) {
-            $factory = new MenuFactory();
-            $menu = $factory->createItem('root');
+            $menu = $this->menuFactory->createItem('root');
         }
 
         $child = $menu->addChild(
@@ -2084,4 +2087,20 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     {
         $this->extensions[] = $extension;
     }
+
+    /**
+     * @param \Knp\Menu\MenuFactory $menuFactory
+     */
+    public function setMenuFactory(MenuFactory $menuFactory)
+    {
+        $this->menuFactory = $menuFactory;
+    }
+
+    /**
+     * @return \Knp\Menu\MenuFactory
+     */
+    public function getMenuFactory()
+    {
+        return $this->menuFactory;
+    }
 }

+ 4 - 3
DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php

@@ -144,8 +144,8 @@ class AddDependencyCallsCompilerPass implements CompilerPassInterface
     /**
      * Apply the default values required by the AdminInterface to the Admin service definition
      *
-     * @param ContainerBuilder $container
-     * @param interger $serviceId
+     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
+     * @param $serviceId
      * @param array $attributes
      * @param array $settings
      * @return \Symfony\Component\DependencyInjection\Definition
@@ -170,7 +170,8 @@ class AddDependencyCallsCompilerPass implements CompilerPassInterface
             'configuration_pool' => 'sonata.admin.pool',
             'router'             => 'router',
             'validator'          => 'validator',
-            'security_handler'   => 'sonata.admin.security.handler'
+            'security_handler'   => 'sonata.admin.security.handler',
+            'menu_factory'       => 'knp_menu.factory',
         );
 
         foreach ($defaultAddServices as $attr => $addServiceId) {