Hoa central
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Hoa\Router\Http\Http Class Reference
Inheritance diagram for Hoa\Router\Http\Http:

Public Member Functions

 __construct (Array $parameters=[])
 
 getParameters ()
 
 route ($uri=null, $prefix=null)
 
 unroute ($id, Array $variables=[], $secured=null, $prefix=null)
 
 getMethod ()
 
 isAsynchronous ()
 
 getStrictDomain ()
 
 getSubdomain ($withSuffix=true)
 
 setSubdomainStack ($stack)
 
 getSubdomainStack ()
 
 setSubdomainSuffix ($suffix)
 
 getSubdomainSuffix ()
 
 setPrefix ($prefix)
 
 getPrefix ()
 
 setDefaultPort ($port, $secure=self::UNSECURE)
 
 getDefaultPort ($secure=self::UNSECURE)
 
- Public Member Functions inherited from Hoa\Router\Generic
 addRule ($id, Array $methods, $pattern, $call=null, $able=null, Array $variables=[])
 
 addPrivateRule ($id, Array $methods, $pattern, $call=null, $able=null, Array $variables=[])
 
 __call ($name, $arguments)
 
 removeRule ($id)
 
 ruleExists ($id)
 
 getRules ()
 
 getRule ($id)
 
getTheRule ()
 
- Public Member Functions inherited from Hoa\Router\Router
 route ()
 
 unroute ($id, Array $variables=[])
 

Static Public Member Functions

static getURI ()
 
static getQuery ()
 
static getDomain ()
 
static getPort ()
 
static getBootstrap ()
 
static isSecure ()
 

Public Attributes

const SECURE = true
 
const UNSECURE = false
 
- Public Attributes inherited from Hoa\Router\Router
const VISIBILITY_PUBLIC = 0
 
const VISIBILITY_PRIVATE = 1
 
const RULE_VISIBILITY = 0
 
const RULE_ID = 1
 
const RULE_METHODS = 2
 
const RULE_PATTERN = 3
 
const RULE_CALL = 4
 
const RULE_ABLE = 5
 
const RULE_VARIABLES = 6
 

Protected Member Functions

 _addRule ($visibility, $id, Array $methods, $pattern, $call, $able, Array $variables)
 
 _unroute ($id, $pattern, Array $variables, $allowEmpty=true)
 
- Protected Member Functions inherited from Hoa\Router\Generic
 _addRule ($visibility, $id, Array $methods, $pattern, $call, $able, Array $variables)
 

Protected Attributes

 $_parameters = null
 
 $_pathPrefix = null
 
 $_httpPort = 80
 
 $_httpsPort = 443
 
 $_subdomainStack = _static
 
 $_subdomainSuffix = null
 
- Protected Attributes inherited from Hoa\Router\Generic
 $_rules = []
 
 $_rule = null
 

Static Protected Attributes

static $_methods
 
- Static Protected Attributes inherited from Hoa\Router\Generic
static $_methods = []
 

Detailed Description

Class .

HTTP router.

Definition at line 50 of file Http.php.

Constructor & Destructor Documentation

Hoa\Router\Http\Http::__construct ( Array  $parameters = [])

Constructor.

Returns
void

Definition at line 138 of file Http.php.

139  {
140  $this->_parameters = new Core\Parameter(
141  $this,
142  [],
143  [
144  'prefix' => null,
145  'rules.public' => [],
146  'rules.private' => []
147  ]
148  );
149  $this->_parameters->setParameters($parameters);
150 
151  if (null === $prefix = $this->_parameters->getParameter('prefix')) {
152  $this->setPrefix(
153  ('\\' === $_ = dirname($this->getBootstrap())) ? '/' : $_
154  );
155  } else {
156  $this->setPrefix($prefix);
157  }
158 
159  foreach ($this->_parameters->getParameter('rules.public') as $id => $rule) {
160  @list($methods, $pattern, $call, $able, $variables)
161  = $rule;
162 
163  if (null === $variables) {
164  $variables = [];
165  }
166 
167  $this->addRule($id, $methods, $pattern, $call, $able, $variables);
168  }
169 
170  foreach ($this->_parameters->getParameter('rules.private') as $id => $rule) {
171  @list($methods, $pattern, $call, $able, $variables)
172  = $rule;
173 
174  if (null === $variables) {
175  $variables = [];
176  }
177 
178  $this->addPrivateRule(
179  $id, $methods, $pattern, $call, $able, $variables
180  );
181  }
182 
183  $this->setDefaultPort(static::getPort(), static::isSecure());
184 
185  return;
186  }
setPrefix($prefix)
Definition: Http.php:930
addRule($id, Array $methods, $pattern, $call=null, $able=null, Array $variables=[])
Definition: Generic.php:108
setDefaultPort($port, $secure=self::UNSECURE)
Definition: Http.php:955
static getBootstrap()
Definition: Http.php:913
addPrivateRule($id, Array $methods, $pattern, $call=null, $able=null, Array $variables=[])
Definition: Generic.php:139

Here is the call graph for this function:

Member Function Documentation

Hoa\Router\Http\Http::_addRule (   $visibility,
  $id,
Array  $methods,
  $pattern,
  $call,
  $able,
Array  $variables 
)
protected

Fallback for add*Rule() methods.

Parameters
int$visibilityVisibility (please, see Router::VISIBILITY_* constants).
string$idID.
array$methodsHTTP methods allowed by the rule.
string$patternPattern (on-subdomain-request).
mixed$callCall (first part).
mixed$ableAble (second part).
array$variablesVariables (default or additional values).
Returns
Exceptions

Definition at line 212 of file Http.php.

220  {
221  if (true === $this->ruleExists($id)) {
222  throw new Router\Exception(
223  'Cannot add rule %s because it already exists.',
224  0,
225  $id
226  );
227  }
228 
229  array_walk($methods, function (&$method) {
230  $method = strtolower($method);
231  });
232  $diff = array_diff($methods, static::$_methods);
233 
234  if (!empty($diff)) {
235  throw new Router\Exception(
236  (1 == count($diff)
237  ? 'Method %s is'
238  : 'Methods %s are') .
239  ' invalid for the rule %s (valid methods are: %s).',
240  1,
241  [
242  implode(', ', $diff),
243  $id,
244  implode(', ', static::$_methods)
245  ]
246  );
247  }
248 
249  if (_static === $this->_subdomainStack &&
250  false !== strpos($pattern, '@')) {
251  $this->_subdomainStack = _dynamic;
252  }
253 
254  $this->_rules[$id] = [
255  Router::RULE_VISIBILITY => $visibility,
256  Router::RULE_ID => $id,
257  Router::RULE_METHODS => $methods,
258  Router::RULE_PATTERN => $pattern,
259  Router::RULE_CALL => $call,
260  Router::RULE_ABLE => $able,
261  Router::RULE_VARIABLES => $variables
262  ];
263 
264  return $this;
265  }
const RULE_METHODS
Definition: Router.php:84
const RULE_VARIABLES
Definition: Router.php:112
const RULE_VISIBILITY
Definition: Router.php:70
const RULE_PATTERN
Definition: Router.php:91
const RULE_CALL
Definition: Router.php:98

Here is the call graph for this function:

Hoa\Router\Http\Http::_unroute (   $id,
  $pattern,
Array  $variables,
  $allowEmpty = true 
)
protected

Real unroute method.

Parameters
string$idID.
string$patternPattern.
array$variablesVariables.
bool$allowEmptyWhether allow empty variables.
Returns
string
Exceptions

Definition at line 618 of file Http.php.

623  {
624  // (?<named>…)
625  $out = preg_replace_callback(
626  '#\(\?<([^>]+)>[^\)]*\)[\?\*\+]{0,2}#',
627  function (Array $matches) use (&$id, &$variables, &$allowEmpty) {
628  $m = strtolower($matches[1]);
629 
630  if (!isset($variables[$m]) || '' === $variables[$m]) {
631  if (true === $allowEmpty) {
632  return '';
633  } else {
634  throw new Router\Exception(
635  'Variable %s is empty and it is not allowed when ' .
636  'unrouting rule %s.',
637  6,
638  [$m, $id]
639  );
640  }
641  }
642 
643  return $variables[$m];
644  },
645  // (-…)
646  preg_replace('#\(\?\-?[imsxUXJ]+\)#', '', $pattern)
647  );
648 
649  // (?:
650  $out = preg_replace('#(?<!\\\)\(\?:#', '(', $out);
651 
652  // (…)?, (…)*
653  $out = preg_replace('#(?<!\\\)\((.*)(?<!\\\)\)[\?\*]#', '\1', $out);
654 
655  // (…)+
656  $out = preg_replace('#(?<!\\\)\((.+)(?<!\\\)\)\+#', '\1', $out);
657 
658  // …?, …*, …+
659  $out = preg_replace('#(.)(?<![\)\\\])[\?\*\+]#', '\1', $out);
660 
661  return str_replace(
662  [
663  '\.', '\\\\', '\+', '\*', '\?', '\[', '\]', '\^', '\$', '\(',
664  '\)', '\{', '\}', '\=', '\!', '<', '>', '\|', '\:', '\-'
665  ],
666  [
667  '.', '\\', '+', '*', '?', '[', ']', '^', '$', '(',
668  ')', '{', '}', '=', '!', '<', '>', '|', ':', '-'
669  ],
670  $out
671  );
672  }

Here is the caller graph for this function:

static Hoa\Router\Http\Http::getBootstrap ( )
static

Get bootstrap (script name).

Returns
string

Definition at line 913 of file Http.php.

914  {
915  $sapi = php_sapi_name();
916 
917  if ('cli' === $sapi || 'cli-server' === $sapi) {
918  return '';
919  }
920 
921  return $_SERVER['SCRIPT_NAME'];
922  }

Here is the caller graph for this function:

Hoa\Router\Http\Http::getDefaultPort (   $secure = self::UNSECURE)

Get HTTP port.

Parameters
bool$secureWhether the connection is secured.
Returns
int

Definition at line 974 of file Http.php.

975  {
976  if (static::UNSECURE === $secure) {
977  return $this->_httpPort;
978  }
979 
980  return $this->_httpsPort;
981  }
static Hoa\Router\Http\Http::getDomain ( )
static

Get domain (with subdomain if exists).

Returns
string

Definition at line 758 of file Http.php.

759  {
760  static $domain = null;
761 
762  if (null === $domain) {
763  if ('cli' === php_sapi_name()) {
764  return $domain = '';
765  }
766 
767  $domain = $_SERVER['SERVER_NAME'];
768 
769  if (empty($domain)) {
770  $domain = $_SERVER['SERVER_ADDR'];
771  }
772 
773  if (0 !== preg_match('#^(.+):' . static::getPort() . '$#', $domain, $m)) {
774  $domain = $m[1];
775  }
776  }
777 
778  return $domain;
779  }
Hoa\Router\Http\Http::getMethod ( )

Get HTTP method.

Returns
string

Implements Hoa\Router\Router.

Definition at line 679 of file Http.php.

680  {
681  if ('cli' === php_sapi_name()) {
682  return 'get';
683  }
684 
685  return strtolower($_SERVER['REQUEST_METHOD']);
686  }

Here is the caller graph for this function:

Hoa\Router\Http\Http::getParameters ( )

Get parameters.

Returns

Implements Hoa\Core\Parameter\Parameterizable.

Definition at line 193 of file Http.php.

194  {
195  return $this->_parameters;
196  }
static Hoa\Router\Http\Http::getPort ( )
static

Get port.

Returns
int

Definition at line 899 of file Http.php.

900  {
901  if ('cli' === php_sapi_name()) {
902  return 80;
903  }
904 
905  return (int) $_SERVER['SERVER_PORT'];
906  }
Hoa\Router\Http\Http::getPrefix ( )

Get path prefix (aka “base”).

Returns
string

Definition at line 943 of file Http.php.

944  {
945  return $this->_pathPrefix;
946  }

Here is the caller graph for this function:

static Hoa\Router\Http\Http::getQuery ( )
static

Get query.

Returns
array

Definition at line 732 of file Http.php.

733  {
734  if ('cli' === php_sapi_name()) {
735  return [];
736  }
737 
738  if (!isset($_SERVER['REQUEST_URI'])) {
739  throw new Router\Exception('Cannot find URI so we cannot get query.', 8);
740  }
741 
742  $uri = $_SERVER['REQUEST_URI'];
743 
744  if (false === $pos = strpos($uri, '?')) {
745  return [];
746  }
747 
748  parse_str(substr($uri, $pos + 1), $out);
749 
750  return $out;
751  }
Hoa\Router\Http\Http::getStrictDomain ( )

Get strict domain (i.e. without subdomain).

Returns
string

Definition at line 786 of file Http.php.

787  {
788  $sub = $this->getSubdomain();
789 
790  if (empty($sub)) {
791  return static::getDomain();
792  }
793 
794  return substr(static::getDomain(), strlen($sub) + 1);
795  }
getSubdomain($withSuffix=true)
Definition: Http.php:803

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Router\Http\Http::getSubdomain (   $withSuffix = true)

Get subdomain.

Parameters
bool$withSuffixWith or without suffix.
Returns
string

Definition at line 803 of file Http.php.

804  {
805  static $subdomain = null;
806 
807  if (null === $subdomain) {
808  $domain = static::getDomain();
809 
810  if (empty($domain)) {
811  return null;
812  }
813 
814  if ($domain == long2ip(ip2long($domain))) {
815  return null;
816  }
817 
818  if (2 > substr_count($domain, '.', 1)) {
819  return null;
820  }
821 
822  $subdomain = substr(
823  $domain,
824  0,
825  strrpos(
826  $domain,
827  '.',
828  -(strlen($domain) - strrpos($domain, '.') + 1)
829  )
830  );
831  }
832 
833  if (true === $withSuffix) {
834  return $subdomain;
835  }
836 
837  $suffix = $this->getSubdomainSuffix();
838 
839  if (null === $suffix) {
840  return $subdomain;
841  }
842 
843  return substr($subdomain, 0, -strlen($suffix) - 1);
844  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Router\Http\Http::getSubdomainStack ( )

Get subdomain stack.

Returns
int

Definition at line 865 of file Http.php.

866  {
867  return $this->_subdomainStack;
868  }

Here is the caller graph for this function:

Hoa\Router\Http\Http::getSubdomainSuffix ( )

Get subdomain suffix.

Returns
string

Definition at line 889 of file Http.php.

890  {
892  }

Here is the caller graph for this function:

static Hoa\Router\Http\Http::getURI ( )
static

Get URI.

Returns
string
Exceptions

Definition at line 708 of file Http.php.

709  {
710  if ('cli' === php_sapi_name()) {
711  return ltrim(@$_SERVER['argv'][1] ?: '', '/');
712  }
713 
714  if (!isset($_SERVER['REQUEST_URI'])) {
715  throw new Router\Exception('Cannot find URI so we cannot route.', 7);
716  }
717 
718  $uri = ltrim(urldecode($_SERVER['REQUEST_URI']), '/');
719 
720  if (false !== $pos = strpos($uri, '?')) {
721  $uri = substr($uri, 0, $pos);
722  }
723 
724  return $uri;
725  }
Hoa\Router\Http\Http::isAsynchronous ( )

Whether the router is called asynchronously or not.

Returns
bool

Implements Hoa\Router\Router.

Definition at line 693 of file Http.php.

694  {
695  if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
696  return false;
697  }
698 
699  return 'xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']);
700  }
static Hoa\Router\Http\Http::isSecure ( )
static

Whether the connection is secure.

Returns
bool

Definition at line 988 of file Http.php.

989  {
990  if (!isset($_SERVER['HTTPS'])) {
991  return static::UNSECURE;
992  }
993 
994  return
995  (!empty($_SERVER['HTTPS']) && 'off' !== $_SERVER['HTTPS'])
996  ? static::SECURE
997  : static::UNSECURE;
998  }
Hoa\Router\Http\Http::route (   $uri = null,
  $prefix = null 
)

Find the appropriated rule. Special variables: _domain, _subdomain, _call, _able and _request.

Parameters
string$uriURI. If null, it will be deduced. Can contain subdomain.
string$prefixPath prefix. If null, it will be deduced.
Returns
Exceptions

Definition at line 277 of file Http.php.

278  {
279  if (null === $uri) {
280  $uri = static::getURI();
281  $subdomain = $this->getSubdomain();
282  } else {
283  if (false !== $pos = strpos($uri, '@')) {
284  list($subdomain, $uri) = explode('@', $uri, 2);
285  } else {
286  $subdomain = $this->getSubdomain();
287  }
288 
289  $uri = ltrim(urldecode($uri), '/');
290  }
291 
292  if (null === $prefix) {
293  $prefix = $this->getPrefix();
294  }
295 
296  if (!empty($prefix)) {
297  $prefix = ltrim($prefix, '/');
298 
299  if (0 === preg_match('#^' . $prefix . '(.*)?$#', $uri, $matches)) {
300  throw new Router\Exception\NotFound(
301  'Cannot match the path prefix %s in the URI %s.',
302  2,
303  [$prefix, $uri]
304  );
305  }
306 
307  $uri = ltrim($matches[1], '/');
308  }
309 
310  // Please, see http://php.net/language.variables.external, section “Dots
311  // in incoming variable names”.
312  unset($_REQUEST[$_uri = str_replace('.', '_', $uri)]);
313  unset($_GET[$_uri]);
314 
315  $method = $this->getMethod();
316  $subdomainStack = $this->getSubdomainStack();
317  $subdomainSuffix = $this->getSubdomainSuffix();
318 
319  if (null !== $subdomainSuffix) {
320  $subdomainSuffix = '\.' . $subdomainSuffix;
321  }
322 
323  $rules = array_filter(
324  $this->getRules(),
325  function ($rule) use (
326  &$method,
327  &$subdomain,
328  &$subdomainStack,
329  &$subdomainSuffix
330  ) {
332  return false;
333  }
334 
335  if (false === in_array($method, $rule[Router::RULE_METHODS])) {
336  return false;
337  }
338 
339  if (false !== $pos = strpos($rule[Router::RULE_PATTERN], '@')) {
340  if (empty($subdomain)) {
341  return false;
342  } else {
343  return 0 !== preg_match(
344  '#^' .
345  substr($rule[Router::RULE_PATTERN], 0, $pos) .
346  $subdomainSuffix .
347  '$#i',
348  $subdomain
349  );
350  }
351  }
352 
353  return
354  _dynamic == $subdomainStack
355  ? empty($subdomain)
356  : true;
357  }
358  );
359 
360  if (empty($rules)) {
361  throw new Router\Exception\NotFound(
362  'No rule to apply to route %s.',
363  3,
364  $uri
365  );
366  }
367 
368  $gotcha = false;
369 
370  foreach ($rules as $rule) {
371  $pattern = $rule[Router::RULE_PATTERN];
372 
373  if (false !== $pos = strpos($pattern, '@')) {
374  $pattern = substr($pattern, $pos + 1);
375  }
376 
377  $pattern = ltrim($pattern, '/');
378 
379  if (0 !== preg_match('#^' . $pattern . '$#i', $uri, $muri)) {
380  $gotcha = true;
381 
382  break;
383  }
384  }
385 
386  if (false === $gotcha) {
387  throw new Router\Exception\NotFound(
388  'Cannot found an appropriated rule to route %s.',
389  4,
390  $uri
391  );
392  }
393 
394  if (false !== $pos) {
395  preg_match(
396  '#^' .
397  substr($rule[Router::RULE_PATTERN], 0, $pos) .
398  $subdomainSuffix .
399  '$#i',
400  $subdomain,
401  $msubdomain
402  );
403  } else {
404  $msubdomain = [];
405  }
406 
407  array_shift($muri);
408  $sub = array_shift($msubdomain) ?: null;
409  $rule[Router::RULE_VARIABLES]['_uri'] = $uri;
410  $rule[Router::RULE_VARIABLES]['_method'] = $method;
411  $rule[Router::RULE_VARIABLES]['_domain'] = static::getDomain();
412  $rule[Router::RULE_VARIABLES]['_subdomain'] = $sub;
413  $rule[Router::RULE_VARIABLES]['_call'] = &$rule[Router::RULE_CALL];
414  $rule[Router::RULE_VARIABLES]['_able'] = &$rule[Router::RULE_ABLE];
415  $rule[Router::RULE_VARIABLES]['_request'] = $_REQUEST;
416 
417  $caseless = 0 === preg_match(
418  '#\(\?\-[imsxUXJ]+\)#',
419  $rule[Router::RULE_PATTERN]
420  );
421 
422  foreach (array_merge($muri, $msubdomain) as $key => $value) {
423  if (!is_string($key)) {
424  continue;
425  }
426 
427  if (true === $caseless) {
428  $key = mb_strtolower($key);
429  }
430 
431  if (isset($rule[Router::RULE_VARIABLES][$key]) && empty($value)) {
432  continue;
433  }
434 
435  if (true === $caseless) {
436  $value = mb_strtolower($value);
437  }
438 
439  $rule[Router::RULE_VARIABLES][$key] = $value;
440  }
441 
442  $this->_rule = $rule;
443 
444  return $this;
445  }
const RULE_METHODS
Definition: Router.php:84
const VISIBILITY_PUBLIC
Definition: Router.php:56
const RULE_VARIABLES
Definition: Router.php:112
const RULE_VISIBILITY
Definition: Router.php:70
getSubdomain($withSuffix=true)
Definition: Http.php:803
const RULE_PATTERN
Definition: Router.php:91
const RULE_CALL
Definition: Router.php:98

Here is the call graph for this function:

Hoa\Router\Http\Http::setDefaultPort (   $port,
  $secure = self::UNSECURE 
)

Set port.

Parameters
int$portPort.
bool$secureWhether the connection is secured.
Returns
int

Definition at line 955 of file Http.php.

956  {
957  if (static::UNSECURE === $secure) {
958  $old = $this->_httpPort;
959  $this->_httpPort = $port;
960  } else {
961  $old = $this->_httpsPort;
962  $this->_httpsPort = $port;
963  }
964 
965  return $old;
966  }

Here is the caller graph for this function:

Hoa\Router\Http\Http::setPrefix (   $prefix)

Set path prefix.

Parameters
string$prefixPath prefix.
Returns
string

Definition at line 930 of file Http.php.

931  {
932  $old = $this->_pathPrefix;
933  $this->_pathPrefix = preg_quote(rtrim($prefix, '/'));
934 
935  return $old;
936  }

Here is the caller graph for this function:

Hoa\Router\Http\Http::setSubdomainStack (   $stack)

Set subdomain stack: static or dynamic.

Parameters
int$stackStack: _static or _dynamic constants.
Returns
int

Definition at line 852 of file Http.php.

853  {
854  $old = $this->_subdomainStack;
855  $this->_subdomainStack = $stack;
856 
857  return $old;
858  }
Hoa\Router\Http\Http::setSubdomainSuffix (   $suffix)

Set subdomain suffix.

Parameters
string$suffixSuffix.
Returns
string

Definition at line 876 of file Http.php.

877  {
879  $this->_subdomainSuffix = preg_quote($suffix);
880 
881  return $old;
882  }
Hoa\Router\Http\Http::unroute (   $id,
Array  $variables = [],
  $secured = null,
  $prefix = null 
)

Unroute a rule (i.e. route()^-1). Special variables: _subdomain and _fragment. _subdomain accepts 3 keywords:

  • root to go back to the root (with the smallest subdomain);
  • self to copy the current subdomain (useful if you want a complete URL with protocol etc., not only the query part);
  • shift to shift a subdomain part, i.e. going to the upper domain; if you want to shift x times, just type shift * x.
Parameters
string$idID.
array$variablesVariables.
bool$secureWhether the connection is secured. If null, will use the self::isSecure() value.
string$prefixPath prefix. If null, it will be deduced.
Returns
string
Exceptions

Definition at line 465 of file Http.php.

470  {
471  if (null === $prefix) {
472  $prefix = $this->getPrefix();
473  }
474 
475  $suffix = $this->getSubdomainSuffix();
476  $rule = $this->getRule($id);
477  $pattern = $rule[Router::RULE_PATTERN];
478 
479  foreach ($variables as $KeY => $value) {
480  if ($KeY != $key = strtolower($KeY)) {
481  unset($variables[$KeY]);
482  $variables[$key] = $value;
483  }
484  }
485 
486  $variables = array_merge($rule[Router::RULE_VARIABLES], $variables);
487  $anchor = !empty($variables['_fragment'])
488  ? '#' . $variables['_fragment']
489  : null;
490  unset($variables['_fragment']);
491 
492  $self = $this;
493  $prependPrefix = function ($unroute) use (&$prefix) {
494  if (0 !== preg_match('#^https?://#', $unroute)) {
495  return $unroute;
496  }
497 
498  return $prefix . $unroute;
499  };
500  $getPort = function ($secure) use ($self) {
501  $defaultPort = $self->getDefaultPort($secure);
502 
503  if (static::UNSECURE === $secure) {
504  return 80 !== $defaultPort ? ':' . $defaultPort : '';
505  }
506 
507  return 443 !== $defaultPort ? ':' . $defaultPort : '';
508  };
509 
510  if (true === array_key_exists('_subdomain', $variables) &&
511  null !== $variables['_subdomain']) {
512  if (empty($variables['_subdomain'])) {
513  throw new Router\Exception(
514  'Subdomain is empty, cannot unroute the rule %s properly.',
515  5,
516  $id
517  );
518  }
519 
520  $secure = null === $secured ? static::isSecure() : $secured;
521 
522  if (false !== $pos = strpos($pattern, '@')) {
523  $pattern = substr($pattern, $pos + 1);
524  }
525 
526  $subdomain = $variables['_subdomain'];
527  $handle = strtolower($subdomain);
528 
529  switch ($handle) {
530  case '__self__':
531  $subdomain = $this->getSubdomain();
532 
533  break;
534 
535  case '__root__':
536  $subdomain = '';
537 
538  break;
539 
540  default:
541  if (0 !== preg_match('#__shift__(?:\s*\*\s*(\d+))?#', $handle, $m)) {
542  $repetition = isset($m[1]) ? (int) $m[1] : 1;
543  $subdomain = $this->getSubdomain();
544 
545  for (; $repetition >= 1; --$repetition) {
546  if (false === $pos = strpos($subdomain, '.')) {
547  $subdomain = '';
548 
549  break;
550  }
551 
552  $subdomain = substr($subdomain, $pos + 1);
553  }
554 
555  break;
556  }
557 
558  if (null !== $suffix) {
559  $subdomain .= '.' . $suffix;
560  }
561 
562  break;
563  }
564 
565  if (!empty($subdomain)) {
566  $subdomain .= '.';
567  }
568 
569  return
570  (true === $secure ? 'https://' : 'http://') .
571  $subdomain .
572  $this->getStrictDomain() .
573  $getPort($secure) .
574  $prependPrefix($this->_unroute($id, $pattern, $variables)) .
575  $anchor;
576  }
577 
578  if (false !== $pos = strpos($pattern, '@')) {
579  $subPattern = substr($pattern, 0, $pos);
580  $pattern = substr($pattern, $pos + 1);
581 
582  if (null !== $suffix) {
583  $subPattern .= '.' . $suffix;
584  }
585 
586  if ($suffix === $subPattern) {
587  return
588  $prependPrefix($this->_unroute($id, $pattern, $variables)) .
589  $anchor;
590  }
591 
592  $secure = null === $secured ? static::isSecure() : $secured;
593 
594  return
595  (true === $secure ? 'https://' : 'http://') .
596  $this->_unroute($id, $subPattern, $variables, false) .
597  '.' . $this->getStrictDomain() .
598  $getPort($secure) .
599  $prependPrefix($this->_unroute($id, $pattern, $variables)) .
600  $anchor;
601  }
602 
603  return
604  $prependPrefix($this->_unroute($id, $pattern, $variables)) .
605  $anchor;
606  }
_unroute($id, $pattern, Array $variables, $allowEmpty=true)
Definition: Http.php:618
const RULE_VARIABLES
Definition: Router.php:112
getSubdomain($withSuffix=true)
Definition: Http.php:803
const RULE_PATTERN
Definition: Router.php:91

Here is the call graph for this function:

Member Data Documentation

Hoa\Router\Http\Http::$_httpPort = 80
protected

Definition at line 85 of file Http.php.

Hoa\Router\Http\Http::$_httpsPort = 443
protected

Definition at line 92 of file Http.php.

Hoa\Router\Http\Http::$_methods
staticprotected
Initial value:
= [
'connect',
'delete',
'get',
'head',
'link',
'options',
'patch',
'post',
'put',
'trace',
'unlink'
]

Definition at line 100 of file Http.php.

Hoa\Router\Http\Http::$_parameters = null
protected

Definition at line 71 of file Http.php.

Hoa\Router\Http\Http::$_pathPrefix = null
protected

Definition at line 78 of file Http.php.

Hoa\Router\Http\Http::$_subdomainStack = _static
protected

Definition at line 121 of file Http.php.

Hoa\Router\Http\Http::$_subdomainSuffix = null
protected

Definition at line 129 of file Http.php.

const Hoa\Router\Http\Http::SECURE = true

Secure connection.

bool

Definition at line 57 of file Http.php.

const Hoa\Router\Http\Http::UNSECURE = false

Unsecure connection.

bool

Definition at line 64 of file Http.php.


The documentation for this class was generated from the following file: