Skip to content

Commit d6a208a

Browse files
committed
2.3
1 parent 6181b66 commit d6a208a

File tree

9 files changed

+420
-271
lines changed

9 files changed

+420
-271
lines changed

README.md

Lines changed: 185 additions & 129 deletions
Large diffs are not rendered by default.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php namespace MiladRahimi\PHPRouter\Exceptions;
2+
3+
/**
4+
* Class BadController
5+
* BadController exception will be thrown when the controller is not a invokable procedure
6+
*
7+
* @package MiladRahimi\PHPRouter\Exceptions
8+
* @author Milad Rahimi <[email protected]>
9+
*/
10+
class BadController extends \Exception {
11+
// Not implemented yet!
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php namespace MiladRahimi\PHPRouter\Exceptions;
2+
3+
/**
4+
* Class BadMiddleware
5+
* BadMiddleware exception will be thrown when the middleware is not a invokable procedure
6+
*
7+
* @package MiladRahimi\PHPRouter\Exceptions
8+
* @author Milad Rahimi <[email protected]>
9+
*/
10+
class BadMiddleware extends \Exception {
11+
// Not implemented yet!
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php namespace MiladRahimi\PHPRouter\Exceptions;
2+
3+
/**
4+
* Class FileNotFound
5+
* FileNotFound exception will be thrown when the file does not exist
6+
*
7+
* @package MiladRahimi\PHPRouter\Exceptions
8+
* @author Milad Rahimi <[email protected]>
9+
*/
10+
class FileNotFound extends \Exception {
11+
// Not implemented yet!
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php namespace MiladRahimi\PHPRouter\Exceptions;
2+
3+
/**
4+
* Class HttpError
5+
* HttpError Exception would be thrown when HTTP error occurs. The exception
6+
* message would be HTTP error code like 404 for HTTP 404 Not Found error.
7+
*
8+
* @package MiladRahimi\PHPRouter\Exceptions
9+
* @author Milad Rahimi <[email protected]>
10+
*/
11+
class HttpError extends \Exception {
12+
// Not implemented yet!
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php namespace MiladRahimi\PHPRouter\Exceptions;
2+
3+
/**
4+
* Class InvalidArgumentException
5+
*
6+
* @package MiladRahimi\PHPRouter\Exceptions
7+
* @author Milad Rahimi <[email protected]>
8+
*/
9+
class InvalidArgumentException extends \InvalidArgumentException {
10+
// Not implemented yet!
11+
}

src/MiladRahimi/PHPRouter/Request.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace MiladRahimi\PHPRouter;
22

3+
use MiladRahimi\PHPRouter\Exceptions\InvalidArgumentException;
4+
35
/**
46
* Class Request
57
* Request Class is used to get user HTTP request information. Whole the project
Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php namespace MiladRahimi\PHPRouter;
22

3+
use MiladRahimi\PHPRouter\Exceptions\InvalidArgumentException;
4+
use MiladRahimi\PHPTemplate\Exceptions\FileNotFoundException;
5+
36
/**
47
* Class Response
58
* Response Class is used to help developer to response the matched route, it
69
* includes methods like redirect(), render(), etc.
710
*
811
* @package MiladRahimi\PHPRouter
9-
* @author Milad Rahimi <[email protected]>
12+
* @author Milad Rahimi <[email protected]>
1013
*/
11-
class Response
12-
{
14+
class Response {
1315

1416
/**
1517
* Singleton instance of the class
@@ -30,21 +32,21 @@ class Response
3032
*
3133
* @param Router $rooter
3234
*/
33-
private function __construct(Router $rooter)
34-
{
35-
if (!($rooter instanceof Router))
35+
private function __construct(Router $rooter) {
36+
if (!($rooter instanceof Router)) {
3637
throw new \InvalidArgumentException("Neatplex PHPRouter: Invalid object given instead of Router object");
38+
}
3739
$this->router = $rooter;
3840
}
3941

4042
/**
4143
* Get singleton instance of the class
4244
*
4345
* @param Router $router
46+
*
4447
* @return Request
4548
*/
46-
public static function getInstance(Router $router)
47-
{
49+
public static function getInstance(Router $router) {
4850
return isset(self::$instance) ? self::$instance : self::$instance = new Response($router);
4951
}
5052

@@ -53,10 +55,10 @@ public static function getInstance(Router $router)
5355
*
5456
* @param string $to
5557
*/
56-
public function redirect($to = "/")
57-
{
58-
if (!is_scalar($to))
58+
public function redirect($to = "/") {
59+
if (!is_scalar($to)) {
5960
throw new InvalidArgumentException("To must be a string value");
61+
}
6062
$to = $this->router->getBaseURI() . (is_string($to) ? $to : "");
6163
ob_start();
6264
ob_clean();
@@ -67,49 +69,50 @@ public function redirect($to = "/")
6769
/**
6870
* @return string
6971
*/
70-
public function __toString()
71-
{
72+
public function __toString() {
7273
return ob_get_contents();
7374
}
7475

7576
/**
7677
* Render (PHP) file
7778
*
7879
* @param string $file : PHP file to render
79-
* @throws InvalidArgumentException
80-
* @throws PHPRouterError
80+
*
81+
* @throws FileNotFoundException
8182
*/
82-
public function render($file)
83-
{
84-
if (!is_scalar($file))
83+
public function render($file) {
84+
if (!is_scalar($file)) {
8585
throw new InvalidArgumentException("File must be a string value");
86+
}
8687
if (file_exists($file)) {
88+
/** @noinspection PhpIncludeInspection */
8789
require $file;
8890
} else {
89-
throw new PHPRouterError("File does not exist");
91+
throw new FileNotFoundException();
9092
}
9193
}
9294

93-
9495
/**
9596
* Response cookies (write-only)
9697
*
9798
* @param string $name
9899
* @param string $value
99-
* @param int $expire
100+
* @param int $expire
100101
* @param string $path
101102
* @param string $domain
102-
* @param bool $secure
103-
* @param bool $httponly
103+
* @param bool $secure
104+
* @param bool $httponly
104105
*
105106
* @return array
106107
*/
107-
public function cookie($name, $value, $expire = 0, $path = null, $domain = null, $secure = false, $httponly = false)
108-
{
109-
if (!isset($name) || !is_scalar($name))
108+
public function cookie($name, $value, $expire = 0, $path = null, $domain = null, $secure = false,
109+
$httponly = false) {
110+
if (!isset($name) || !is_scalar($name)) {
110111
throw new InvalidArgumentException("Name must be a string value");
111-
if (!isset($value))
112+
}
113+
if (!isset($value)) {
112114
throw new InvalidArgumentException("Value must be set");
115+
}
113116
return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
114117
}
115118

@@ -118,8 +121,7 @@ public function cookie($name, $value, $expire = 0, $path = null, $domain = null,
118121
*
119122
* @return string
120123
*/
121-
public function contents()
122-
{
124+
public function contents() {
123125
return ob_get_contents();
124126
}
125127

@@ -128,21 +130,20 @@ public function contents()
128130
*
129131
* @param string|mixed $content
130132
*/
131-
public function publish($content)
132-
{
133-
if (!isset($content))
134-
throw new InvalidArgumentException("Content must be set");
133+
public function publish($content = null) {
135134
// Open output stream
136135
$fp = fopen("php://output", 'r+');
137136
// Raw content
138137
if (is_string($content) || is_numeric($content) || is_null($content)) {
139138
fputs($fp, $content);
140139
} // Object with __toString method
141-
else if (is_object($content) && method_exists($content, "__toString")) {
142-
fputs($fp, $content->__toString());
143-
} // Else
144140
else {
145-
fputs($fp, print_r($content, true));
141+
if (is_object($content) && method_exists($content, "__toString")) {
142+
fputs($fp, $content->__toString());
143+
} // Else
144+
else {
145+
fputs($fp, print_r($content, true));
146+
}
146147
}
147148
}
148149
}

0 commit comments

Comments
 (0)