Skip to content

Commit ddf4699

Browse files
committed
2.2
1 parent 59294de commit ddf4699

File tree

8 files changed

+71
-53
lines changed

8 files changed

+71
-53
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,23 @@ If you are not familiar with Composer, The article
3131
can be useful.
3232
After installing Composer, go to your project directory and run following command there:
3333
```
34-
php composer.phar require miladrahimi/phprouter
34+
composer require miladrahimi/phprouter
3535
```
3636
Or if you have `composer.json` file already in your application,
3737
you may add this package to your application requirements
3838
and update your dependencies:
3939
```
4040
"require": {
41-
"miladrahimi/phprouter": "dev-master"
41+
"miladrahimi/phprouter": "~2.2"
4242
}
4343
```
4444
```
45-
php composer.phar update
45+
composer update
4646
```
47+
#### Using PHPVendor
48+
If you don't use Composer you may use [PHPVendor](https://github.com/miladrahimi/phpvendor).
49+
Copy `src` directory content in your application vendor directory,
50+
then include the `phpvendor.php` in your application.
4751
#### Manually
4852
You can use your own autoloader as long as it follows [PSR-0](http://www.php-fig.org/psr/psr-0) or
4953
[PSR-4](http://www.php-fig.org/psr/psr-4) standards.

composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
"php": ">=5.3.0"
2222
},
2323
"autoload": {
24-
"psr-4": {
25-
"MiladRahimi\\": "src/MiladRahimi/"
26-
}
24+
"psr-4": {"MiladRahimi\\": "src/MiladRahimi/"}
2725
}
2826
}

src/MiladRahimi/PHPRouter/HttpError.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
/**
44
* Class HttpError
5-
*
65
* HttpError Exception would be thrown when HTTP error occurs. The exception
76
* message would be HTTP error code like 404 for HTTP 404 Not Found error.
87
*
98
* @package MiladRahimi\PHPRouter
10-
*
119
* @author Milad Rahimi <[email protected]>
1210
*/
1311
class HttpError extends \Exception
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php namespace MiladRahimi\PHPRouter;
2+
3+
/**
4+
* Class InvalidArgumentException
5+
* Specified InvalidArgumentException exception for the package
6+
*
7+
* @package MiladRahimi\PHPConfig
8+
* @author Milad Rahimi <[email protected]>
9+
*/
10+
class InvalidArgumentException extends \InvalidArgumentException
11+
{
12+
// Not implemented yet!
13+
}

src/MiladRahimi/PHPRouter/PHPRouterError.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
/**
44
* Class PHPRouterError
5-
*
6-
* PHPRouterError Exception is the main error exception for whole the package.
5+
* The main package exception
76
*
87
* @package MiladRahimi\PHPRouter
9-
*
108
* @author Milad Rahimi <[email protected]>
119
*/
1210
class PHPRouterError extends \Exception

src/MiladRahimi/PHPRouter/Request.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
/**
44
* Class Request
5-
*
65
* Request Class is used to get user HTTP request information. Whole the project
76
* would have only one instance (singleton pattern) an that is an optional
87
* parameter for controller method, function to closure.
98
*
109
* @package MiladRahimi\PHPRouter
11-
*
1210
* @author Milad Rahimi <[email protected]>
1311
*/
1412
class Request
@@ -120,12 +118,12 @@ class Request
120118
private $get;
121119

122120
/**
123-
* @param Router $router
121+
* Constructor
122+
*
123+
* @param Router $router : Router object
124124
*/
125125
private function __construct(Router $router)
126126
{
127-
if (!($router instanceof Router))
128-
throw new \InvalidArgumentException("Neatplex PHPRouter: Invalid object given instead of Router object");
129127
$this->router = $router;
130128
$u = $this->uri = urldecode($_SERVER["REQUEST_URI"]);
131129
$q = $this->query_string = $_SERVER["QUERY_STRING"];
@@ -150,10 +148,9 @@ private function __construct(Router $router)
150148
* Get singleton instance of the class
151149
*
152150
* @param Router $router
153-
*
154151
* @return Request
155152
*/
156-
public static function getInstance(Router $router = null)
153+
public static function getInstance(Router $router)
157154
{
158155
return isset(self::$instance) ? self::$instance : self::$instance = new Request($router);
159156
}
@@ -197,16 +194,16 @@ public function get($key = null)
197194
/**
198195
* Return the user HTTP request POST
199196
*
200-
* @param string $key
201-
*
197+
* @param string $name
202198
* @return array
203199
*/
204-
public function post($key = null)
200+
public function post($name = null)
205201
{
206-
if (is_null($key))
202+
if (is_null($name))
207203
return $this->post;
208-
$post = $this->post;
209-
return isset($post[$key]) ? $post[$key] : null;
204+
if(!is_scalar($name))
205+
throw new InvalidArgumentException("Name must be a string value");
206+
return isset($this->post[$name]) ? $this->post[$name] : null;
210207
}
211208

212209
/**
@@ -285,13 +282,14 @@ public function getPage()
285282
* Request cookies (Read-only)
286283
*
287284
* @param string $name
288-
*
289285
* @return array
290286
*/
291287
public function cookie($name = null)
292288
{
293289
if (is_null($name))
294290
return $_COOKIE;
291+
if(!is_scalar($name))
292+
throw new InvalidArgumentException("Name must be a string value");
295293
return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;
296294
}
297295

src/MiladRahimi/PHPRouter/Response.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
/**
44
* Class Response
5-
*
65
* Response Class is used to help developer to response the matched route, it
76
* includes methods like redirect(), render(), etc.
87
*
98
* @package MiladRahimi\PHPRouter
10-
*
119
* @author Milad Rahimi <[email protected]>
1210
*/
1311
class Response
@@ -43,10 +41,9 @@ private function __construct(Router $rooter)
4341
* Get singleton instance of the class
4442
*
4543
* @param Router $router
46-
*
4744
* @return Request
4845
*/
49-
public static function getInstance(Router $router = null)
46+
public static function getInstance(Router $router)
5047
{
5148
return isset(self::$instance) ? self::$instance : self::$instance = new Response($router);
5249
}
@@ -58,6 +55,8 @@ public static function getInstance(Router $router = null)
5855
*/
5956
public function redirect($to = "/")
6057
{
58+
if (!is_scalar($to))
59+
throw new InvalidArgumentException("To must be a string value");
6160
$to = $this->router->getBaseURI() . (is_string($to) ? $to : "");
6261
ob_start();
6362
ob_clean();
@@ -76,12 +75,19 @@ public function __toString()
7675
/**
7776
* Render (PHP) file
7877
*
79-
* @param string $file
78+
* @param string $file : PHP file to render
79+
* @throws InvalidArgumentException
80+
* @throws PHPRouterError
8081
*/
81-
public function render($file = null)
82+
public function render($file)
8283
{
83-
if (file_exists($file))
84-
include $file;
84+
if (!is_scalar($file))
85+
throw new InvalidArgumentException("File must be a string value");
86+
if (file_exists($file)) {
87+
require $file;
88+
} else {
89+
throw new PHPRouterError("File does not exist");
90+
}
8591
}
8692

8793

@@ -100,8 +106,10 @@ public function render($file = null)
100106
*/
101107
public function cookie($name, $value, $expire = 0, $path = null, $domain = null, $secure = false, $httponly = false)
102108
{
103-
if (empty($name) || empty($value))
104-
throw new \InvalidArgumentException("cookie() method catches empty name or value parameter");
109+
if (!isset($name) || !is_scalar($name))
110+
throw new InvalidArgumentException("Name must be a string value");
111+
if (!isset($value))
112+
throw new InvalidArgumentException("Value must be set");
105113
return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
106114
}
107115

@@ -122,6 +130,8 @@ public function contents()
122130
*/
123131
public function publish($content)
124132
{
133+
if (!isset($content))
134+
throw new InvalidArgumentException("Content must be set");
125135
// Open output stream
126136
$fp = fopen("php://output", 'r+');
127137
// Raw content

src/MiladRahimi/PHPRouter/Router.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
/**
44
* Class Router
5-
*
65
* Router class is the main class which developers must interactive with to
76
* dispatch all website routes.
87
*
98
* @package MiladRahimi\PHPRouter
10-
*
119
* @author Milad Rahimi <[email protected]>
1210
*/
1311
class Router
@@ -92,15 +90,16 @@ class Router
9290
private $response;
9391

9492
/**
95-
* @param string $base_uri
93+
* Constructor
9694
*
95+
* @param string $base_uri
9796
* @throw \InvalidArgumentException
9897
*/
9998
public function __construct($base_uri = "")
10099
{
101100
// Set Base URI
102101
if (!is_string($base_uri))
103-
throw new \InvalidArgumentException('Neatplex PHPRouter: $base_uri must be a string value');
102+
throw new InvalidArgumentException("Base URI must be a string value");
104103
$this->base_uri = $base_uri;
105104
// New Request
106105
$this->request = Request::getInstance($this);
@@ -120,11 +119,14 @@ public function __toString()
120119
*
121120
* @param array|callable|string $options
122121
* @param callable $body
123-
*
124122
* @throws PHPRouterError
125123
*/
126124
public function group($options, $body)
127125
{
126+
if (!isset($options))
127+
throw new InvalidArgumentException("Options must be set");
128+
if (!isset($body))
129+
throw new InvalidArgumentException("Body must be set");
128130
if (is_array($options)) {
129131
if (isset($options["middleware"]))
130132
if (!is_array($options["middleware"]))
@@ -235,7 +237,6 @@ private function checkBaseURI()
235237
* Convert route to regex pattern and extract the parameters
236238
*
237239
* @param string $route Route ro compile
238-
*
239240
* @return string Pattern
240241
*/
241242
private function convertToRegex($route)
@@ -247,9 +248,7 @@ private function convertToRegex($route)
247248
* Check whether the set domain is correct or not
248249
*
249250
* @param string $domain Set Domain
250-
*
251251
* @return array arguments
252-
*
253252
* @throws HttpError
254253
*/
255254
private function checkDomain($domain)
@@ -267,7 +266,6 @@ private function checkDomain($domain)
267266
*
268267
* @param callable $function
269268
* @param array $arguments
270-
*
271269
* @return array
272270
*/
273271
private function arrangeFuncArgs($function, $arguments)
@@ -293,7 +291,6 @@ function (\ReflectionParameter $param) use ($arguments) {
293291
* @param object $class
294292
* @param callable $method
295293
* @param array $arguments
296-
*
297294
* @return array
298295
*/
299296
private function arrangeMethodArgs($class, $method, $arguments)
@@ -344,11 +341,16 @@ public function get($routes = null, $controller = null, $middleware = null)
344341
* @param string|array $routes
345342
* @param string|callable $controller
346343
* @param callable|string|null $middleware
347-
*
348344
* @throws PHPRouterError
349345
*/
350346
public function map($methods, $routes, $controller, $middleware = null)
351347
{
348+
if (!isset($methods))
349+
throw new InvalidArgumentException("Methods must be set");
350+
if (!isset($routes))
351+
throw new InvalidArgumentException("Routes must be set");
352+
if (!isset($controller))
353+
throw new InvalidArgumentException("Controllers must be set");
352354
if (!is_array($methods))
353355
$methods = array($methods);
354356
if (!is_array($routes))
@@ -377,7 +379,6 @@ public function map($methods, $routes, $controller, $middleware = null)
377379
* Escape undesired regex from the given content
378380
*
379381
* @param string $content
380-
*
381382
* @return string
382383
*/
383384
private function safeRegex($content)
@@ -416,15 +417,14 @@ public function post($routes = null, $controller = null, $middleware = null)
416417
*
417418
* @param string $parameter_name Desired parameter for changing it's regex pattern
418419
* @param string $regex Desired regex pattern for related parameter
419-
*
420420
* @throw \InvalidArgumentException
421421
*/
422422
public function define($parameter_name, $regex)
423423
{
424-
if (!is_string($parameter_name))
425-
throw new \InvalidArgumentException('Neatplex PHPRouter: $parameter_name must be a string value');
426-
if (!is_string($regex))
427-
throw new \InvalidArgumentException('Neatplex PHPRouter: $regex must be a string value');
424+
if (!isset($parameter_name) || !is_string($parameter_name))
425+
throw new \InvalidArgumentException("Parameter name must be a string value");
426+
if (!isset($regex) || !is_string($regex))
427+
throw new \InvalidArgumentException("Regex must be a string value");
428428
$this->parameters[$parameter_name] = $this->safeRegex($regex);
429429
}
430430

@@ -440,7 +440,6 @@ public function getBaseURI()
440440
* Return the regex pattern of given parameter
441441
*
442442
* @param string $name Parameter Name
443-
*
444443
* @return string Pattern
445444
*/
446445
private function regexParameter($name)

0 commit comments

Comments
 (0)