Author: Kévin Dunglas (dunglas@gmail.com)
Inheritance: implements Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface
 /**
  * {@inheritdoc}
  */
 public function start(Request $request, AuthenticationException $authException = null)
 {
     $factory = new DiactorosFactory();
     $request = $factory->createRequest($request);
     $response = new Response();
     $this->entry_point->start($request, $response);
     $factory = new HttpFoundationFactory();
     $response = $factory->createResponse($response);
     return $response;
 }
 /**
  * {@inheritDoc}
  */
 public function authenticate(TokenInterface $token)
 {
     $psr7Factory = new DiactorosFactory();
     $psr7Request = $psr7Factory->createRequest($token->getRequest());
     try {
         $key = $this->authenticator->authenticate($psr7Request);
         return new HmacToken($token->getRequest(), $key);
     } catch (\Exception $e) {
         throw new AuthenticationException($e->getMessage());
     }
 }
 /** {@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);
 }
示例#4
0
 /**
  * action for routing all requests directly to the third party API
  *
  * @param Request $request request
  *
  * @return \Psr\Http\Message\ResponseInterface|Response
  */
 public function proxyAction(Request $request)
 {
     $api = $this->decideApiAndEndpoint($request->getUri());
     $this->registerProxySources();
     $url = $this->apiLoader->getEndpoint($api['endpoint'], true);
     if (parse_url($url, PHP_URL_SCHEME) === false) {
         $scheme = $request->getScheme();
         $url = $scheme . '://' . $url;
     }
     $response = null;
     try {
         $newRequest = Request::create($url, $request->getMethod(), array(), array(), array(), array(), $request->getContent(false));
         $newRequest->headers->add($request->headers->all());
         $newRequest = $this->transformationHandler->transformRequest($api['apiName'], $api['endpoint'], $request, $newRequest);
         $psrRequest = $this->diactorosFactory->createRequest($newRequest);
         $psrRequest = $psrRequest->withUri($psrRequest->getUri()->withPort(parse_url($url, PHP_URL_PORT)));
         $psrResponse = $this->proxy->forward($psrRequest)->to($this->getHostWithScheme($url));
         $response = $this->httpFoundationFactory->createResponse($psrResponse);
         $this->transformationHandler->transformResponse($api['apiName'], $api['endpoint'], $response, clone $response);
     } catch (ClientException $e) {
         $response = $e->getResponse();
     } catch (ServerException $serverException) {
         $response = $serverException->getResponse();
     }
     return $response;
 }
 public function testValidRequestShouldBeHandledByTheNextMiddleware()
 {
     $content = '{"content": "This is the content"}';
     $request = Request::create('http://localhost/', 'GET', [], [], [], [], $content);
     $request->headers->set('X-Hub-Signature', sprintf('sha1=%s', hash_hmac('sha1', $content, 'my_secret')));
     $psrFactory = new DiactorosFactory();
     $foundationFactory = new HttpFoundationFactory();
     $psrRequest = $psrFactory->createRequest($request);
     $expectedRequest = $foundationFactory->createRequest($psrRequest);
     $response = new Response('OK');
     $expectedResponse = $foundationFactory->createResponse($psrFactory->createResponse($response));
     $next = $this->prophesize('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
     $next->handle($expectedRequest)->shouldBeCalledTimes(1)->willReturn($response);
     $middleware = new GitHubWebHook($next->reveal(), 'my_secret');
     $response = $middleware->handle($request);
     $this->assertEquals($expectedResponse, $response);
 }
 /**
  * @param FilterResponseEvent $event
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $request = $event->getRequest();
     $response = $event->getResponse();
     if ($request->attributes->has('hmac.key')) {
         $psr7Factory = new DiactorosFactory();
         $foundationFactory = new HttpFoundationFactory();
         $psr7Request = $psr7Factory->createRequest($request);
         $psr7Response = $psr7Factory->createResponse($response);
         $signer = new ResponseSigner($request->attributes->get('hmac.key'), $psr7Request);
         $signedResponse = $signer->signResponse($psr7Response);
         $event->setResponse($foundationFactory->createResponse($signedResponse));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function handle(GetResponseEvent $event)
 {
     $factory = new DiactorosFactory();
     $request = $factory->createRequest($event->getRequest());
     $token_id = $this->getAccessTokenTypeManager()->findAccessToken($request);
     if (null === $token_id) {
         return;
     }
     try {
         $token = new OAuth2Token();
         $token->setToken($token_id);
         $result = $this->authentication_manager->authenticate($token);
         $this->token_storage->setToken($result);
     } catch (AuthenticationException $e) {
         if (null !== $e->getPrevious()) {
             $e = $e->getPrevious();
         }
         $response = new Response($e->getMessage(), 401);
         $event->setResponse($response);
     }
 }
示例#8
0
 protected function injectArguments(array $parameters, $request)
 {
     $args = [];
     $repositoryManager = $this->app['repository-manager'];
     foreach ($parameters as $parameter) {
         $class = $parameter->getClass();
         if ($class) {
             $className = (string) $class->getName();
             if ($class->isInstance($this->app)) {
                 $args[$parameter->getName()] = $this->app;
             }
             if ($className == Twig_Environment::class) {
                 $args[$parameter->getName()] = $this->app['twig'];
             }
             if ($className == UrlGenerator::class) {
                 $args[$parameter->getName()] = $this->app['url_generator'];
             }
             if ($className == \Symfony\Component\Form\FormFactory::class) {
                 $args[$parameter->getName()] = $this->app['form.factory'];
             }
             if ($className == \Radvance\Model\SpaceInterface::class) {
                 $args[$parameter->getName()] = $this->app['space'];
             }
             if ($class->implementsInterface(ServerRequestInterface::class)) {
                 $psr7Factory = new DiactorosFactory();
                 $psrRequest = $psr7Factory->createRequest($request);
                 $args[$parameter->getName()] = $psrRequest;
             }
             if (substr($className, -10) == 'Repository') {
                 foreach ($repositoryManager->getRepositories() as $repository) {
                     if (get_class($repository) == $className) {
                         $args[$parameter->getName()] = $repository;
                     }
                 }
             }
         }
     }
     return $args;
 }
 /**
  * @return Bootstrap
  */
 protected function initHttp()
 {
     $factory = new DiactorosFactory();
     $this->setRequest($this->getContainer()->has(ServerRequestInterface::class) ? $this->getContainer()->get(ServerRequestInterface::class) : $factory->createRequest(Request::createFromGlobals()))->setResponse($this->getContainer()->has(ResponseInterface::class) ? $this->getContainer()->get(ResponseInterface::class) : $factory->createResponse(new Response()));
     return $this;
 }
 /**
  * Create scope controllers
  */
 public function createScopeControllers()
 {
     $this->configLoader->load();
     $silex = $this->silex;
     $manifests = $this->configLoader->getManifests();
     foreach ($manifests as $scope => $manifest) {
         /** @var ControllerCollection $controllers_factory */
         $controllers_factory = $this->silex['controllers_factory'];
         foreach ($manifest['routes'] as $http_method => $route) {
             $scope_path = $this->configLoader->getPath();
             /** @var Controller $routeController */
             $routeController = $controllers_factory->{$http_method}($route['route'], function (Request $request) use($silex, $scope, $manifest, $route, $scope_path) {
                 //Scope container
                 $scope_container = $silex['scopes'][$scope];
                 if (isset($silex['translator'])) {
                     $scope_container['translator'] = $silex['translator'];
                 }
                 if (isset($silex['url_generator'])) {
                     $scope_container['url_generator'] = $silex['url_generator'];
                 }
                 if (isset($silex['log_factory'])) {
                     $scope_container['log_factory'] = $silex['log_factory'];
                 }
                 if (isset($silex['predis'])) {
                     $scope_container['predis'] = $silex['predis'];
                 }
                 $silex['scopes'][$scope] = $scope_container;
                 /** @var Twig_Loader_Filesystem $twig_loader_filesystem */
                 $twig_loader_filesystem = $silex['twig.loader.filesystem'];
                 $twig_loader_filesystem->prependPath($scope_path . '/' . $scope . '/views');
                 //Create request
                 $psr7Factory = new DiactorosFactory();
                 $psrRequest = $psr7Factory->createRequest($request);
                 /** @var AbstractScopeController $scope_controller */
                 $namespace_controller = $this->scope_namespace . implode('\\', [$scope, 'controller', ucfirst($route['controller']) . 'Controller']);
                 $scope_controller = $namespace_controller::create($manifest, $silex['scopes'][$scope], $psrRequest, $this->base_namespace);
                 $scope_controller->setTwig($silex['twig']);
                 $psrResponse = $scope_controller->execute($route['call']);
                 //Create response
                 $httpFoundationFactory = new HttpFoundationFactory();
                 $symfonyResponse = $httpFoundationFactory->createResponse($psrResponse);
                 return $symfonyResponse;
             });
             //Filter
             if (is_array($route['filter'])) {
                 foreach ($route['filter'] as $filter) {
                     $routeController->before($this->silex['filter_factory']->createFilter($scope, $filter));
                 }
             }
             //Bind
             $routeController->bind(implode('::', [$route['controller'], $route['call']]));
         }
         $this->silex->mount($manifest['mount'], $controllers_factory);
     }
 }
示例#11
0
<?php

use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
require_once 'vendor/autoload.php';
$app = new Silex\Application();
include 'config.php';
include 'services.php';
$app->match('/{requestedUrl}', function (Request $symfonyRequest, $requestedUrl) use($app) {
    $psr7Factory = new DiactorosFactory();
    $request = $psr7Factory->createRequest($symfonyRequest);
    if (!$request->getBody()->isSeekable()) {
        $request = $request->withBody(new GuzzleHttp\Psr7\NoSeekStream($request->getBody()));
    }
    $data = $app['data.router']->data($request);
    return new Response($data, 200, ['Content-Type' => 'application/json']);
})->assert('requestedUrl', '.*');
$app->run();
 /**
  * Create a new instance of JsonApi by transforming a HttpFoundation Request into PSR7 Request.
  *
  * @param Request $request
  *
  * @return JsonApi
  */
 public function create(Request $request)
 {
     $request = $this->psrFactory->createRequest($request);
     return new JsonApi(new JsonApiRequest($request), new Response(), $this->exceptionFactory);
 }