vendor/symfony/src/Symfony/Bundle/FrameworkBundle/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\Bundle\FrameworkBundle;
       
    13 
       
    14 use Symfony\Component\DependencyInjection\ContainerInterface;
       
    15 use Symfony\Component\HttpKernel\HttpKernelInterface;
       
    16 use Symfony\Component\HttpKernel\Client as BaseClient;
       
    17 use Symfony\Component\HttpKernel\Profiler\Profiler as HttpProfiler;
       
    18 use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
       
    19 use Symfony\Component\HttpFoundation\Request;
       
    20 use Symfony\Component\HttpFoundation\Response;
       
    21 
       
    22 /**
       
    23  * Client simulates a browser and makes requests to a Kernel object.
       
    24  *
       
    25  * @author Fabien Potencier <fabien@symfony.com>
       
    26  */
       
    27 class Client extends BaseClient
       
    28 {
       
    29     private $hasPerformedRequest = false;
       
    30 
       
    31     /**
       
    32      * Returns the container.
       
    33      *
       
    34      * @return ContainerInterface
       
    35      */
       
    36     public function getContainer()
       
    37     {
       
    38         return $this->kernel->getContainer();
       
    39     }
       
    40 
       
    41     /**
       
    42      * Returns the kernel.
       
    43      *
       
    44      * @return HttpKernelInterface
       
    45      */
       
    46     public function getKernel()
       
    47     {
       
    48         return $this->kernel;
       
    49     }
       
    50 
       
    51     /**
       
    52      * Gets the profile associated with the current Response.
       
    53      *
       
    54      * @return HttpProfile A Profile instance
       
    55      */
       
    56     public function getProfile()
       
    57     {
       
    58         if (!$this->kernel->getContainer()->has('profiler')) {
       
    59             return false;
       
    60         }
       
    61 
       
    62         return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
       
    63     }
       
    64 
       
    65     /**
       
    66      * Makes a request.
       
    67      *
       
    68      * @param Request $request A Request instance
       
    69      *
       
    70      * @return Response A Response instance
       
    71      */
       
    72     protected function doRequest($request)
       
    73     {
       
    74         // avoid shutting down the Kernel if no request has been performed yet
       
    75         // WebTestCase::createClient() boots the Kernel but do not handle a request
       
    76         if ($this->hasPerformedRequest) {
       
    77             $this->kernel->shutdown();
       
    78         } else {
       
    79             $this->hasPerformedRequest = true;
       
    80         }
       
    81 
       
    82         return $this->kernel->handle($request);
       
    83     }
       
    84 
       
    85     /**
       
    86      * Returns the script to execute when the request must be insulated.
       
    87      *
       
    88      * @param Request $request A Request instance
       
    89      *
       
    90      * @return string The script content
       
    91      */
       
    92     protected function getScript($request)
       
    93     {
       
    94         $kernel = str_replace("'", "\\'", serialize($this->kernel));
       
    95         $request = str_replace("'", "\\'", serialize($request));
       
    96 
       
    97         $r = new \ReflectionObject($this->kernel);
       
    98         $path = str_replace("'", "\\'", $r->getFileName());
       
    99 
       
   100         return <<<EOF
       
   101 <?php
       
   102 
       
   103 require_once '$path';
       
   104 
       
   105 \$kernel = unserialize('$kernel');
       
   106 \$kernel->boot();
       
   107 echo serialize(\$kernel->handle(unserialize('$request')));
       
   108 EOF;
       
   109     }
       
   110 }