Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.

Commit 0d68402

Browse files
author
Jefersson Nathan
committed
Merge pull request #48 from malukenho/feature/refactor
[WIP] Major refactor on PHPRouter
2 parents 274b75c + c1cc045 commit 0d68402

13 files changed

+302
-89
lines changed

composer.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
"symfony/yaml": "@dev"
2222
},
2323
"require-dev": {
24-
"phpunit/phpunit": "3.7.*"
24+
"phpunit/phpunit": "4.*",
25+
"squizlabs/php_codesniffer": "~1.5.4"
2526
},
2627
"autoload": {
27-
"psr-0": {
28-
"PHPRouter": "src/"
28+
"psr-4": {
29+
"PHPRouter\\": "src/PHPRouter/"
2930
}
3031
}
31-
}
32+
}

makefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
phpcs:
2+
@vendor/bin/phpcs --standard=PSR2 src tests
3+
4+
tests:
5+
@vendor/bin/phpunit
6+
7+
.PHONY: tests phpcs

phpunit.xml

-10
This file was deleted.

phpunit.xml.dist

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit bootstrap="./vendor/autoload.php" colors="true" syntaxCheck="true">
2+
<testsuites>
3+
<testsuite name="PHP Router">
4+
<directory>tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>

src/PHPRouter/Config.php

+42-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
11
<?php
2+
/**
3+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*
15+
* This software consists of voluntary contributions made by many individuals
16+
* and is licensed under the MIT license.
17+
*/
218
namespace PHPRouter;
319

20+
use InvalidArgumentException;
421
use Symfony\Component\Yaml\Yaml;
522

6-
class Config
23+
/**
24+
* Auxiliary Config class, to parse a Yaml file.
25+
*
26+
* @author Jefersson Nathan <[email protected]>
27+
* @package PHPRouter
28+
*/
29+
final class Config
730
{
31+
/**
32+
* Avoid instantiation.
33+
*/
34+
private function __construct()
35+
{
36+
}
37+
38+
/**
39+
* @param string $yamlFile file location.
40+
* @throws InvalidArgumentException
41+
*
42+
* @return mixed[]
43+
*/
844
public static function loadFromFile($yamlFile)
945
{
10-
try {
11-
$value = Yaml::parse(file_get_contents($yamlFile));
12-
} catch (\Exception $e) {
13-
echo 'Message %s'.$e->getMessage();
46+
if (! is_file($yamlFile)) {
47+
throw new InvalidArgumentException(sprintf('The file %s not exists!', $yamlFile));
1448
}
15-
return $value;
49+
50+
return Yaml::parse(file_get_contents($yamlFile));
1651
}
17-
}
52+
}

src/PHPRouter/Route.php

+42-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
<?php
2+
/**
3+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*
15+
* This software consists of voluntary contributions made by many individuals
16+
* and is licensed under the MIT license.
17+
*/
218
namespace PHPRouter;
319

420
class Route
@@ -7,122 +23,124 @@ class Route
723
* URL of this Route
824
* @var string
925
*/
10-
private $_url;
26+
private $url;
1127

1228
/**
1329
* Accepted HTTP methods for this route
1430
* @var array
1531
*/
16-
private $_methods = array('GET', 'POST', 'PUT', 'DELETE');
32+
private $methods = array('GET', 'POST', 'PUT', 'DELETE');
1733

1834
/**
1935
* Target for this route, can be anything.
2036
* @var mixed
2137
*/
22-
private $_target;
38+
private $target;
2339

2440
/**
2541
* The name of this route, used for reversed routing
2642
* @var string
2743
*/
28-
private $_name;
44+
private $name;
2945

3046
/**
3147
* Custom parameter filters for this route
3248
* @var array
3349
*/
34-
private $_filters = array();
50+
private $filters = array();
3551

3652
/**
3753
* Array containing parameters passed through request URL
3854
* @var array
3955
*/
40-
private $_parameters = array();
56+
private $parameters = array();
4157

4258
/**
4359
* @param $resource
4460
* @param array $config
4561
*/
4662
public function __construct($resource, array $config)
4763
{
48-
$this->_url = $resource;
64+
$this->url = $resource;
4965
$this->_config = $config;
50-
$this->_methods = isset($config['methods']) ? $config['methods'] : array();
51-
$this->_target = isset($config['target']) ? $config['target'] : null;
66+
$this->methods = isset($config['methods']) ? $config['methods'] : array();
67+
$this->target = isset($config['target']) ? $config['target'] : null;
5268
}
5369

5470
public function getUrl()
5571
{
56-
return $this->_url;
72+
return $this->url;
5773
}
5874

5975
public function setUrl($url)
6076
{
6177
$url = (string) $url;
6278

6379
// make sure that the URL is suffixed with a forward slash
64-
if(substr($url,-1) !== '/') $url .= '/';
80+
if (substr($url, -1) !== '/') {
81+
$url .= '/';
82+
}
6583

66-
$this->_url = $url;
84+
$this->url = $url;
6785
}
6886

6987
public function getTarget()
7088
{
71-
return $this->_target;
89+
return $this->target;
7290
}
7391

7492
public function setTarget($target)
7593
{
76-
$this->_target = $target;
94+
$this->target = $target;
7795
}
7896

7997
public function getMethods()
8098
{
81-
return $this->_methods;
99+
return $this->methods;
82100
}
83101

84102
public function setMethods(array $methods)
85103
{
86-
$this->_methods = $methods;
104+
$this->methods = $methods;
87105
}
88106

89107
public function getName()
90108
{
91-
return $this->_name;
109+
return $this->name;
92110
}
93111

94112
public function setName($name)
95113
{
96-
$this->_name = (string) $name;
114+
$this->name = (string) $name;
97115
}
98116

99117
public function setFilters(array $filters)
100118
{
101-
$this->_filters = $filters;
119+
$this->filters = $filters;
102120
}
103121

104122
public function getRegex()
105123
{
106-
return preg_replace_callback("/:(\w+)/", array(&$this, 'substituteFilter'), $this->_url);
124+
return preg_replace_callback("/:(\w+)/", array(&$this, 'substituteFilter'), $this->url);
107125
}
108126

109127
private function substituteFilter($matches)
110128
{
111-
if (isset($matches[1]) && isset($this->_filters[$matches[1]])) {
112-
return $this->_filters[$matches[1]];
129+
if (isset($matches[1]) && isset($this->filters[$matches[1]])) {
130+
return $this->filters[$matches[1]];
113131
}
114132

115133
return "([\w-%]+)";
116134
}
117135

118136
public function getParameters()
119137
{
120-
return $this->_parameters;
138+
return $this->parameters;
121139
}
122140

123141
public function setParameters(array $parameters)
124142
{
125-
$this->_parameters = $parameters;
143+
$this->parameters = $parameters;
126144
}
127145

128146
public function dispatch()

src/PHPRouter/RouteCollection.php

+37-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,52 @@
11
<?php
2+
/**
3+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*
15+
* This software consists of voluntary contributions made by many individuals
16+
* and is licensed under the MIT license.
17+
*/
218
namespace PHPRouter;
319

20+
/**
21+
* @author Jefersson Nathan <[email protected]>
22+
*
23+
* @package PHPRouter
24+
*/
425
class RouteCollection extends \SplObjectStorage
526
{
27+
/**
28+
* {@inheritDoc}
29+
*
30+
* @param Route $attachObject
31+
*/
32+
public function attach(Route $attachObject)
33+
{
34+
parent::attach($attachObject);
35+
}
36+
637
/**
738
* Fetch all routers stored on this collection of router
839
* and return it.
940
*
10-
* @return array
41+
* @return Route[]
1142
*/
1243
public function all()
1344
{
14-
$_tmp = array();
15-
foreach($this as $objectValue)
16-
{
17-
$_tmp[] = $objectValue;
45+
$temp = [];
46+
foreach ($this as $router) {
47+
$temp[] = $router;
1848
}
19-
return $_tmp;
49+
50+
return $temp;
2051
}
2152
}

0 commit comments

Comments
 (0)