3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Routing; /** * A Route describes a route and its parameters. * * @author Fabien Potencier <fabien@symfony.com> * @author Tobias Schultze <http://tobion.de> * * @api */ class Route implements \Serializable { /** * @var string */ private $path = '/'; /** * @var string */ private $host = ''; /** * @var array */ private $schemes = array(); /** * @var array */ private $methods = array(); /** * @var array */ private $defaults = array(); /** * @var array */ private $requirements = array(); /** * @var array */ private $options = array(); /** * @var null|CompiledRoute */ private $compiled; private $condition; /** * Constructor. * * Available options: * * * compiler_class: A class name able to compile this route instance (RouteCompiler by default) * * @param string $path The path pattern to match * @param array $defaults An array of default parameter values * @param array $requirements An array of requirements for parameters (regexes) * @param array $options An array of options * @param string $host The host pattern to match * @param string|array $schemes A required URI scheme or an array of restricted schemes * @param string|array $methods A required HTTP method or an array of restricted methods * @param string $condition A condition that should evaluate to true for the route to match * * @api */ public function __construct($path, array $defaults = array(), array $requirements = array(), array $options = array(), $host = '', $schemes = array(), $methods = array(), $condition = null) { $this->setPath($path); $this->setDefaults($defaults); $this->setRequirements($requirements); $this->setOptions($options); $this->setHost($host); // The conditions make sure that an initial empty $schemes/$methods does not override the corresponding requirement. // They can be removed when the BC layer is removed. if ($schemes) { $this->setSchemes($schemes); } if ($methods) { $this->setMethods($methods); } $this->setCondition($condition); } public function serialize() { return serialize(array( 'path' => $this->path, 'host' => $this->host, 'defaults' => $this->defaults, 'requirements' => $this->requirements, 'options' => $this->options, 'schemes' => $this->schemes, 'methods' => $this->methods, 'condition' => $this->condition, )); } public function unserialize($data) { $data = unserialize($data); $this->path = $data['path']; $this->host = $data['host']; $this->defaults = $data['defaults']; $this->requirements = $data['requirements']; $this->options = $data['options']; $this->schemes = $data['schemes']; $this->methods = $data['methods']; $this->condition = $data['condition']; } /** * Returns the pattern for the path. * * @return string The pattern * * @deprecated Deprecated in 2.2, to be removed in 3.0. Use getPath instead. */ public function getPattern() { return $this->path; } /** * Sets the pattern for the path. * * This method implements a fluent interface. * * @param string $pattern The path pattern * * @return Route The current Route instance * * @deprecated Deprecated in 2.2, to be removed in 3.0. Use setPath instead. */ public function setPattern($pattern) { return $this->setPath($pattern); } /** * Returns the pattern for the path. * * @return string The path pattern */ public function getPath() { return $this->path; } /** * Sets the pattern for the path. * * This method implements a fluent interface. * * @param string $pattern The path pattern * * @return Route The current Route instance */ public function setPath($pattern) { // A pattern must start with a slash and must not have multiple slashes at the beginning because the // generated path for this route would be confused with a network path, e.g. '//domain.com/path'. $this->path = '/'.ltrim(trim($pattern), '/'); $this->compiled = null; return $this; } /** * Returns the pattern for the host. * * @return string The host pattern */ public function getHost() { return $this->host; } /** * Sets the pattern for the host. * * This method implements a fluent interface. * * @param string $pattern The host pattern * * @return Route The current Route instance */ public function setHost($pattern) { $this->host = (string) $pattern; $this->compiled = null; return $this; } /** * Returns the lowercased schemes this route is restricted to. * So an empty array means that any scheme is allowed. * * @return array The schemes */ public function getSchemes() { return $this->schemes; } /** * Sets the schemes (e.g. 'https') this route is restricted to. * So an empty array means that any scheme is allowed. * * This method implements a fluent interface. * * @param string|array $schemes The scheme or an array of schemes * * @return Route The current Route instance */ public function setSchemes($schemes) { $this->schemes = array_map('strtolower', (array) $schemes); // this is to keep BC and will be removed in a future version if ($this->schemes) { $this->requirements['_scheme'] = implode('|', $this->schemes); } else { unset($this->requirements['_scheme']); } $this->compiled = null; return $this; } /** * Checks if a scheme requirement has been set. * * @param string $scheme * * @return bool true if the scheme requirement exists, otherwise false */ public function hasScheme($scheme) { $scheme = strtolower($scheme); foreach ($this->schemes as $requiredScheme) { if ($scheme === $requiredScheme) { return true; } } return false; } /** * Returns the uppercased HTTP methods this route is restricted to. * So an empty array means that any method is allowed. * * @return array The schemes */ public function getMethods() { return $this->methods; } /** * Sets the HTTP methods (e.g. 'POST') this route is restricted to. * So an empty array means that any method is allowed. * * This method implements a fluent interface. * * @param string|array $methods The method or an array of methods * * @return Route The current Route instance */ public function setMethods($methods) { $this->methods = array_map('strtoupper', (array) $methods); // this is to keep BC and will be removed in a future version if ($this->methods) { $this->requirements['_method'] = implode('|', $this->methods); } else { unset($this->requirements['_method']); } $this->compiled = null; return $this; } /** * Returns the options. * * @return array The options */ public function getOptions() { return $this->options; } /** * Sets the options. * * This method implements a fluent interface. * * @param array $options The options * * @return Route The current Route instance */ public function setOptions(array $options) { $this->options = array( 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler', ); return $this->addOptions($options); } /** * Adds options. * * This method implements a fluent interface. * * @param array $options The options * * @return Route The current Route instance */ public function addOptions(array $options) { foreach ($options as $name => $option) { $this->options[$name] = $option; } $this->compiled = null; return $this; } /** * Sets an option value. * * This method implements a fluent interface. * * @param string $name An option name * @param mixed $value The option value * * @return Route The current Route instance * * @api */ public function setOption($name, $value) { $this->options[$name] = $value; $this->compiled = null; return $this; } /** * Get an option value. * * @param string $name An option name * * @return mixed The option value or null when not given */ public function getOption($name) { return isset($this->options[$name]) ? $this->options[$name] : null; } /** * Checks if an option has been set * * @param string $name An option name * * @return bool true if the option is set, false otherwise */ public function hasOption($name) { return array_key_exists($name, $this->options); } /** * Returns the defaults. * * @return array The defaults */ public function getDefaults() { return $this->defaults; } /** * Sets the defaults. * * This method implements a fluent interface. * * @param array $defaults The defaults * * @return Route The current Route instance */ public function setDefaults(array $defaults) { $this->defaults = array(); return $this->addDefaults($defaults); } /** * Adds defaults. * * This method implements a fluent interface. * * @param array $defaults The defaults * * @return Route The current Route instance */ public function addDefaults(array $defaults) { foreach ($defaults as $name => $default) { $this->defaults[$name] = $default; } $this->compiled = null; return $this; } /** * Gets a default value. * * @param string $name A variable name * * @return mixed The default value or null when not given */ public function getDefault($name) { return isset($this->defaults[$name]) ? $this->defaults[$name] : null; } /** * Checks if a default value is set for the given variable. * * @param string $name A variable name * * @return bool true if the default value is set, false otherwise */ public function hasDefault($name) { return array_key_exists($name, $this->defaults); } /** * Sets a default value. * * @param string $name A variable name * @param mixed $default The default value * * @return Route The current Route instance * * @api */ public function setDefault($name, $default) { $this->defaults[$name] = $default; $this->compiled = null; return $this; } /** * Returns the requirements. * * @return array The requirements */ public function getRequirements() { return $this->requirements; } /** * Sets the requirements. * * This method implements a fluent interface. * * @param array $requirements The requirements * * @return Route The current Route instance */ public function setRequirements(array $requirements) { $this->requirements = array(); return $this->addRequirements($requirements); } /** * Adds requirements. * * This method implements a fluent interface. * * @param array $requirements The requirements * * @return Route The current Route instance */ public function addRequirements(array $requirements) { foreach ($requirements as $key => $regex) { $this->requirements[$key] = $this->sanitizeRequirement($key, $regex); } $this->compiled = null; return $this; } /** * Returns the requirement for the given key. * * @param string $key The key * * @return string|null The regex or null when not given */ public function getRequirement($key) { return isset($this->requirements[$key]) ? $this->requirements[$key] : null; } /** * Checks if a requirement is set for the given key. * * @param string $key A variable name * * @return bool true if a requirement is specified, false otherwise */ public function hasRequirement($key) { return array_key_exists($key, $this->requirements); } /** * Sets a requirement for the given key. * * @param string $key The key * @param string $regex The regex * * @return Route The current Route instance * * @api */ public function setRequirement($key, $regex) { $this->requirements[$key] = $this->sanitizeRequirement($key, $regex); $this->compiled = null; return $this; } /** * Returns the condition. * * @return string The condition */ public function getCondition() { return $this->condition; } /** * Sets the condition. * * This method implements a fluent interface. * * @param string $condition The condition * * @return Route The current Route instance */ public function setCondition($condition) { $this->condition = (string) $condition; $this->compiled = null; return $this; } /** * Compiles the route. * * @return CompiledRoute A CompiledRoute instance * * @throws \LogicException If the Route cannot be compiled because the * path or host pattern is invalid * * @see RouteCompiler which is responsible for the compilation process */ public function compile() { if (null !== $this->compiled) { return $this->compiled; } $class = $this->getOption('compiler_class'); return $this->compiled = $class::compile($this); } private function sanitizeRequirement($key, $regex) { if (!is_string($regex)) { throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string.', $key)); } if ('' !== $regex && '^' === $regex[0]) { $regex = (string) substr($regex, 1); // returns false for a single character } if ('$' === substr($regex, -1)) { $regex = substr($regex, 0, -1); } if ('' === $regex) { throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty.', $key)); } // this is to keep BC and will be removed in a future version if ('_scheme' === $key) { $this->setSchemes(explode('|', $regex)); } elseif ('_method' === $key) { $this->setMethods(explode('|', $regex)); } return $regex; } } $r = new Route('/d', array('f'=>'g')); var_dump($s = serialize($r));

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.70.0060.01018.55
8.3.60.0130.00718.80
8.3.50.0110.01121.19
8.3.40.0110.00418.94
8.3.30.0040.01219.22
8.3.20.0080.00020.52
8.3.10.0080.00023.61
8.3.00.0060.00319.38
8.2.180.0120.00316.63
8.2.170.0040.01122.96
8.2.160.0070.00720.52
8.2.150.0030.00624.18
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0050.00322.25
8.2.110.0060.00320.38
8.2.100.0040.00818.34
8.2.90.0030.00519.36
8.2.80.0030.00617.97
8.2.70.0000.00817.75
8.2.60.0060.00318.29
8.2.50.0040.00418.07
8.2.40.0030.00620.07
8.2.30.0030.00518.20
8.2.20.0000.00817.83
8.2.10.0080.00018.25
8.2.00.0040.00417.74
8.1.280.0110.01125.92
8.1.270.0030.00622.23
8.1.260.0050.00326.35
8.1.250.0040.00428.09
8.1.240.0090.00023.96
8.1.230.0000.01119.13
8.1.220.0050.00317.77
8.1.210.0000.00918.77
8.1.200.0000.01117.25
8.1.190.0040.00417.35
8.1.180.0000.00818.10
8.1.170.0000.00818.50
8.1.160.0030.00622.14
8.1.150.0020.00518.96
8.1.140.0040.00417.58
8.1.130.0000.00817.94
8.1.120.0070.00017.61
8.1.110.0000.00817.55
8.1.100.0000.00817.60
8.1.90.0000.00717.59
8.1.80.0000.00717.50
8.1.70.0000.00717.54
8.1.60.0060.00317.66
8.1.50.0000.00917.63
8.1.40.0060.00317.65
8.1.30.0030.00617.67
8.1.20.0030.00517.74
8.1.10.0000.00817.67
8.1.00.0000.00817.56
8.0.300.0040.00418.77
8.0.290.0080.00016.88
8.0.280.0000.00718.50
8.0.270.0040.00417.38
8.0.260.0000.00817.40
8.0.250.0000.00717.13
8.0.240.0080.00017.15
8.0.230.0050.00217.06
8.0.220.0040.00416.98
8.0.210.0030.00316.91
8.0.200.0000.00717.15
8.0.190.0030.00617.09
8.0.180.0040.00416.93
8.0.170.0080.00017.09
8.0.160.0000.00717.08
8.0.150.0040.00416.91
8.0.140.0050.00216.97
8.0.130.0000.00613.38
8.0.120.0000.00816.98
8.0.110.0030.00517.05
8.0.100.0040.00416.92
8.0.90.0040.00416.93
8.0.80.0090.00617.01
8.0.70.0080.00016.87
8.0.60.0070.00016.93
8.0.50.0080.00017.04
8.0.30.0090.01516.97
8.0.20.0090.01517.40
8.0.10.0000.00916.96
8.0.00.0070.01216.86
7.4.330.0020.00215.00
7.4.320.0000.00616.78
7.4.300.0070.00016.70
7.4.290.0040.00416.52
7.4.280.0000.00816.72
7.4.270.0050.00316.59
7.4.260.0070.00016.58
7.4.250.0040.00416.61
7.4.240.0000.00716.75
7.4.230.0040.00416.43
7.4.220.0060.01216.74
7.4.210.0070.00716.76
7.4.200.0000.00716.54
7.4.160.0100.00716.71
7.4.150.0130.00717.40
7.4.140.0070.01417.86
7.4.130.0080.00916.62
7.4.120.0110.00616.63
7.4.110.0120.00416.43
7.4.100.0090.00916.67
7.4.90.0170.00716.82
7.4.80.0070.01119.39
7.4.70.0070.01016.46
7.4.60.0110.00616.57
7.4.50.0000.00616.55
7.4.40.0130.00316.69
7.4.30.0120.01116.68
7.4.00.0030.01314.95
7.3.330.0000.00513.31
7.3.320.0050.00013.50
7.3.310.0000.00716.50
7.3.300.0030.00316.45
7.3.290.0090.00616.47
7.3.280.0100.00816.45
7.3.270.0120.00617.40
7.3.260.0110.00716.79
7.3.250.0090.00816.49
7.3.240.0140.00716.42
7.3.230.0070.01016.73
7.3.210.0120.00916.70
7.3.200.0030.01919.39
7.3.190.0070.01716.55
7.3.180.0060.01016.71
7.3.170.0100.00616.48
7.3.160.0110.00516.55
7.2.330.0110.00716.90
7.2.320.0070.01016.71
7.2.310.0070.01016.89
7.2.300.0100.00716.56
7.2.290.0080.01316.98
7.2.60.0060.00916.76
7.1.200.0070.00715.83
7.1.70.0060.01016.91
7.1.60.0090.00919.11
7.1.50.0130.01017.11
7.1.00.0000.08022.54
7.0.200.0040.00416.48
7.0.140.0030.07322.11
7.0.100.0000.08020.27
7.0.90.0200.08019.96
7.0.80.0100.08319.94
7.0.70.0170.07719.99
7.0.60.0030.09020.16
7.0.50.0370.07720.39
7.0.40.0070.04720.13
7.0.30.0200.07320.11
7.0.20.0100.08020.09
7.0.10.0170.07319.95
7.0.00.0030.07320.14
5.6.280.0030.07720.87
5.6.250.0070.08020.71
5.6.240.0070.07720.69
5.6.230.0070.06320.72
5.6.220.0070.06720.68
5.6.210.0100.07720.75
5.6.200.0030.09021.14
5.6.190.0030.08321.20
5.6.180.0070.08721.22
5.6.170.0030.06321.13
5.6.160.0030.07321.13
5.6.150.0030.07721.13
5.6.140.0030.07721.17
5.6.130.0130.08321.04
5.6.120.0030.05321.14
5.6.110.0030.09021.13
5.6.100.0070.04721.21
5.6.90.0170.03721.12
5.6.80.0100.05020.48
5.6.70.0030.07020.42
5.6.60.0070.05720.59
5.6.50.0100.07320.43
5.6.40.0030.08720.45
5.6.30.0130.07020.53
5.6.20.0030.05020.45
5.6.10.0200.05320.55
5.6.00.0130.07020.39
5.5.380.0030.08320.54
5.5.370.0130.04320.51
5.5.360.0000.05720.59
5.5.350.0070.08320.46
5.5.340.0170.07320.96
5.5.330.0100.07720.82
5.5.320.0030.07720.96
5.5.310.0030.08021.00
5.5.300.0070.06321.00
5.5.290.0030.06720.84
5.5.280.0100.04020.89
5.5.270.0030.06320.95
5.5.260.0170.07721.01
5.5.250.0200.04020.80
5.5.240.0030.07720.24
5.5.230.0100.08720.11
5.5.220.0070.07720.35
5.5.210.0130.07720.21
5.5.200.0000.08320.36
5.5.190.0070.08020.20
5.5.180.0100.08020.35
5.5.160.0130.03320.09
5.5.150.0070.06320.30
5.5.140.0130.07320.36
5.5.130.0070.08320.32
5.5.120.0170.06720.30
5.5.110.0130.07020.30
5.5.100.0070.08320.16
5.5.90.0100.07720.16
5.5.80.0100.05720.03
5.5.70.0030.07320.15
5.5.60.0170.08020.13
5.5.50.0000.05020.18
5.5.40.0100.05020.14
5.5.30.0070.07720.14
5.5.20.0030.09020.05
5.5.10.0030.08020.22
5.5.00.0070.04320.19
5.4.450.0170.07019.49
5.4.440.0030.08319.30
5.4.430.0100.06719.38
5.4.420.0170.07319.39
5.4.410.0030.08319.25
5.4.400.0100.05718.93
5.4.390.0100.07019.09
5.4.380.0130.06719.19
5.4.370.0030.07319.05
5.4.360.0130.07319.18
5.4.350.0170.07019.13
5.4.340.0130.05019.05
5.4.320.0130.06718.98
5.4.310.0070.06319.14
5.4.300.0070.06019.16
5.4.290.0000.08018.91
5.4.280.0070.04319.09
5.4.270.0100.06718.91
5.4.260.0100.04718.91
5.4.250.0130.07719.06
5.4.240.0130.08018.91
5.4.230.0100.06719.15
5.4.220.0100.05318.87
5.4.210.0070.06718.87
5.4.200.0070.07719.14
5.4.190.0130.06718.89
5.4.180.0030.05319.12
5.4.170.0030.04019.22
5.4.160.0170.06718.86
5.4.150.0130.06718.88
5.4.140.0030.07716.35
5.4.130.0100.06016.45
5.4.120.0070.06316.40
5.4.110.0030.07016.54
5.4.100.0030.05716.53
5.4.90.0070.07316.56
5.4.80.0100.06016.33
5.4.70.0070.07716.46
5.4.60.0100.04716.57
5.4.50.0100.07016.54
5.4.40.0000.08016.55
5.4.30.0000.04316.51
5.4.20.0070.07016.51
5.4.10.0070.07716.49
5.4.00.0130.07015.86

preferences:
90.65 ms | 401 KiB | 5 Q