diff --git a/composer.json b/composer.json index 1c5103cba6c173af5e6b9a465c6eb7bb4347fa4f..8aaa506afac386ef3105d348de7589bb8bc35787 100644 --- a/composer.json +++ b/composer.json @@ -26,19 +26,15 @@ "autoload": { "classmap": [ - "src/", - "example/" + "src/" ] }, "scripts": { - "example" : [ - "php -S localhost:8000 -t example/" - ], "phpcs": [ - "phpcs --standard=PSR2 --colors --ignore=example/cache/ src/ example/ tests/" + "phpcs --standard=PSR2 --colors src/ tests/" ], "phpcs-fix": [ - "phpcbf --standard=PSR2 --ignore=example/cache/ src/ example/ tests/" + "phpcbf --standard=PSR2 src/ tests/" ], "test": [ "vendor/bin/phpunit --coverage-text --bootstrap ./tests/bootstrap.php --testdox --color -v tests/" diff --git a/example/ActIndex.php b/example/ActIndex.php deleted file mode 100644 index e626c496f39498a61e7741bf1ee499839af96f12..0000000000000000000000000000000000000000 --- a/example/ActIndex.php +++ /dev/null @@ -1,34 +0,0 @@ -<?php - -namespace ExampleApp; - -use Lipid\Action; -use Lipid\Request; -use Lipid\Request\RqGET; -use Lipid\Response; - -/** - * Start page of example app. - * - * @author Alexandr Gorlov <a.gorlov@gmail.com> - */ -final class ActIndex implements Action -{ - private $rqGet; - - public function __construct(Request $req = null) - { - $this->rqGet = $req ?? new RqGET(); - } - - public function handle(Response $resp): Response - { - $test = $this->rqGet->param('test') ?? 'nope'; - - return $resp->withBody( - "Hello, World 2!<br>" . - '<a href="/login">login</a><br>' . - '$_GET[test]=' . htmlentities($test) - ); - } -} diff --git a/example/ActIndexTwig.php b/example/ActIndexTwig.php deleted file mode 100644 index 8632d2c8d5aecf2e2320d225b9be876d591752b1..0000000000000000000000000000000000000000 --- a/example/ActIndexTwig.php +++ /dev/null @@ -1,53 +0,0 @@ -<?php - -namespace ExampleApp; - -use Lipid\Action; -use Lipid\Request; -use Lipid\Config\CfgFile; -use Lipid\Request\RqGET; -use Lipid\Request\RqENV; -use Lipid\Response; -use Lipid\Tpl; -use ExampleApp\AppTwig; - -/** - * Start page of example app. - * - * @author Alexandr Gorlov <a.gorlov@gmail.com> - */ -final class ActIndexTwig implements Action -{ - private $rqGet; - private $rqEnv; - private $tpl; - private $cfg; - - public function __construct( - Request $reqGet = null, - Request $env = null, - Tpl $tpl = null, - Config $cfgFile = null - ) { - $this->rqGet = $reqGet ?? new RqGET(); - $this->rqEnv = $env ?? new RqENV(); - $this->tpl = $tpl ?? new AppTwig('index.twig'); - $this->cfg = $cfgFile ?? new Cfg(); - } - - public function handle(Response $resp): Response - { - $test = $this->rqGet->param('test') ?? 'nope'; - - - return $resp->withBody( - $this->tpl->render([ - 'date' => new \DateTime('now'), - 'test' => $test, - 'USER' => $this->rqEnv->param('USER'), - 'cfgTEST' => $this->cfg->param('TEST'), - 'cfgDBUSER' => $this->cfg->param('dbuser'), - ]) - ); - } -} diff --git a/example/ActLk.php b/example/ActLk.php deleted file mode 100644 index ef10bd8f0344131a2f06dffc7ee207fda4c79be1..0000000000000000000000000000000000000000 --- a/example/ActLk.php +++ /dev/null @@ -1,49 +0,0 @@ -<?php - -namespace ExampleApp; - -use Lipid\Action; -use Lipid\Request; -use Lipid\Response; -use Lipid\Session; -use Lipid\Session\AppSession; -use Lipid\AccessDeniedException; -use Lipid\Config; -use Lipid\Config\CfgParam; -//use Lipid\AccessDeniedException; -use PDO; - -final class ActLk implements Action -{ - private $sess; - private $config; - - public function __construct( - Session $sess = null, - Config $config = null, - PDO $pdo = null - ) { - $this->sess = $sess ?? new AppSession(); - $this->config = $config ?? new AppConfig(); - $this->cfgDbname = new CfgParam('dbname', $this->config); - $this->pdo = $pdo ?? new AppPDO(); - } - - public function handle(Response $resp): Response - { - if (! $this->sess->exists('login')) { - throw new AccessDeniedException('Only Authorized Access'); - } - - $this->pdo->quote("Test string!"); - - // login and password are ok - - return $resp->withBody( - "Hello: " . $this->sess->get('login') . "<br>" . - '<a href="/logout">logout</a><br>' . - 'Config var dbname=' . $this->config->param('dbname') . "<br>" . - 'Config var dbname=' . $this->cfgDbname->val() - ); - } -} diff --git a/example/ActLogin.php b/example/ActLogin.php deleted file mode 100644 index 4ea2b74405eef437322010813126062a785dd20b..0000000000000000000000000000000000000000 --- a/example/ActLogin.php +++ /dev/null @@ -1,37 +0,0 @@ -<?php - -namespace ExampleApp; - -use Lipid\Action; -use Lipid\Action\ActRedirect; -use Lipid\Request; -use Lipid\Request\RqGET; -use Lipid\Response; -use Lipid\Session; -use Lipid\Session\AppSession; - -final class ActLogin implements Action -{ - private $session; - private $redirect; - - public function __construct( - Session $sess = null, - Action $redirect = null - ) { - $this->session = $sess ?? new AppSession; - $this->redirect = $redirect ?? new ActRedirect('/lk'); - } - - public function handle(Response $resp): Response - { - if (true) { // login and password ok - $this->session->set('login', 'user1'); - return $this->redirect->handle($resp); - } else { - return $this->response->withBody( - "Bad login or password." - ); - } - } -} diff --git a/example/ActLogout.php b/example/ActLogout.php deleted file mode 100644 index e12f92ab246cc96a5db969e719fadd0b9fc484d1..0000000000000000000000000000000000000000 --- a/example/ActLogout.php +++ /dev/null @@ -1,37 +0,0 @@ -<?php - -namespace ExampleApp; - -use Lipid\Action; -use Lipid\Action\ActRedirect; -use Lipid\Request; -use Lipid\Request\RqGET; -use Lipid\Response; -use Lipid\Session; -use Lipid\Session\AppSession; - -final class ActLogout implements Action -{ - private $session; - private $redirect; - - public function __construct( - Session $sess = null, - Action $redirect = null - ) { - $this->session = $sess ?? new AppSession; - $this->redirect = $redirect ?? new ActRedirect('/'); - } - - public function handle(Response $resp): Response - { - if ($this->session->exists('login')) { // login and password ok - $this->session->unset('login'); - return $this->redirect->handle($resp); - } - - return $resp->withBody( - "You are not logged in." - ); - } -} diff --git a/example/AppPDO.php b/example/AppPDO.php deleted file mode 100644 index 55604a62575aac7475431df95611d6761f051279..0000000000000000000000000000000000000000 --- a/example/AppPDO.php +++ /dev/null @@ -1,16 +0,0 @@ -<?php - -namespace ExampleApp; - -use Lipid\BasePDO; -use Lipid\Config; - -final class AppPDO extends BasePDO -{ - public function __construct(Config $config = null) - { - parent::__construct( - $config ?? new AppConfig() - ); - } -} diff --git a/example/AppTwig.php b/example/AppTwig.php deleted file mode 100644 index a0b6fb6f923a3567b56a5a1463086d6f6c1ac74a..0000000000000000000000000000000000000000 --- a/example/AppTwig.php +++ /dev/null @@ -1,54 +0,0 @@ -<?php - -namespace ExampleApp; - -use Exception; -use Lipid\Tpl; -use Lipid\Tpl\Twig; -use Lipid\Request\RqENV; - -final class AppTwig implements Tpl -{ - - private $tpl; - private $env; - private $tplName; - - public function __construct(string $tplName, Tpl $tpl = null, Request $env = null) - { - $this->tplName = $tplName; - $this->tpl = $tpl; - $this->env = $env ?? new RqENV(); - } - - private function tpl(): Tpl - { - if (! is_null($this->tpl)) { - return $this->tpl; - } - - try { - $debug = (boolean) $this->env->param('APP_DEBUG'); - } catch (Exception $e) { - $debug = true; - } - - return new Twig( - $this->tplName, - new \Twig\Environment( - new \Twig\Loader\FilesystemLoader( - __DIR__ . '/tpl' - ), - [ - 'cache' => __DIR__ . '/cache', - 'debug' => $debug - ] - ) - ); - } - - public function render(array $data = null): string - { - return $this->tpl()->render($data); - } -} diff --git a/example/README.md b/example/README.md deleted file mode 100644 index ec5de187dc1e106d36be47d6d948eb628ff79f6f..0000000000000000000000000000000000000000 --- a/example/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Example appilcation - -With template engine Twig diff --git a/example/config.php b/example/config.php deleted file mode 100644 index 38fbda6e6ad60423e3bac4cca3121e1e15586115..0000000000000000000000000000000000000000 --- a/example/config.php +++ /dev/null @@ -1,5 +0,0 @@ -<?php - -return [ - 'TEST' => 123 -]; diff --git a/example/creds.php b/example/creds.php deleted file mode 100644 index 57c66f39a01787186b15c185c4d8be69e9c95c69..0000000000000000000000000000000000000000 --- a/example/creds.php +++ /dev/null @@ -1,8 +0,0 @@ -<?php - -return [ - 'dbhost' => '127.0.0.1', - 'dbname' => 'mydb', - 'dbuser' => 'myuser', - 'dbpass' => 'mypass', -]; diff --git a/example/index.php b/example/index.php deleted file mode 100644 index 7075e58182d13749c8f371b88101b82b07aa9ad6..0000000000000000000000000000000000000000 --- a/example/index.php +++ /dev/null @@ -1,26 +0,0 @@ -<?php - -/** - * Object MVC Example app - * - * @author Alexandr Gorlov <a.gorlov@gmail.com> - */ - -require_once '../vendor/autoload.php'; - -use Lipid\App\ApplicationStd; -use Lipid\Response\RespStd; -use ExampleApp\ActIndexTwig; -use ExampleApp\ActLogin; -use ExampleApp\ActLogout; -use ExampleApp\ActLk; - -(new ApplicationStd( - [ - '/' => new ActIndexTwig(), - '/login' => new ActLogin(), - '/logout' => new ActLogout(), - '/lk' => new ActLk(), - ], - new RespStd -))->start(); diff --git a/example/tpl/index.twig b/example/tpl/index.twig deleted file mode 100644 index 2e496326d9740d43f6299ab54755d690dbcf01ca..0000000000000000000000000000000000000000 --- a/example/tpl/index.twig +++ /dev/null @@ -1,10 +0,0 @@ -{# First Template #} -<h1>Hello, World!</h1> - -<p>Now is: {{date.format('d.m.Y H:i:s')}}</p> -<p>_GET['test']: {{test}}</p> -<p>_ENV['USER']: {{USER}}</p> -<p>config.php:TEST={{cfgTEST}}</p> - - -<p>You could <a href="/login">login</a>.</p> diff --git a/phpunit.xml b/phpunit.xml index d57b89b649631eec1daef73bcf0f121fc8ce998d..1fc01660ada987ad503d7ca6a60397b91de0458e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -2,7 +2,7 @@ <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd" - ackupGlobals="false" + backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" diff --git a/src/Action/ActRouted.php b/src/Action/ActRouted.php index 470842498617b66c4355c9622df9b1668e5d94ff..7f2eb9db3c8008e23b7e14b405a46777e5ece636 100644 --- a/src/Action/ActRouted.php +++ b/src/Action/ActRouted.php @@ -55,11 +55,10 @@ final class ActRouted implements Action } $path = parse_url($requestUri)['path']; - if (! (is_string($path) && strlen($path) > 0) ) { - throw new Exception("Unrecognized path in REQUEST_URI"); + if (! (is_string($path) && strlen($path) > 0)) { + throw new Exception("Unrecognized path in REQUEST_URI=$requestUri"); } - $path = '/' . rtrim(substr($path, 1), '/'); if (! array_key_exists($path, $this->actionMap)) { diff --git a/src/Config/Cfg.php b/src/Config/Cfg.php index fe0cc6014de3e985cd96d8cfd97940d3b6203edc..e430110866b7fb6e1c55bd48198a173d700169c0 100644 --- a/src/Config/Cfg.php +++ b/src/Config/Cfg.php @@ -67,7 +67,7 @@ final class Cfg implements Config public function param($name) { try { - return $this->creds->call($this)->param($name); + return $this->creds->call($this)->param($name); } catch (Exception $e) { return $this->cfg->call($this)->param($name); } diff --git a/tests/ActRoutedTest.php b/tests/ActRoutedTest.php index ff2b9148ec1ebbad0e86052901985f98005271e6..37c02b9705c1f0d5804eb5797882c9878c935278 100644 --- a/tests/ActRoutedTest.php +++ b/tests/ActRoutedTest.php @@ -9,6 +9,7 @@ use Lipid\Action; use Lipid\Request; use Lipid\Response; use Lipid\NotFoundException; +use Exception; /** * RqGET Test @@ -65,6 +66,8 @@ final class ActRoutedTest extends TestCase public function testRootRoot(): void { + $this->expectException(Exception::class); + $request = new class() implements Request { public function param($param) @@ -75,7 +78,7 @@ final class ActRoutedTest extends TestCase (new ActRouted($request, [ '/' => $this->action ]))->handle($this->response); - $this->assertTrue($this->action->isInvoked); + // $this->assertTrue($this->action->isInvoked); } public function testPage(): void diff --git a/tests/CfgArrTest.php b/tests/CfgArrTest.php index 8851479238471a6b8c175cbb50854901f85fa96b..7ce35dfd55ea71e633481d98c2276ef637b801a0 100644 --- a/tests/CfgArrTest.php +++ b/tests/CfgArrTest.php @@ -27,5 +27,5 @@ final class CfgArrTest extends TestCase { $this->expectException(Exception::class); (new CfgArr(['paramNotInConfig' => 123]))->param('param'); - } + } } diff --git a/tests/CfgFileTest.php b/tests/CfgFileTest.php index 6b5a250b3256c11859ccd8675080a746fa1ce4fb..5a01058df3029de0fd25fabf1e26a2c719d2f774 100644 --- a/tests/CfgFileTest.php +++ b/tests/CfgFileTest.php @@ -21,5 +21,5 @@ final class CfgFileTest extends TestCase 'asdf', (new CfgFile(__DIR__ . '/data/config.php'))->param('param') ); - } + } } diff --git a/tests/CfgTest.php b/tests/CfgTest.php index d74d076454eb87ed929dfb03d5ee8b2e162b57f6..219d081315d872bb2cc09aece0e7b12da3cddd59 100644 --- a/tests/CfgTest.php +++ b/tests/CfgTest.php @@ -31,6 +31,6 @@ final class CfgTest extends TestCase $this->assertEquals( 'asdf', $cfg->param('param') - ); - } + ); + } }