Exemplo n.º 1
0
 /**
  * @param \Brick\App\View\View $view
  *
  * @return string
  */
 protected function renderAsString(View $view)
 {
     if ($this->injector) {
         $this->injector->inject($view);
     }
     return $view->render();
 }
Exemplo n.º 2
0
 /**
  * Renders a partial View.
  *
  * @param \Brick\App\View\View $view The View object to render.
  *
  * @return string The rendered View.
  */
 public final function partial(View $view)
 {
     if ($this->injector) {
         $this->injector->inject($view);
     }
     return $view->render();
 }
Exemplo n.º 3
0
 public function testCanInjectPrivateMethod()
 {
     $injector = new Injector(new DefaultValueResolver(), new NullPolicy());
     $object = $injector->instantiate(PrivateConstructor::class, ['foo' => 'Foo', 'bar' => 'Bar']);
     $this->assertSame('Foo', $object->foo);
     $this->assertSame('Bar', $object->bar);
 }
Exemplo n.º 4
0
 /**
  * Runs all sample data providers.
  *
  * @param callable|null $callback A function that will be called with the name of each provider as a parameter.
  * @return void
  */
 public function run(callable $callback = null)
 {
     $this->em->beginTransaction();
     foreach ($this->providers as $provider) {
         $this->em->clear();
         /** @var $provider \Brick\Sample\SampleDataProvider */
         $provider = $this->injector->instantiate($provider);
         if ($callback) {
             $callback($provider->getName());
         }
         $provider->run();
         $this->em->flush();
     }
     $this->em->commit();
 }
Exemplo n.º 5
0
 /**
  * @param \Brick\Http\Request $request The request to handle.
  *
  * @return \Brick\Http\Response The generated response.
  *
  * @throws \Brick\Http\Exception\HttpException If a route throws such an exception, or no route matches the request.
  * @throws \UnexpectedValueException           If a route or controller returned an invalid value.
  */
 private function handleRequest(Request $request)
 {
     $event = new IncomingRequestEvent($request);
     $this->eventDispatcher->dispatch(IncomingRequestEvent::class, $event);
     $match = $this->route($request);
     $event = new RouteMatchedEvent($request, $match);
     $this->eventDispatcher->dispatch(RouteMatchedEvent::class, $event);
     $controllerReflection = $match->getControllerReflection();
     $instance = null;
     $this->valueResolver->setRequest($request);
     $this->valueResolver->setParameters($match->getClassParameters());
     if ($controllerReflection instanceof \ReflectionMethod) {
         $className = $controllerReflection->getDeclaringClass()->getName();
         $instance = $this->injector->instantiate($className);
         $callable = $controllerReflection->getClosure($instance);
     } elseif ($controllerReflection instanceof \ReflectionFunction) {
         $callable = $controllerReflection->getClosure();
     } else {
         throw new \UnexpectedValueException('Unknown controller reflection type.');
     }
     $event = new ControllerReadyEvent($request, $match, $instance);
     $event->addParameters($match->getFunctionParameters());
     $this->eventDispatcher->dispatch(ControllerReadyEvent::class, $event);
     $response = $event->getResponse();
     if ($response === null) {
         $this->valueResolver->setParameters($event->getParameters());
         try {
             $result = $this->injector->invoke($callable);
             if ($result instanceof Response) {
                 $response = $result;
             } else {
                 $event = new NonResponseResultEvent($request, $match, $instance, $result);
                 $this->eventDispatcher->dispatch(NonResponseResultEvent::class, $event);
                 $response = $event->getResponse();
                 if ($response === null) {
                     throw $this->invalidReturnValue('controller', Response::class, $result);
                 }
             }
         } catch (HttpException $e) {
             $response = $this->handleHttpException($e, $request);
         } finally {
             $event = new ControllerInvocatedEvent($request, $match, $instance);
             $this->eventDispatcher->dispatch(ControllerInvocatedEvent::class, $event);
         }
     }
     $event = new ResponseReceivedEvent($request, $response, $match, $instance);
     $this->eventDispatcher->dispatch(ResponseReceivedEvent::class, $event);
     return $response;
 }