"Fossies" - the Fresh Open Source Software Archive

Member "grav/system/src/Grav/Framework/Psr7/AbstractUri.php" (19 Mar 2020, 9175 Bytes) of package /linux/www/grav-v1.6.23.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractUri.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 /**
    4  * @package    Grav\Framework\Psr7
    5  *
    6  * @copyright  Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
    7  * @license    MIT License; see LICENSE file for details.
    8  */
    9 
   10 namespace Grav\Framework\Psr7;
   11 
   12 use Grav\Framework\Uri\UriPartsFilter;
   13 use Psr\Http\Message\UriInterface;
   14 
   15 /**
   16  * Bare minimum PSR7 implementation.
   17  *
   18  * @package Grav\Framework\Uri\Psr7
   19  * @deprecated 1.6 Using message PSR-7 decorators instead.
   20  */
   21 abstract class AbstractUri implements UriInterface
   22 {
   23     protected static $defaultPorts = [
   24         'http'  => 80,
   25         'https' => 443
   26     ];
   27 
   28     /** @var string Uri scheme. */
   29     private $scheme = '';
   30 
   31     /** @var string Uri user. */
   32     private $user = '';
   33 
   34     /** @var string Uri password. */
   35     private $password = '';
   36 
   37     /** @var string Uri host. */
   38     private $host = '';
   39 
   40     /** @var int|null Uri port. */
   41     private $port;
   42 
   43     /** @var string Uri path. */
   44     private $path = '';
   45 
   46     /** @var string Uri query string (without ?). */
   47     private $query = '';
   48 
   49     /** @var string Uri fragment (without #). */
   50     private $fragment = '';
   51 
   52     /**
   53      * Please define constructor which calls $this->init().
   54      */
   55     abstract public function __construct();
   56 
   57     /**
   58      * @inheritdoc
   59      */
   60     public function getScheme()
   61     {
   62         return $this->scheme;
   63     }
   64 
   65     /**
   66      * @inheritdoc
   67      */
   68     public function getAuthority()
   69     {
   70         $authority = $this->host;
   71 
   72         $userInfo = $this->getUserInfo();
   73         if ($userInfo !== '') {
   74             $authority = $userInfo . '@' . $authority;
   75         }
   76 
   77         if ($this->port !== null) {
   78             $authority .= ':' . $this->port;
   79         }
   80 
   81         return $authority;
   82     }
   83 
   84     /**
   85      * @inheritdoc
   86      */
   87     public function getUserInfo()
   88     {
   89         $userInfo = $this->user;
   90 
   91         if ($this->password !== '') {
   92             $userInfo .= ':' . $this->password;
   93         }
   94 
   95         return $userInfo;
   96     }
   97 
   98     /**
   99      * @inheritdoc
  100      */
  101     public function getHost()
  102     {
  103         return $this->host;
  104     }
  105 
  106     /**
  107      * @inheritdoc
  108      */
  109     public function getPort()
  110     {
  111         return $this->port;
  112     }
  113 
  114     /**
  115      * @inheritdoc
  116      */
  117     public function getPath()
  118     {
  119         return $this->path;
  120     }
  121 
  122     /**
  123      * @inheritdoc
  124      */
  125     public function getQuery()
  126     {
  127         return $this->query;
  128     }
  129 
  130     /**
  131      * @inheritdoc
  132      */
  133     public function getFragment()
  134     {
  135         return $this->fragment;
  136     }
  137 
  138     /**
  139      * @inheritdoc
  140      */
  141     public function withScheme($scheme)
  142     {
  143         $scheme = UriPartsFilter::filterScheme($scheme);
  144 
  145         if ($this->scheme === $scheme) {
  146             return $this;
  147         }
  148 
  149         $new = clone $this;
  150         $new->scheme = $scheme;
  151         $new->unsetDefaultPort();
  152         $new->validate();
  153 
  154         return $new;
  155     }
  156 
  157     /**
  158      * @inheritdoc
  159      * @throws \InvalidArgumentException
  160      */
  161     public function withUserInfo($user, $password = null)
  162     {
  163         $user = UriPartsFilter::filterUserInfo($user);
  164         $password = UriPartsFilter::filterUserInfo($password ?? '');
  165 
  166         if ($this->user === $user && $this->password === $password) {
  167             return $this;
  168         }
  169 
  170         $new = clone $this;
  171         $new->user = $user;
  172         $new->password = $user !== '' ? $password : '';
  173         $new->validate();
  174 
  175         return $new;
  176     }
  177 
  178     /**
  179      * @inheritdoc
  180      */
  181     public function withHost($host)
  182     {
  183         $host = UriPartsFilter::filterHost($host);
  184 
  185         if ($this->host === $host) {
  186             return $this;
  187         }
  188 
  189         $new = clone $this;
  190         $new->host = $host;
  191         $new->validate();
  192 
  193         return $new;
  194     }
  195 
  196     /**
  197      * @inheritdoc
  198      */
  199     public function withPort($port)
  200     {
  201         $port = UriPartsFilter::filterPort($port);
  202 
  203         if ($this->port === $port) {
  204             return $this;
  205         }
  206 
  207         $new = clone $this;
  208         $new->port = $port;
  209         $new->unsetDefaultPort();
  210         $new->validate();
  211 
  212         return $new;
  213     }
  214 
  215     /**
  216      * @inheritdoc
  217      */
  218     public function withPath($path)
  219     {
  220         $path = UriPartsFilter::filterPath($path);
  221 
  222         if ($this->path === $path) {
  223             return $this;
  224         }
  225 
  226         $new = clone $this;
  227         $new->path = $path;
  228         $new->validate();
  229 
  230         return $new;
  231     }
  232 
  233     /**
  234      * @inheritdoc
  235      */
  236     public function withQuery($query)
  237     {
  238         $query = UriPartsFilter::filterQueryOrFragment($query);
  239 
  240         if ($this->query === $query) {
  241             return $this;
  242         }
  243 
  244         $new = clone $this;
  245         $new->query = $query;
  246 
  247         return $new;
  248     }
  249 
  250     /**
  251      * @inheritdoc
  252      * @throws \InvalidArgumentException
  253      */
  254     public function withFragment($fragment)
  255     {
  256         $fragment = UriPartsFilter::filterQueryOrFragment($fragment);
  257 
  258         if ($this->fragment === $fragment) {
  259             return $this;
  260         }
  261 
  262         $new = clone $this;
  263         $new->fragment = $fragment;
  264 
  265         return $new;
  266     }
  267 
  268     /**
  269      * @return string
  270      */
  271     public function __toString()
  272     {
  273         return $this->getUrl();
  274     }
  275 
  276     /**
  277      * @return array
  278      */
  279     protected function getParts()
  280     {
  281         return [
  282             'scheme'    => $this->scheme,
  283             'host'      => $this->host,
  284             'port'      => $this->port,
  285             'user'      => $this->user,
  286             'pass'      => $this->password,
  287             'path'      => $this->path,
  288             'query'     => $this->query,
  289             'fragment'  => $this->fragment
  290         ];
  291     }
  292 
  293     /**
  294      * Return the fully qualified base URL ( like http://getgrav.org ).
  295      *
  296      * Note that this method never includes a trailing /
  297      *
  298      * @return string
  299      */
  300     protected function getBaseUrl()
  301     {
  302         $uri = '';
  303 
  304         $scheme = $this->getScheme();
  305         if ($scheme !== '') {
  306             $uri .= $scheme . ':';
  307         }
  308 
  309         $authority = $this->getAuthority();
  310         if ($authority !== '' || $scheme === 'file') {
  311             $uri .= '//' . $authority;
  312         }
  313 
  314         return $uri;
  315     }
  316 
  317     /**
  318      * @return string
  319      */
  320     protected function getUrl()
  321     {
  322         $uri = $this->getBaseUrl() . $this->getPath();
  323 
  324         $query = $this->getQuery();
  325         if ($query !== '') {
  326             $uri .= '?' . $query;
  327         }
  328 
  329         $fragment = $this->getFragment();
  330         if ($fragment !== '') {
  331             $uri .= '#' . $fragment;
  332         }
  333 
  334         return $uri;
  335     }
  336 
  337     /**
  338      * @return string
  339      */
  340     protected function getUser()
  341     {
  342         return $this->user;
  343     }
  344 
  345     /**
  346      * @return string
  347      */
  348     protected function getPassword()
  349     {
  350         return $this->password;
  351     }
  352 
  353     /**
  354      * @param array $parts
  355      * @throws \InvalidArgumentException
  356      */
  357     protected function initParts(array $parts)
  358     {
  359         $this->scheme = isset($parts['scheme']) ? UriPartsFilter::filterScheme($parts['scheme']) : '';
  360         $this->user = isset($parts['user']) ? UriPartsFilter::filterUserInfo($parts['user']) : '';
  361         $this->password = isset($parts['pass']) ? UriPartsFilter::filterUserInfo($parts['pass']) : '';
  362         $this->host = isset($parts['host']) ? UriPartsFilter::filterHost($parts['host']) : '';
  363         $this->port = isset($parts['port']) ? UriPartsFilter::filterPort((int)$parts['port']) : null;
  364         $this->path = isset($parts['path']) ? UriPartsFilter::filterPath($parts['path']) : '';
  365         $this->query = isset($parts['query']) ? UriPartsFilter::filterQueryOrFragment($parts['query']) : '';
  366         $this->fragment = isset($parts['fragment']) ? UriPartsFilter::filterQueryOrFragment($parts['fragment']) : '';
  367 
  368         $this->unsetDefaultPort();
  369         $this->validate();
  370     }
  371 
  372     /**
  373      * @throws \InvalidArgumentException
  374      */
  375     private function validate()
  376     {
  377         if ($this->host === '' && ($this->scheme === 'http' || $this->scheme === 'https')) {
  378             throw new \InvalidArgumentException('Uri with a scheme must have a host');
  379         }
  380 
  381         if ($this->getAuthority() === '') {
  382             if (0 === strpos($this->path, '//')) {
  383                 throw new \InvalidArgumentException('The path of a URI without an authority must not start with two slashes \'//\'');
  384             }
  385             if ($this->scheme === '' && false !== strpos(explode('/', $this->path, 2)[0], ':')) {
  386                 throw new \InvalidArgumentException('A relative URI must not have a path beginning with a segment containing a colon');
  387             }
  388         } elseif (isset($this->path[0]) && $this->path[0] !== '/') {
  389             throw new \InvalidArgumentException('The path of a URI with an authority must start with a slash \'/\' or be empty');
  390         }
  391     }
  392 
  393     protected function isDefaultPort()
  394     {
  395         $scheme = $this->scheme;
  396         $port = $this->port;
  397 
  398         return $this->port === null
  399             || (isset(static::$defaultPorts[$scheme]) && $port === static::$defaultPorts[$scheme]);
  400     }
  401 
  402     private function unsetDefaultPort()
  403     {
  404         if ($this->isDefaultPort()) {
  405             $this->port = null;
  406         }
  407     }
  408 }