Пример #1
0
 public function __invoke(Request $request)
 {
     $repository = $this->getService('repository.blog_post');
     $id = $request->getAttribute('id');
     if (!($post = $repository->find($id))) {
         throw new HttpNotFoundException(sprintf('No blog post found for id #%u.', $id));
     }
     return $this->render('blog/show.twig', ['post' => $post]);
 }
 /**
  * @param Request $request
  * @return BaseController
  */
 public function create(Request $request)
 {
     $route = Route::findRoute($request->getRoutePart(), $request->getRequestType() == 'GET' ? Route::METHOD_GET : Route::METHOD_POST);
     if ($route == null) {
         $route = Route::getDefault();
     }
     $controllerName = "\\App\\Controllers\\" . $route->getControllerName();
     $obj = new $controllerName();
     $obj->setRequest($request);
     $obj->setRoute($route);
     return $obj;
 }
Пример #3
0
 public static function getRouteParameters(Route $route, Request $request)
 {
     $requestRoute = explode("/", $request->getRoutePart());
     $defRoute = explode("/", $route->routeName);
     $parameterStore = [];
     for ($i = 0; $i < count($requestRoute); ++$i) {
         if ($defRoute[$i] == "{value}") {
             $parameterStore[] = $requestRoute[$i];
         }
     }
     return $parameterStore;
 }
Пример #4
0
 /**
  * @dataProvider providerRequestParameters
  */
 public function testCreateRequestInstance($method, $path)
 {
     $request = new Request($method, $path, Request::HTTP, '1.1', ['Host' => 'http://wikipedia.com', 'User-Agent' => 'Mozilla/Firefox']);
     $this->assertSame($method, $request->getMethod());
     $this->assertSame($path, $request->getPath());
     $this->assertSame(Request::HTTP, $request->getScheme());
     $this->assertSame('1.1', $request->getSchemeVersion());
     $this->assertCount(2, $request->getHeaders());
     $this->assertSame(['host' => 'http://wikipedia.com', 'user-agent' => 'Mozilla/Firefox'], $request->getHeaders());
     $this->assertSame('http://wikipedia.com', $request->getHeader('Host'));
     $this->assertSame('Mozilla/Firefox', $request->getHeader('User-Agent'));
     $this->assertEmpty($request->getBody());
 }
Пример #5
0
 private function doHandleRequest(Request $request)
 {
     $event = $this->dispatch(KernelEvents::REQUEST, new KernelEvent($request));
     if ($event->hasResponse()) {
         return $event->getResponse();
     }
     // Create the controller based on request attributes
     $controller = $this->factory->createController($request->getAttributes());
     $event = $this->dispatch(KernelEvents::CONTROLLER, new ControllerEvent($controller, $request));
     $response = call_user_func_array($event->getController(), [$request]);
     if (!$response instanceof Response) {
         throw new \RuntimeException('A controller must return a Response object.');
     }
     return $response;
 }
Пример #6
0
 /**
  * @param Request $request
  *
  * @return bool
  */
 public function recognizeLocale(Request $request)
 {
     $locales = $this->container->parameters->get('locales', []);
     if (empty($locales)) {
         return false;
     }
     $string = implode('|', $locales);
     if (preg_match("#^/({$string})/?#", $request->getUrl(), $matches)) {
         $locale = $matches[1];
         $request->setUrl(preg_replace("#^/{$matches[1]}/?#", '/', $request->getUrl(), 1));
     } else {
         $locale = $locales[0];
     }
     $request->setLocale($locale);
     $this->container->get('translator')->setLocale($locale);
     return true;
 }
Пример #7
0
 /**
  * @expectedException \RuntimeException
  */
 public function testNoGeneratedResponseForException()
 {
     $em = new EventManager();
     $em->addEventListener(KernelEvents::EXCEPTION, function (ExceptionEvent $event) {
         // do not generate a response!
     });
     $kernel = new HttpKernel($em, new ClosureControllerFactory());
     $kernel->handle(Request::create('GET', '/home'));
 }
Пример #8
0
 public function testHandleRequest()
 {
     $dic = new ServiceLocator();
     $dic->register('http_kernel', function () {
         return new DummyHttpKernel();
     });
     $kernel = new Kernel($dic);
     $response = $kernel->handle(Request::create('GET', '/'));
     $this->assertInstanceOf(Response::class, $response);
     $this->assertSame('DUMMY!', $response->getBody());
 }
Пример #9
0
 public function start($applicationRoot)
 {
     $this->applicationRoot = $applicationRoot;
     include $this->applicationRoot . "/../App/routes.php";
     $request = new Request();
     $request->setGet($_GET);
     $request->setPost($_POST);
     $request->setSession(new Session($_SESSION));
     $request->setUri($_SERVER["REQUEST_URI"]);
     $request->setRequestType($_SERVER['REQUEST_METHOD']);
     return $this->draw($request);
 }
Пример #10
0
	public function viewCreate($params)
	{

		$strings = Config::load('guildTranslations', 'translations/guild');
		foreach ($strings as $key => $str) {
			Request::getInstance()->setParam('txt.' . $key, _($str));
		}
		Request::getInstance()->setParam('txt.enter_guild_name', _('alliance.enter_guild_name'));
		Request::getInstance()->setParam('txt.create', _('common.create'));

		$params['isLocked'] = getHero()->getLevel() < 15 ? true : false;

		return $params;
	}
Пример #11
0
    public function testCreateRequestFromMessage()
    {
        $message = <<<MESSAGE
GET /fr/article/42 HTTP/1.1
host: http://wikipedia.com
user-agent: Mozilla/Firefox
accept: text/plain, text/html

{"foo": "bar"}
MESSAGE;
        $request = Request::createFromMessage($message);
        $this->assertInstanceOf(Request::class, $request);
        $this->assertSame($message, $request->getMessage());
        $this->assertSame($message, (string) $request);
    }
Пример #12
0
 /**
  * @dataProvider provideGlobalInformation
  */
 public function testCreateFromGlobals($path, $expectedPath)
 {
     $_SERVER['PATH_INFO'] = $path;
     $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
     $_SERVER['REQUEST_METHOD'] = 'POST';
     $request = Request::createFromGlobals();
     $this->assertSame($expectedPath, $request->getPath());
     $this->assertSame('HTTP', $request->getScheme());
     $this->assertSame('1.1', $request->getSchemeVersion());
     $this->assertSame('POST', $request->getMethod());
 }
Пример #13
0
    public function testGetMessage()
    {
        $message = <<<MESSAGE
GET /home HTTP/1.1
host: http://wikipedia.com
user-agent: Mozilla/Firefox
content-type: application/json

{ "foo": "bar" }
MESSAGE;
        $body = '{ "foo": "bar" }';
        $headers = ['Host' => 'http://wikipedia.com', 'User-Agent' => 'Mozilla/Firefox', 'Content-Type' => 'application/json'];
        $request = new Request('GET', '/home', 'HTTP', '1.1', $headers, $body);
        $this->assertSame($message, $request->getMessage());
        $this->assertSame($message, (string) $request);
    }
Пример #14
0
 /**
  * @dataProvider provideRequestParameters
  */
 public function testCreateRequestInstance($method, $path)
 {
     $body = '{ "foo": "bar" }';
     $headers = ['Host' => 'http://wikipedia.com', 'User-Agent' => 'Mozilla/Firefox', 'Content-Type' => 'application/json'];
     $request = new Request($method, $path, Request::HTTP, '1.1', $headers, $body);
     $this->assertSame($method, $request->getMethod());
     $this->assertSame($path, $request->getPath());
     $this->assertSame(Request::HTTP, $request->getScheme());
     $this->assertSame('1.1', $request->getSchemeVersion());
     $this->assertSame($body, $request->getBody());
     $this->assertCount(3, $request->getHeaders());
     $this->assertSame(['host' => 'http://wikipedia.com', 'user-agent' => 'Mozilla/Firefox', 'content-type' => 'application/json'], $request->getHeaders());
     $this->assertSame('http://wikipedia.com', $request->getHeader('Host'));
     $this->assertSame('Mozilla/Firefox', $request->getHeader('User-Agent'));
     $this->assertSame('application/json', $request->getHeader('Content-Type'));
 }
Пример #15
0
 /**
  * @throws \React\Socket\ConnectionException
  */
 public function initialize()
 {
     $this->loop = Factory::create();
     $this->socket = new Server($this->loop);
     $this->http = new \React\Http\Server($this->socket);
     $this->http->on('request', function (\React\Http\Request $request, \React\Http\Response $response) {
         /** @var EventDispatcher $dispatcher */
         $dispatcher = $this->container->get('event_dispatcher');
         $kernelRequest = new KernelRequest($request->getMethod(), $request->getPath(), $request->getQuery(), $request->headers, $request->getVersion());
         $kernelRequest->findClientIp();
         $kernelRequest->on('response', function (KernelResponse $kernelResponse) use($response) {
             $response->writeHead($kernelResponse->getStatusCode(), $kernelResponse->getHeaders());
             $response->end($kernelResponse->getBody());
         });
         $kernelRequest->on('end', function () use($dispatcher, $kernelRequest) {
             $dispatcher->dispatch(new RequestEvent($kernelRequest));
         });
         $requestHandler = new RequestHandler();
         $requestHandler->on('end', function () use($kernelRequest) {
             $kernelRequest->emit('end');
         });
         $requestHandler->on('data', function (FormField $field) use($kernelRequest) {
             if ($field->isFile()) {
                 $filename = tempnam(sys_get_temp_dir(), '');
                 $dataHandler = function ($data) use($filename) {
                     file_put_contents($filename, $data, FILE_APPEND | FILE_BINARY);
                 };
                 $endDataHandler = function ($data) use($filename, $field, $kernelRequest, $dataHandler) {
                     $dataHandler($data);
                     if (!filesize($filename)) {
                         return;
                     }
                     $file = new UploadedFile($filename, $field->attributes->get(FormField::ORIGINAL_FILENAME), null, null, null, true);
                     parse_str($field->getName(), $data);
                     $key = key($data);
                     $parent = $data[$key];
                     $parent[key($parent)] = $file;
                     $kernelRequest->files->set($key, $parent);
                 };
             } else {
                 $total = '';
                 $dataHandler = function ($data) use(&$total) {
                     if (is_array($data)) {
                         $total = $data;
                     } else {
                         $total .= $data;
                     }
                 };
                 $endDataHandler = function ($data) use(&$total, $dataHandler, $kernelRequest, $field) {
                     $dataHandler($data);
                     $value = $kernelRequest->atributes->get($field->getName());
                     if ($value === null) {
                         $kernelRequest->atributes->set($field->getName(), $total);
                     } elseif (is_array($value)) {
                         $value[] = $total;
                         $kernelRequest->atributes->set($field->getName(), $value);
                     } else {
                         $kernelRequest->atributes->set($field->getName(), [$value, $total]);
                     }
                 };
             }
             $field->on('data', $dataHandler);
             $field->on('end', $endDataHandler);
         });
         $requestHandler->handle($request);
     });
     $this->socket->listen(8080, '0.0.0.0');
 }
Пример #16
0
 /**
  * @param Request $request
  *
  * @return \Symfony\Component\HttpFoundation\Request
  */
 private function convertRequest(Request $request)
 {
     return \Symfony\Component\HttpFoundation\Request::create($request->getUrl(), $request->getMethod(), $request->atributes->all(), $request->cookies->all(), $request->files->all(), []);
 }
Пример #17
0
<?php

require_once __DIR__ . "/../vendor/autoload.php";
use Framework\ControllerFactory;
use Framework\Http\Request;
use Framework\Http\StreamableInterface;
use Framework\Kernel;
use Framework\Routing\Loader\CompositeFileLoader;
use Framework\Routing\Loader\PhpFileLoader;
use Framework\Routing\Loader\XmlFileLoader;
use Framework\Routing\Router;
use Framework\Templating\BracketRenderer;
use Framework\Templating\PhpRenderer;
$loader = new Twig_Loader_Filesystem(__DIR__ . '/../app/views');
$twig = new Twig_Environment($loader, array('cache' => __DIR__ . '/../app/cache/twig', 'debug' => true));
$renderer = new PhpRenderer(__DIR__ . "/../app/views");
$renderer = new BracketRenderer(__DIR__ . '/../app/views');
$renderer = new \Framework\Templating\TwigRendererAdapter($twig);
$loader = new CompositeFileLoader();
$loader->add(new PhpFileLoader());
$loader->add(new XmlFileLoader());
$router = new Router(__DIR__ . "/../app/config/routes.xml", $loader);
$kernel = new Kernel($router, new ControllerFactory(), $renderer);
$response = $kernel->handle(Request::createFromGlobals());
if ($response instanceof StreamableInterface) {
    $response->send();
}
Пример #18
0
 public function isCurrentUrl($url)
 {
     return (bool) ($this->request !== null ? $url == $this->request->getUrl() : false);
 }