Ejemplo n.º 1
0
 public function testGetArguments()
 {
     $app = new Application();
     $resolver = new ControllerResolver($app);
     $controller = function (Application $app) {
     };
     $args = $resolver->getArguments(Request::create('/'), $controller);
     $this->assertSame($app, $args[0]);
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 protected function createController($controller)
 {
     $callable = parent::createController($controller);
     if ($callable[0] instanceof ContainerAwareInterface) {
         $callable[0]->setContainer($this->app);
     }
     return $callable;
 }
Ejemplo n.º 3
0
 public function getController(SymfonyRequest $request)
 {
     $callable = parent::getController($request);
     if (is_array($callable)) {
         if (isset($callable[0]) && $callable[0] instanceof ControllerInterface) {
             $callable[0]->setRequest($request);
             $callable[0]->setApplication($this->app);
         }
     }
     return $callable;
 }
 /** {@inheritdoc} */
 protected function doGetArguments(Request $request, $controller, array $parameters)
 {
     $psr7Factory = new DiactorosFactory();
     $psrRequestClasses = [RequestInterface::class, ServerRequestInterface::class];
     foreach ($parameters as $param) {
         if ($param->getClass() && in_array($param->getClass()->getName(), $psrRequestClasses)) {
             $request->attributes->set($param->getName(), $psr7Factory->createRequest($request));
             break;
         }
     }
     return parent::doGetArguments($request, $controller, $parameters);
 }
Ejemplo n.º 5
0
 /**
  * If class name passed in is an extension then return
  * the extension instance instead of creating a new class.
  *
  * @param string $class
  *
  * @return object
  */
 protected function instantiateController($class)
 {
     $refCls = new \ReflectionClass($class);
     if ($refCls->implementsInterface('\\Bolt\\Extensions\\ExtensionInterface')) {
         /** @var \Bolt\Extensions\ExtensionInterface[] $extensions */
         $extensions = $this->app['extensions']->getEnabled();
         foreach ($extensions as $extension) {
             if ($class === get_class($extension)) {
                 return $extension;
             }
         }
     }
     return parent::instantiateController($class);
 }
Ejemplo n.º 6
0
 protected function createController($controller)
 {
     if (false !== strpos($controller, '::')) {
         return parent::createController($controller);
     }
     if (false === strpos($controller, ':')) {
         throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
     }
     list($service, $method) = explode(':', $controller, 2);
     if (!isset($this->app[$service])) {
         throw new \InvalidArgumentException(sprintf('Service "%s" does not exist.', $controller));
     }
     return array($this->app[$service], $method);
 }
Ejemplo n.º 7
0
 protected function doGetArguments(Request $request, $controller, array $parameters)
 {
     foreach ($parameters as $param) {
         if ($param->isArray() && $param->getName() == 'params' && !$request->attributes->has('params')) {
             $request->attributes->set('params', $request->request->all());
             break;
         }
         if ($param->isArray() && $param->getName() == 'query' && !$request->attributes->has('query')) {
             $request->attributes->set('query', $request->query->all());
             break;
         }
     }
     return parent::doGetArguments($request, $controller, $parameters);
 }
Ejemplo n.º 8
0
 /**
  * We're overriding this protected method to auto-inject the application container
  * into our controllers.
  *
  * @param  string      $controller
  * @return array|mixed
  */
 protected function createController($controller)
 {
     if (false !== strpos($controller, '::')) {
         $instance = parent::createController($controller);
         // Injects container from side rather than constructor.
         if ($instance[0] instanceof BaseController) {
             $instance[0]->setApplication($this->app);
         }
         return $instance;
     }
     if (false === strpos($controller, ':')) {
         throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
     }
     list($service, $method) = explode(':', $controller, 2);
     if (!isset($this->app[$service])) {
         throw new \InvalidArgumentException(sprintf('Service "%s" does not exist.', $controller));
     }
     return [$this->app[$service], $method];
 }
Ejemplo n.º 9
0
 /**
  * {@inheritdoc}
  */
 protected function createController($controller)
 {
     if (false === strpos($controller, '::')) {
         $count = substr_count($controller, ':');
         if (2 == $count) {
             if (3 !== count($parts = explode(':', $controller))) {
                 throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid "a:b:c" controller string.', $controller));
             }
             // Controller in the a:b:c notation then
             list($namespace, $controller, $action) = $parts;
             $controller = str_replace('/', '\\', $controller);
             $try = sprintf('%s\\Controller\\%sController', $namespace, $controller);
             if (class_exists($try)) {
                 $controller = sprintf('%s::%sAction', $try, $action);
             }
         }
     }
     return parent::createController($controller);
 }
 protected function createController($controller)
 {
     if (is_string($controller) && class_exists($controller)) {
         $methodHandler = strtolower($this->request->getMethod());
         $method = null;
         $whitelistedMethods = array('get', 'post', 'put', 'patch', 'delete', 'copy', 'head', 'options', 'link', 'unlink', 'purge');
         if (!in_array($methodHandler, $whitelistedMethods)) {
             $methodHandler = '';
         }
         if (method_exists($controller, 'handleRequest')) {
             $method = 'handleRequest';
         } elseif (method_exists($controller, $methodHandler)) {
             $method = $methodHandler;
         } elseif (method_exists($controller, 'handleMissingMethod')) {
             $method = 'handleMissingMethod';
         } else {
             throw new \LogicException('No method handler found');
         }
         /** @var \SDispatcher\Common\ResourceOptionInterface $resourceOption */
         $resourceOption = $this->app['sdispatcher.resource_option'];
         $resourceOption->setTarget($controller);
         $svcIds = $resourceOption->getRequiredServices();
         if (empty($svcIds) && (is_subclass_of($controller, 'SDispatcher\\Common\\RequiredServiceMetaProviderInterface') || method_exists($controller, 'getRequiredServices'))) {
             $svcIds = (array) call_user_func("{$controller}::getRequiredServices");
         }
         if (!empty($svcIds)) {
             $reflection = new \ReflectionClass($controller);
             if ($reflection->getConstructor()) {
                 $deps = array();
                 foreach ($svcIds as $id) {
                     $deps[] = $this->app[$id];
                 }
                 $c = $reflection->newInstanceArgs($deps);
             } else {
                 $c = $reflection->newInstanceWithoutConstructor();
             }
         } else {
             $c = new $controller();
         }
         return array($c, $method);
     }
     return parent::createController($controller);
 }
Ejemplo n.º 11
0
 /**
  * @param Application $app
  * @param ContainerInterface $container
  */
 public function __construct(Application $app, ContainerInterface $container)
 {
     parent::__construct($app);
     $this->container = $container;
 }