This repository has been archived by the owner on Jul 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- review unit tests - namespaced controllers - example website
- Loading branch information
Showing
32 changed files
with
979 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
.idea | ||
composer.lock | ||
example | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
$appPath = dirname(__DIR__); | ||
return [ | ||
'appPath' => $appPath, | ||
'controllersPath' => $appPath . '/controllers', | ||
'viewsPath' => $appPath . '/views' | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace example\controllers; | ||
|
||
use Tebe\Pvc\Controller\BaseController; | ||
|
||
class ApiController extends BaseController | ||
{ | ||
public function indexAction() | ||
{ | ||
return [ | ||
'a' => 123, | ||
'b' => 456, | ||
'c' => 789 | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace example\controllers; | ||
|
||
use Tebe\Pvc\Controller\BaseController; | ||
|
||
class IndexController extends BaseController | ||
{ | ||
public function httpMethods() | ||
{ | ||
return [ | ||
'index' => ['GET'], | ||
'features' => ['GET'], | ||
'contact' => ['GET'], | ||
'post' => ['POST'] | ||
]; | ||
} | ||
|
||
public function indexAction() | ||
{ | ||
return $this->render('index'); | ||
} | ||
|
||
public function featuresAction() | ||
{ | ||
return $this->render('features'); | ||
} | ||
|
||
public function contactAction(int $a, int $b) | ||
{ | ||
return $this->render('contact'); | ||
} | ||
|
||
public function postAction() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace example\library; | ||
|
||
use Tebe\Pvc\Application; | ||
use Tebe\Pvc\Event\Event; | ||
use Tebe\Pvc\Event\EventHandler; | ||
use Tebe\Pvc\Exception\SystemException; | ||
|
||
class AuthLoggingEventHandler implements EventHandler | ||
{ | ||
|
||
protected $logFile; | ||
|
||
/** | ||
* AuthLoggingEventHandler constructor. | ||
* @param string $logFile | ||
*/ | ||
public function __construct(string $logFile) | ||
{ | ||
$this->logFile = $logFile; | ||
} | ||
|
||
/** | ||
* @param Event $event | ||
* @throws SystemException | ||
*/ | ||
public function handle(Event $event) : void | ||
{ | ||
|
||
$authData = $event->getInfo(); | ||
|
||
$fields = [ | ||
date('Y-m-d H:i:s'), | ||
json_encode(Application::instance()->getRequest()->getServerParams()), | ||
$event->getName(), | ||
$authData['user'], | ||
$authData['password'] | ||
]; | ||
|
||
error_log(implode('|', $fields) . "\n", 3, $this->logFile); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace example\library; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class HeaderMiddleware implements MiddlewareInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $header; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $value; | ||
|
||
/** | ||
* HeaderMiddleware constructor. | ||
* @param string $header | ||
* @param string $value | ||
*/ | ||
public function __construct(string $header, string $value) | ||
{ | ||
$this->header = $header; | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* @param ServerRequestInterface $request | ||
* @param RequestHandlerInterface $handler | ||
* @return ResponseInterface | ||
*/ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface | ||
{ | ||
$response = $handler->handle($request); | ||
return $response->withHeader($this->header, $this->value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace example\library; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class HtmlMiddleware implements MiddlewareInterface | ||
{ | ||
const MODE_PREPEND = 0; | ||
const MODE_APPEND = 1; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $string; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $mode; | ||
|
||
/** | ||
* HtmlMiddleware constructor. | ||
* @param string $string | ||
* @param int $mode | ||
*/ | ||
public function __construct(string $string, int $mode = 0) | ||
{ | ||
$this->string = $string; | ||
$this->mode = $mode; | ||
} | ||
|
||
/** | ||
* @param ServerRequestInterface $request | ||
* @param RequestHandlerInterface $handler | ||
* @return ResponseInterface | ||
*/ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$response = $handler->handle($request); | ||
|
||
// see: https://github.com/jshannon63/laravel-psr15-middleware/blob/master/src/exampleMiddleware.php | ||
$response->getBody()->rewind(); | ||
$body = $response->getBody(); | ||
$contents = $body->getContents(); | ||
|
||
if (static::MODE_PREPEND == $this->mode) { | ||
$contents = str_replace( | ||
'<body>', | ||
"<body>".$this->string, | ||
$contents | ||
); | ||
} | ||
if (static::MODE_APPEND == $this->mode) { | ||
$contents = str_replace( | ||
'</body>', | ||
$this->string.'</body>', | ||
$contents | ||
); | ||
} | ||
|
||
$body->rewind(); | ||
$body->write($contents); | ||
|
||
return $response->withBody($body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace example\library; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Zend\Diactoros\Response; | ||
|
||
class HttpBasicAuthMiddleware implements MiddlewareInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $users; | ||
|
||
/** | ||
* HttpBasicAuthMiddleware constructor. | ||
* @param array $users | ||
*/ | ||
public function __construct(array $users) | ||
{ | ||
$this->users = $users; | ||
} | ||
|
||
/** | ||
* @param ServerRequestInterface $request | ||
* @param RequestHandlerInterface $handler | ||
* @return ResponseInterface | ||
*/ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$login = $this->login($request); | ||
|
||
if (empty($login)) { | ||
$response = new Response('php://memory'); | ||
return $response->withStatus(401, 'Unauthorized')->withHeader('WWW-Authenticate', 'Basic realm="Test"'); | ||
} | ||
|
||
$response = $handler->handle($request); | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Check the user credentials and return the username or false. | ||
* | ||
* @param ServerRequestInterface $request | ||
* @return bool|mixed | ||
*/ | ||
private function login(ServerRequestInterface $request) | ||
{ | ||
//Check header | ||
$authorization = $this->parseHeader($request->getHeaderLine('Authorization')); | ||
|
||
if (!$authorization) { | ||
return false; | ||
} | ||
//Check the user | ||
if (!isset($this->users[$authorization['username']])) { | ||
return false; | ||
} | ||
if ($this->users[$authorization['username']] !== $authorization['password']) { | ||
return false; | ||
} | ||
return $authorization['username']; | ||
} | ||
|
||
/** | ||
* Parses the authorization header for a basic authentication. | ||
* | ||
* @param string $header | ||
* @return array|bool | ||
*/ | ||
private function parseHeader(string $header) | ||
{ | ||
if (strpos($header, 'Basic') !== 0) { | ||
return false; | ||
} | ||
$header = explode(':', base64_decode(substr($header, 6)), 2); | ||
return [ | ||
'username' => $header[0], | ||
'password' => isset($header[1]) ? $header[1] : null, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace example\library; | ||
|
||
use Tebe\Pvc\Application; | ||
use Tebe\Pvc\Event\Event; | ||
use Tebe\Pvc\EventHandler; | ||
|
||
class IpCheckEventHandler implements EventHandler | ||
{ | ||
|
||
protected $blockedIps; | ||
|
||
public function __construct($blockedIps) | ||
{ | ||
$this->blockedIps = $blockedIps; | ||
} | ||
|
||
public function handle(Event $event) : void | ||
{ | ||
$request = Application::instance()->getRequest(); | ||
$ipAdress = $request->getRemoteAddress(); | ||
|
||
if (in_array($ipAdress, $this->blockedIps)) { | ||
$event->cancel(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace example\library; | ||
|
||
use Tebe\Pvc\View\ViewExtension; | ||
use Tebe\Pvc\View\View; | ||
|
||
class MyViewExtension implements ViewExtension | ||
{ | ||
public function register(View $view) | ||
{ | ||
$view->registerHelper('hello', function (string $name = '') { | ||
return sprintf("Hallo %s", $name); | ||
}); | ||
$view->registerHelper('add', function (float ...$operands) { | ||
return array_sum($operands); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace example\library; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class ResponseTimeMiddleware implements MiddlewareInterface | ||
{ | ||
const HEADER = 'X-Response-Time'; | ||
|
||
/** | ||
* @param ServerRequestInterface $request | ||
* @param RequestHandlerInterface $handler | ||
* @return ResponseInterface | ||
*/ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$server = $request->getServerParams(); | ||
if (!isset($server['REQUEST_TIME_FLOAT'])) { | ||
$server['REQUEST_TIME_FLOAT'] = microtime(true); | ||
} | ||
$response = $handler->handle($request); | ||
$time = (microtime(true) - $server['REQUEST_TIME_FLOAT']) * 1000; | ||
return $response->withHeader(self::HEADER, sprintf('%2.3fms', $time)); | ||
} | ||
} |
Oops, something went wrong.