예제 #1
0
 /**
  * Build a route for the given admin and action, with the provided params
  *
  * This method will first attempt to find a custom Route (like "YourCustomAdminBundle:Section:index")
  * and if it does not work
  *
  * @param \Leapt\AdminBundle\Admin\AdminInterface $admin
  * @param string $action
  * @param array $params
  * @param bool $defaultRoute
  * @return \Symfony\Component\Routing\Route
  */
 public function getRoute(AdminInterface $admin, $action, $params = array(), $defaultRoute = false)
 {
     $defaults = array();
     $pattern = '/' . $admin->getAlias();
     if (!$defaultRoute) {
         $pattern .= '/' . $action;
     }
     foreach ($params as $paramKey => $paramValue) {
         if (is_int($paramKey)) {
             $paramName = $paramValue;
         } else {
             $paramName = $paramKey;
             $defaults[$paramName] = $paramValue;
         }
         $pattern .= '/{' . $paramName . '}';
     }
     preg_match('/(?:[A-Z](?:[A-Za-z0-9])+\\\\)*(?:)[A-Z](?:[A-Za-z0-9])+Bundle/', get_class($admin), $matches);
     $bundle = implode('', explode('\\', $matches[0]));
     $section = StringUtil::camelize($admin->getAlias());
     $controller = $bundle . ':' . $section . ':' . $action;
     try {
         $controller = $this->parser->parse($controller);
     } catch (\InvalidArgumentException $e) {
         $controller = $this->parser->parse('LeaptAdminBundle:Content:' . $action);
     }
     $defaults = array_merge(array('_controller' => $controller, 'alias' => $admin->getAlias()), $defaults);
     return new Route($pattern, $defaults);
 }
예제 #2
0
 /**
  * parse controller name like Acme:DemoBundle:index
  * 
  * @param string $controllerName
  * @return array
  * @throws InvalidArgumentException
  */
 public function parse($controllerName)
 {
     $parser = new ControllerNameParser($this->container->get('kernel'));
     $controller = $parser->parse($controllerName);
     list($class, $method) = explode('::', $controller, 2);
     if (!class_exists($class)) {
         throw new InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
     }
     $controller = new $class();
     $controller->setContainer($this->container);
     return array($controller, $method);
 }
예제 #3
0
 /**
  * Returns the ReflectionMethod for the given controller string.
  *
  * @param string $controller
  * @return \ReflectionMethod|null
  */
 public function getReflectionMethod($controller)
 {
     if (false === strpos($controller, '::') && 2 === substr_count($controller, ':')) {
         $controller = $this->controllerNameParser->parse($controller);
     }
     if (preg_match('#(.+)::([\\w]+)#', $controller, $matches)) {
         $class = $matches[1];
         $method = $matches[2];
     } else {
         if (preg_match('#(.+):([\\w]+)#', $controller, $matches)) {
             $controller = $matches[1];
             $method = $matches[2];
         }
         if ($this->container->has($controller)) {
             $this->container->enterScope('request');
             $this->container->set('request', new Request(), 'request');
             $class = ClassUtils::getRealClass(get_class($this->container->get($controller)));
             $this->container->leaveScope('request');
             if (!isset($method) && method_exists($class, '__invoke')) {
                 $method = '__invoke';
             }
         }
     }
     if (isset($class) && isset($method)) {
         try {
             return new \ReflectionMethod($class, $method);
         } catch (\ReflectionException $e) {
         }
     }
     return null;
 }
예제 #4
0
 public function build($controller)
 {
     if ('\\' === substr($controller, 0, 1)) {
         $controller = substr($controller, 1);
     }
     return parent::build($controller);
 }
 /**
  * Parses request controller and returns vendor, bundle, controller, action
  *
  * @param string $controller
  * @return array
  */
 protected function parseRequestController($controller)
 {
     if (!is_string($controller)) {
         return array();
     }
     if (array_key_exists($controller, $this->parserCache)) {
         return $this->parserCache[$controller];
     }
     $result = array();
     if ($this->isControllerActionFullName($controller)) {
         // Format: "Foo\BarBundle\Controller\BazController::indexAction"
         $controllerActionKey = $this->parser->build($controller);
         $controllerFullName = $controller;
     } elseif ($this->isControllerActionShortName($controller)) {
         // Format: "FooBarBundle:BazController:index"
         $controllerActionKey = $controller;
         $controllerFullName = $this->parser->parse($controller);
     } else {
         // Format with service id: "foo_bar_bundle.baz_controller:indexAction"
         // Cannot be used to parse vendor, bundle, controller, action
         return $result;
     }
     $controllerNameParts = explode('::', $controllerFullName);
     $vendorName = current(explode('\\', $controllerNameParts[0]));
     list($bundleName, $controllerName, $actionName) = explode(':', $controllerActionKey);
     return $this->parserCache[$controller] = array('vendor' => $vendorName, 'bundle' => $bundleName, 'controller' => $controllerName, 'action' => $actionName);
 }
 /**
  * Returns the fully qualified name of the controller.
  *
  * @param string $controller Name of the controller
  *
  * @return string Fully qualified name of the controller
  */
 protected function getControllerName($controller)
 {
     try {
         return $this->nameParser->parse($controller);
     } catch (\Exception $e) {
         return null;
     }
 }
예제 #7
0
    public function testParse()
    {
        $kernel = new Kernel();
        $kernel->boot();
        $logger = new Logger();
        $parser = new ControllerNameParser($kernel, $logger);

        $this->assertEquals('TestBundle\FooBundle\Controller\DefaultController::indexAction', $parser->parse('FooBundle:Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
        $this->assertEquals('TestBundle\FooBundle\Controller\Sub\DefaultController::indexAction', $parser->parse('FooBundle:Sub\Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
        $this->assertEquals('TestBundle\Fabpot\FooBundle\Controller\DefaultController::indexAction', $parser->parse('SensioFooBundle:Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');
        $this->assertEquals('TestBundle\Sensio\Cms\FooBundle\Controller\DefaultController::indexAction', $parser->parse('SensioCmsFooBundle:Default:index'), '->parse() converts a short a:b:c notation string to a class::method string');

        try {
            $parser->parse('foo:');
            $this->fail('->parse() throws an \InvalidArgumentException if the controller is not an a:b:c string');
        } catch (\Exception $e) {
            $this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException if the controller is not an a:b:c string');
        }

        try {
            $parser->parse('BarBundle:Default:index');
            $this->fail('->parse() throws a \InvalidArgumentException if the class is found but does not exist');
        } catch (\Exception $e) {
            $this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws a \LogicException if the class is found but does not exist');
        }
    }
예제 #8
0
 /**
  * Get current action name
  *
  * @return string|null
  */
 public function getActionName()
 {
     try {
         $controller = $this->controllerNameParser->parse($this->request->get('_controller'));
     } catch (\InvalidArgumentException $e) {
         $controller = $this->request->get('_controller');
     }
     $pattern = "#::([a-zA-Z]*)Action#";
     $matches = array();
     if (preg_match($pattern, $controller, $matches)) {
         return $matches[1];
     }
 }
 /**
  * {@inheritdoc}
  */
 public function parse($controller)
 {
     $parsed = parent::parse($controller);
     $parts = explode(':', $controller);
     if (3 === count($parts) && 'App' === $parts[0]) {
         $parts = explode('::', $parsed);
         if (method_exists($parts[0], $parts[1])) {
             return $parsed;
         }
         if (method_exists($parts[0], $action = substr($parts[1], 0, -6))) {
             return $parts[0] . '::' . $action;
         }
     }
     return $parsed;
 }