vendor/symfony/src/Symfony/Component/BrowserKit/CookieJar.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\BrowserKit;
       
    13 
       
    14 /**
       
    15  * CookieJar.
       
    16  *
       
    17  * @author Fabien Potencier <fabien@symfony.com>
       
    18  *
       
    19  * @api
       
    20  */
       
    21 class CookieJar
       
    22 {
       
    23     protected $cookieJar = array();
       
    24 
       
    25     /**
       
    26      * Sets a cookie.
       
    27      *
       
    28      * @param Cookie $cookie A Cookie instance
       
    29      *
       
    30      * @api
       
    31      */
       
    32     public function set(Cookie $cookie)
       
    33     {
       
    34         $this->cookieJar[$cookie->getName()] = $cookie;
       
    35     }
       
    36 
       
    37     /**
       
    38      * Gets a cookie by name.
       
    39      *
       
    40      * @param string $name The cookie name
       
    41      *
       
    42      * @return Cookie|null A Cookie instance or null if the cookie does not exist
       
    43      *
       
    44      * @api
       
    45      */
       
    46     public function get($name)
       
    47     {
       
    48         $this->flushExpiredCookies();
       
    49 
       
    50         return isset($this->cookieJar[$name]) ? $this->cookieJar[$name] : null;
       
    51     }
       
    52 
       
    53     /**
       
    54      * Removes a cookie by name.
       
    55      *
       
    56      * @param string $name The cookie name
       
    57      *
       
    58      * @api
       
    59      */
       
    60     public function expire($name)
       
    61     {
       
    62         unset($this->cookieJar[$name]);
       
    63     }
       
    64 
       
    65     /**
       
    66      * Removes all the cookies from the jar.
       
    67      *
       
    68      * @api
       
    69      */
       
    70     public function clear()
       
    71     {
       
    72         $this->cookieJar = array();
       
    73     }
       
    74 
       
    75     /**
       
    76      * Updates the cookie jar from a Response object.
       
    77      *
       
    78      * @param Response $response A Response object
       
    79      * @param string   $uri    The base URL
       
    80      */
       
    81     public function updateFromResponse(Response $response, $uri = null)
       
    82     {
       
    83         foreach ($response->getHeader('Set-Cookie', false) as $cookie) {
       
    84             $this->set(Cookie::fromString($cookie, $uri));
       
    85         }
       
    86     }
       
    87 
       
    88     /**
       
    89      * Returns not yet expired cookies.
       
    90      *
       
    91      * @return array An array of cookies
       
    92      */
       
    93     public function all()
       
    94     {
       
    95         $this->flushExpiredCookies();
       
    96 
       
    97         return $this->cookieJar;
       
    98     }
       
    99 
       
   100     /**
       
   101      * Returns not yet expired cookie values for the given URI.
       
   102      *
       
   103      * @param string  $uri A URI
       
   104      * @param Boolean $returnsRawValue Returns raw value or urldecoded value
       
   105      *
       
   106      * @return array An array of cookie values
       
   107      */
       
   108     public function allValues($uri, $returnsRawValue = false)
       
   109     {
       
   110         $this->flushExpiredCookies();
       
   111 
       
   112         $parts = array_replace(array('path' => '/'), parse_url($uri));
       
   113 
       
   114         $cookies = array();
       
   115         foreach ($this->cookieJar as $cookie) {
       
   116             if ($cookie->getDomain()) {
       
   117                 $domain = ltrim($cookie->getDomain(), '.');
       
   118                 if ($domain != substr($parts['host'], -strlen($domain))) {
       
   119                     continue;
       
   120                 }
       
   121             }
       
   122 
       
   123             if ($cookie->getPath() != substr($parts['path'], 0, strlen($cookie->getPath()))) {
       
   124                 continue;
       
   125             }
       
   126 
       
   127             if ($cookie->isSecure() && 'https' != $parts['scheme']) {
       
   128                 continue;
       
   129             }
       
   130 
       
   131             $cookies[$cookie->getName()] = $returnsRawValue ? $cookie->getRawValue() : $cookie->getValue();
       
   132         }
       
   133 
       
   134         return $cookies;
       
   135     }
       
   136 
       
   137     /**
       
   138      * Returns not yet expired raw cookie values for the given URI.
       
   139      *
       
   140      * @param string $uri A URI
       
   141      *
       
   142      * @return array An array of cookie values
       
   143      */
       
   144     public function allRawValues($uri)
       
   145     {
       
   146         return $this->allValues($uri, true);
       
   147     }
       
   148 
       
   149     /**
       
   150      * Removes all expired cookies.
       
   151      */
       
   152     public function flushExpiredCookies()
       
   153     {
       
   154         $cookies = $this->cookieJar;
       
   155         foreach ($cookies as $name => $cookie) {
       
   156             if ($cookie->isExpired()) {
       
   157                 unset($this->cookieJar[$name]);
       
   158             }
       
   159         }
       
   160     }
       
   161 }