vendor/symfony/src/Symfony/Component/HttpKernel/Client.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\HttpKernel;
       
    13 
       
    14 use Symfony\Component\HttpFoundation\File\UploadedFile;
       
    15 use Symfony\Component\HttpFoundation\Request;
       
    16 use Symfony\Component\BrowserKit\Client as BaseClient;
       
    17 use Symfony\Component\BrowserKit\Request as DomRequest;
       
    18 use Symfony\Component\BrowserKit\Response as DomResponse;
       
    19 use Symfony\Component\BrowserKit\Cookie as DomCookie;
       
    20 use Symfony\Component\BrowserKit\History;
       
    21 use Symfony\Component\BrowserKit\CookieJar;
       
    22 
       
    23 /**
       
    24  * Client simulates a browser and makes requests to a Kernel object.
       
    25  *
       
    26  * @author Fabien Potencier <fabien@symfony.com>
       
    27  *
       
    28  * @api
       
    29  */
       
    30 class Client extends BaseClient
       
    31 {
       
    32     protected $kernel;
       
    33 
       
    34     /**
       
    35      * Constructor.
       
    36      *
       
    37      * @param HttpKernelInterface $kernel    An HttpKernel instance
       
    38      * @param array               $server    The server parameters (equivalent of $_SERVER)
       
    39      * @param History             $history   A History instance to store the browser history
       
    40      * @param CookieJar           $cookieJar A CookieJar instance to store the cookies
       
    41      */
       
    42     public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
       
    43     {
       
    44         $this->kernel = $kernel;
       
    45 
       
    46         parent::__construct($server, $history, $cookieJar);
       
    47 
       
    48         $this->followRedirects = false;
       
    49     }
       
    50 
       
    51     /**
       
    52      * Makes a request.
       
    53      *
       
    54      * @param Request  $request A Request instance
       
    55      *
       
    56      * @return Response A Response instance
       
    57      */
       
    58     protected function doRequest($request)
       
    59     {
       
    60         return $this->kernel->handle($request);
       
    61     }
       
    62 
       
    63     /**
       
    64      * Returns the script to execute when the request must be insulated.
       
    65      *
       
    66      * @param Request $request A Request instance
       
    67      */
       
    68     protected function getScript($request)
       
    69     {
       
    70         $kernel = str_replace("'", "\\'", serialize($this->kernel));
       
    71         $request = str_replace("'", "\\'", serialize($request));
       
    72 
       
    73         $r = new \ReflectionClass('\\Symfony\\Component\\ClassLoader\\UniversalClassLoader');
       
    74         $requirePath = str_replace("'", "\\'", $r->getFileName());
       
    75 
       
    76         $symfonyPath = str_replace("'", "\\'", realpath(__DIR__.'/../../..'));
       
    77 
       
    78         return <<<EOF
       
    79 <?php
       
    80 
       
    81 require_once '$requirePath';
       
    82 
       
    83 \$loader = new Symfony\Component\ClassLoader\UniversalClassLoader();
       
    84 \$loader->registerNamespaces(array('Symfony' => '$symfonyPath'));
       
    85 \$loader->register();
       
    86 
       
    87 \$kernel = unserialize('$kernel');
       
    88 echo serialize(\$kernel->handle(unserialize('$request')));
       
    89 EOF;
       
    90     }
       
    91 
       
    92     /**
       
    93      * Converts the BrowserKit request to a HttpKernel request.
       
    94      *
       
    95      * @param DomRequest $request A Request instance
       
    96      *
       
    97      * @return Request A Request instance
       
    98      */
       
    99     protected function filterRequest(DomRequest $request)
       
   100     {
       
   101         $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
       
   102 
       
   103         $httpRequest->files->replace($this->filterFiles($httpRequest->files->all()));
       
   104 
       
   105         return $httpRequest;
       
   106     }
       
   107 
       
   108     /**
       
   109      * Filters an array of files.
       
   110      *
       
   111      * This method created test instances of UploadedFile so that the move()
       
   112      * method can be called on those instances.
       
   113      *
       
   114      * If the size of a file is greater than the allowed size (from php.ini) then
       
   115      * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
       
   116      *
       
   117      * @see Symfony\Component\HttpFoundation\File\UploadedFile
       
   118      *
       
   119      * @param array $files An array of files
       
   120      *
       
   121      * @return array An array with all uploaded files marked as already moved
       
   122      */
       
   123     protected function filterFiles(array $files)
       
   124     {
       
   125         $filtered = array();
       
   126         foreach ($files as $key => $value) {
       
   127             if (is_array($value)) {
       
   128                 $filtered[$key] = $this->filterFiles($value);
       
   129             } elseif ($value instanceof UploadedFile) {
       
   130                 if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
       
   131                     $filtered[$key] = new UploadedFile(
       
   132                         '',
       
   133                         $value->getClientOriginalName(),
       
   134                         $value->getClientMimeType(),
       
   135                         0,
       
   136                         UPLOAD_ERR_INI_SIZE,
       
   137                         true
       
   138                     );
       
   139                 } else {
       
   140                     $filtered[$key] = new UploadedFile(
       
   141                         $value->getPathname(),
       
   142                         $value->getClientOriginalName(),
       
   143                         $value->getClientMimeType(),
       
   144                         $value->getClientSize(),
       
   145                         $value->getError(),
       
   146                         true
       
   147                     );
       
   148                 }
       
   149             } else {
       
   150                 $filtered[$key] = $value;
       
   151             }
       
   152         }
       
   153 
       
   154         return $filtered;
       
   155     }
       
   156 
       
   157     /**
       
   158      * Converts the HttpKernel response to a BrowserKit response.
       
   159      *
       
   160      * @param Response $response A Response instance
       
   161      *
       
   162      * @return Response A Response instance
       
   163      */
       
   164     protected function filterResponse($response)
       
   165     {
       
   166         $headers = $response->headers->all();
       
   167         if ($response->headers->getCookies()) {
       
   168             $cookies = array();
       
   169             foreach ($response->headers->getCookies() as $cookie) {
       
   170                 $cookies[] = new DomCookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
       
   171             }
       
   172             $headers['Set-Cookie'] = implode(', ', $cookies);
       
   173         }
       
   174 
       
   175         return new DomResponse($response->getContent(), $response->getStatusCode(), $headers);
       
   176     }
       
   177 }