Skip to content
This repository has been archived by the owner on Jul 31, 2022. It is now read-only.

Commit

Permalink
Integrated an example website
Browse files Browse the repository at this point in the history
  • Loading branch information
tbreuss committed Oct 18, 2018
1 parent a07501a commit ef23074
Show file tree
Hide file tree
Showing 24 changed files with 961 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.idea
composer.lock
example
vendor
8 changes: 8 additions & 0 deletions example/config/main.php
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'
];
17 changes: 17 additions & 0 deletions example/controllers/ApiController.php
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
];
}
}
37 changes: 37 additions & 0 deletions example/controllers/IndexController.php
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()
{
}
}
43 changes: 43 additions & 0 deletions example/library/AuthLoggingEventHandler.php
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);
}
}
43 changes: 43 additions & 0 deletions example/library/HeaderMiddleware.php
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);
}
}
70 changes: 70 additions & 0 deletions example/library/HtmlMiddleware.php
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);
}
}
87 changes: 87 additions & 0 deletions example/library/HttpBasicAuthMiddleware.php
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,
];
}
}
28 changes: 28 additions & 0 deletions example/library/IpCheckEventHandler.php
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();
}
}
}
19 changes: 19 additions & 0 deletions example/library/MyViewExtension.php
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);
});
}
}
29 changes: 29 additions & 0 deletions example/library/ResponseTimeMiddleware.php
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));
}
}
Loading

0 comments on commit ef23074

Please sign in to comment.