From 94f014179be1137ca53c0115821ec1187b970ebc Mon Sep 17 00:00:00 2001 From: shubaivan Date: Mon, 3 Nov 2014 22:51:57 +0200 Subject: [PATCH 1/7] create MVC --- .gitignore | 3 + .htaccess | 49 +++++++ app/views/article.html.twig | 1 + app/views/error404.html.twig | 11 ++ app/views/index.html.twig | 1 + app/views/tim.html.twig | 13 ++ app/views/tims.html.twig | 14 ++ composer.json | 10 ++ composer.lock | 174 +++++++++++++++++++++++++ index.php | 36 +++++ src/mvc/Controller/IndexController.php | 18 +++ src/mvc/Controller/TimController.php | 72 ++++++++++ src/mvc/Models/AbstractFootballTim.php | 54 ++++++++ src/mvc/Models/Article.php | 18 +++ src/mvc/Models/Barcelona.php | 23 ++++ src/mvc/Models/LCInterface.php | 13 ++ src/mvc/Models/RealMadrid.php | 27 ++++ 17 files changed, 537 insertions(+) create mode 100644 .gitignore create mode 100644 .htaccess create mode 100644 app/views/article.html.twig create mode 100644 app/views/error404.html.twig create mode 100644 app/views/index.html.twig create mode 100644 app/views/tim.html.twig create mode 100644 app/views/tims.html.twig create mode 100644 composer.json create mode 100644 composer.lock create mode 100755 index.php create mode 100644 src/mvc/Controller/IndexController.php create mode 100644 src/mvc/Controller/TimController.php create mode 100644 src/mvc/Models/AbstractFootballTim.php create mode 100644 src/mvc/Models/Article.php create mode 100644 src/mvc/Models/Barcelona.php create mode 100644 src/mvc/Models/LCInterface.php create mode 100644 src/mvc/Models/RealMadrid.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..932b431 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +.idea/ +vendor/ diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..9f9b9b2 --- /dev/null +++ b/.htaccess @@ -0,0 +1,49 @@ +# Use the front controller as index file. It serves as a fallback solution when +# every other rewrite/redirect fails (e.g. in an aliased environment without +# mod_rewrite). Additionally, this reduces the matching process for the +# start page (path "/") because otherwise Apache will apply the rewriting rules +# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl). +DirectoryIndex index.php + +RewriteEngine On +# Determine the RewriteBase automatically and set it as environment variable. +# If you are using Apache aliases to do mass virtual hosting or installed the +# project in a subdirectory, the base path will be prepended to allow proper +# resolution of the index.php file and to redirect to the correct URI. It will +# work in environments without path prefix as well, providing a safe, one-size +# fits all solution. But as you do not need it in this case, you can comment +# the following 2 lines to eliminate the overhead. +RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$ +RewriteRule ^(.*) - [E=BASE:%1] +# Sets the HTTP_AUTHORIZATION header removed by apache +RewriteCond %{HTTP:Authorization} . +RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] +# Redirect to URI without front controller to prevent duplicate content +# (with and without `/index.php`). Only do this redirect on the initial +# rewrite by Apache and not on subsequent cycles. Otherwise we would get an +# endless redirect loop (request -> rewrite to front controller -> +# redirect -> request -> ...). +# So in case you get a "too many redirects" error or you always get redirected +# to the start page because your Apache does not expose the REDIRECT_STATUS +# environment variable, you have 2 choices: +# - disable this feature by commenting the following 2 lines or +# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the +# following RewriteCond (best solution) +RewriteCond %{ENV:REDIRECT_STATUS} ^$ +RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L] +# If the requested filename exists, simply serve it. +# We only want to let Apache serve files and not directories. +RewriteCond %{REQUEST_FILENAME} -f +RewriteRule .? - [L] +# Rewrite all other queries to the front controller. +RewriteRule .? %{ENV:BASE}/index.php [L] + + + +# When mod_rewrite is not available, we instruct a temporary redirect of +# the start page to the front controller explicitly so that the website +# and the generated links can still be used. +RedirectMatch 302 ^/$ /index.php/ +# RedirectTemp cannot be used instead + + diff --git a/app/views/article.html.twig b/app/views/article.html.twig new file mode 100644 index 0000000..8394e93 --- /dev/null +++ b/app/views/article.html.twig @@ -0,0 +1 @@ +{{ method|upper }} Article with "{{ articleId }}" id action \ No newline at end of file diff --git a/app/views/error404.html.twig b/app/views/error404.html.twig new file mode 100644 index 0000000..5fab5f7 --- /dev/null +++ b/app/views/error404.html.twig @@ -0,0 +1,11 @@ + + + + + Base MVC Application - Page not found + + +

404:

+

Page not found on this server

+ + \ No newline at end of file diff --git a/app/views/index.html.twig b/app/views/index.html.twig new file mode 100644 index 0000000..6769dd6 --- /dev/null +++ b/app/views/index.html.twig @@ -0,0 +1 @@ +Hello world! \ No newline at end of file diff --git a/app/views/tim.html.twig b/app/views/tim.html.twig new file mode 100644 index 0000000..971977b --- /dev/null +++ b/app/views/tim.html.twig @@ -0,0 +1,13 @@ + + + + + Base MVC Application - Football Tim + + +

+

+ +

+ + \ No newline at end of file diff --git a/app/views/tims.html.twig b/app/views/tims.html.twig new file mode 100644 index 0000000..2b0a449 --- /dev/null +++ b/app/views/tims.html.twig @@ -0,0 +1,14 @@ + + + + + Base MVC Application - Football Tim + + +

Tims

+

+ Tims Champoin + and new Champoin +

+ + \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a9e13b5 --- /dev/null +++ b/composer.json @@ -0,0 +1,10 @@ +{ +"autoload": { +"psr-0": {"mvc": "src/"} +}, +"require": { +"symfony/http-foundation": "2.5.6", +"twig/twig": "1.16.2", +"phroute/phroute": "1.4.1" +} +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..30a49ba --- /dev/null +++ b/composer.lock @@ -0,0 +1,174 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "c2943e721b080ea027bf7043156f4b64", + "packages": [ + { + "name": "phroute/phroute", + "version": "v1.4.1", + "source": { + "type": "git", + "url": "https://github.com/mrjgreen/phroute.git", + "reference": "fd2a2be54171a650df94e03077dfcb851e41d7ec" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mrjgreen/phroute/zipball/fd2a2be54171a650df94e03077dfcb851e41d7ec", + "reference": "fd2a2be54171a650df94e03077dfcb851e41d7ec", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "satooshi/php-coveralls": "dev-master" + }, + "type": "library", + "autoload": { + "psr-0": { + "Phroute": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Nikita Popov", + "email": "nikic@php.net" + }, + { + "name": "Joe Green", + "email": "joe.green.0991@gmail.com" + } + ], + "description": "Fast request router for PHP", + "keywords": [ + "router", + "routing" + ], + "time": "2014-10-19 13:33:03" + }, + { + "name": "symfony/http-foundation", + "version": "v2.5.6", + "target-dir": "Symfony/Component/HttpFoundation", + "source": { + "type": "git", + "url": "https://github.com/symfony/HttpFoundation.git", + "reference": "56111fc8ba8bcad93d367532babecc6ce17f66ce" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/HttpFoundation/zipball/56111fc8ba8bcad93d367532babecc6ce17f66ce", + "reference": "56111fc8ba8bcad93d367532babecc6ce17f66ce", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "symfony/expression-language": "~2.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.5-dev" + } + }, + "autoload": { + "psr-0": { + "Symfony\\Component\\HttpFoundation\\": "" + }, + "classmap": [ + "Symfony/Component/HttpFoundation/Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + }, + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Symfony HttpFoundation Component", + "homepage": "http://symfony.com", + "time": "2014-10-24 05:49:22" + }, + { + "name": "twig/twig", + "version": "v1.16.2", + "source": { + "type": "git", + "url": "https://github.com/fabpot/Twig.git", + "reference": "42f758d9fe2146d1f0470604fc05ee43580873fc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/fabpot/Twig/zipball/42f758d9fe2146d1f0470604fc05ee43580873fc", + "reference": "42f758d9fe2146d1f0470604fc05ee43580873fc", + "shasum": "" + }, + "require": { + "php": ">=5.2.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.16-dev" + } + }, + "autoload": { + "psr-0": { + "Twig_": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + }, + { + "name": "Armin Ronacher", + "email": "armin.ronacher@active-4.com", + "role": "Project Founder" + }, + { + "name": "Twig Team", + "homepage": "https://github.com/fabpot/Twig/graphs/contributors", + "role": "Contributors" + } + ], + "description": "Twig, the flexible, fast, and secure template language for PHP", + "homepage": "http://twig.sensiolabs.org", + "keywords": [ + "templating" + ], + "time": "2014-10-17 12:53:44" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "platform": [], + "platform-dev": [] +} diff --git a/index.php b/index.php new file mode 100755 index 0000000..55120df --- /dev/null +++ b/index.php @@ -0,0 +1,36 @@ +get('/', [$indexController, 'indexAction']); +$router->get('/tim', [$TimController, 'getTimsAction']); +$router->get('/tim/{timId}', [$TimController, 'getTimAction']); + + +$dispatcher = new Phroute\Dispatcher($router); +try { + $response = $dispatcher->dispatch($request->getMethod(), parse_url($request->getPathInfo(), PHP_URL_PATH)); +} catch (Phroute\Exception\HttpRouteNotFoundException $e) { + $response = new Response($twig->render('error404.html.twig')); +} catch (Phroute\Exception\HttpMethodNotAllowedException $e) { + $response = new Response(sprintf('

Error 405:

Url was matched but method "%s" is not allowed', $e)); +} + +$response->send(); diff --git a/src/mvc/Controller/IndexController.php b/src/mvc/Controller/IndexController.php new file mode 100644 index 0000000..313843a --- /dev/null +++ b/src/mvc/Controller/IndexController.php @@ -0,0 +1,18 @@ +twig = $twig; + } + public function indexAction() + { + return new Response($this->twig->render('index.html.twig')); + } +} \ No newline at end of file diff --git a/src/mvc/Controller/TimController.php b/src/mvc/Controller/TimController.php new file mode 100644 index 0000000..e85d337 --- /dev/null +++ b/src/mvc/Controller/TimController.php @@ -0,0 +1,72 @@ +twig = $twig; + } + /** + * @return Response + */ + public function getTimsAction() + { + return new Response($this->twig->render('tims.html.twig')); + } + /** + * @param string $id + * @return Response + */ + public function getTimAction($timId) + { + switch ($timId) { + case 'barsa': + $Barcelona = new Barcelona('Luis Enrique', 'Camp Nou', 'Xavier '); + echo $Barcelona->show(); + + + break; + case 'real': + $RealMadrid = new RealMadrid('Carlo Ancelotti', 'Santiago Bernabeu', 'Casillas'); + echo $RealMadrid->show(); + + echo ' Champions League - ' . $RealMadrid->LC(); + break; + default: + return new Response(sprintf('

Error:

Method not found for tim "%s"

', $timId)); + } + return new Response($this->twig->render('tim.html.twig', array('show' => $timId)) . '
' . $timId); + } + /** + * @param string $id + * @return Response + */ + public function putArticleAction($id) + { + return new Response($this->twig->render('article.html.twig', ['method' => 'Put', 'articleId' => $id])); + } + /** + * @param string $id + * @return Response + */ + public function postArticleAction($id) + { + return new Response($this->twig->render('article.html.twig', ['method' => 'Post', 'articleId' => $id])); + } + /** + * @param string $id + * @return Response + */ + public function deleteArticleAction($id) + { + return new Response($this->twig->render('article.html.twig', ['method' => 'Delete', 'articleId' => $id])); + } +} \ No newline at end of file diff --git a/src/mvc/Models/AbstractFootballTim.php b/src/mvc/Models/AbstractFootballTim.php new file mode 100644 index 0000000..ff38bb1 --- /dev/null +++ b/src/mvc/Models/AbstractFootballTim.php @@ -0,0 +1,54 @@ +setTrainer($trainer); + $this->setStadium($stadium); + $this->setCaptain($captain); + } + + abstract public function show(); + + /*================Set Values==================*/ + public function setTrainer($trainer) + { + $this->trainer = $trainer; + } + public function setStadium($stadium) + { + $this->stadium = $stadium; + } + public function setCaptain($captain) + { + $this->captain = $captain; + } + /*================Get Values===================*/ + public function getTrainer() + { + return $this->trainer; + } + public function getStadium() + { + return $this->stadium; + } + public function getCaptain() + { + return $this->captain; + } + +} \ No newline at end of file diff --git a/src/mvc/Models/Article.php b/src/mvc/Models/Article.php new file mode 100644 index 0000000..3fff218 --- /dev/null +++ b/src/mvc/Models/Article.php @@ -0,0 +1,18 @@ +title = $title; + } + + public function getTitle() + { + return $this->title; + } +} diff --git a/src/mvc/Models/Barcelona.php b/src/mvc/Models/Barcelona.php new file mode 100644 index 0000000..677ef64 --- /dev/null +++ b/src/mvc/Models/Barcelona.php @@ -0,0 +1,23 @@ +getTrainer(), $this->getStadium(), $this->getCaptain()); + } + +} \ No newline at end of file diff --git a/src/mvc/Models/LCInterface.php b/src/mvc/Models/LCInterface.php new file mode 100644 index 0000000..45c962d --- /dev/null +++ b/src/mvc/Models/LCInterface.php @@ -0,0 +1,13 @@ +getTrainer(), $this->getStadium(), $this->getCaptain()); + } + public function LC() + { + return "Chempion"; + } + +} \ No newline at end of file From d1661384c41a89d04be378fe21f1b6526228f120 Mon Sep 17 00:00:00 2001 From: shubaivan Date: Tue, 4 Nov 2014 11:14:30 +0200 Subject: [PATCH 2/7] add kernel --- app/views/article.html.twig | 2 +- app/views/error404.html.twig | 2 +- app/views/index.html.twig | 2 +- app/views/tim.html.twig | 2 +- app/views/tims.html.twig | 2 +- composer.json | 3 +- composer.lock | 745 ++++++++++++++++++++++++- index.php | 3 - src/mvc/Controller/IndexController.php | 2 +- src/mvc/Controller/TimController.php | 13 +- src/mvc/Kernel.php | 39 ++ src/mvc/KernelInterface.php | 12 + src/mvc/Models/AbstractFootballTim.php | 3 +- src/mvc/Models/Barcelona.php | 5 +- src/mvc/Models/LCInterface.php | 2 +- src/mvc/Models/RealMadrid.php | 5 +- src/mvc/MvcKernel.php | 23 + 17 files changed, 838 insertions(+), 27 deletions(-) create mode 100644 src/mvc/Kernel.php create mode 100644 src/mvc/KernelInterface.php create mode 100644 src/mvc/MvcKernel.php diff --git a/app/views/article.html.twig b/app/views/article.html.twig index 8394e93..154c80d 100644 --- a/app/views/article.html.twig +++ b/app/views/article.html.twig @@ -1 +1 @@ -{{ method|upper }} Article with "{{ articleId }}" id action \ No newline at end of file +{{ method|upper }} Article with "{{ articleId }}" id action diff --git a/app/views/error404.html.twig b/app/views/error404.html.twig index 5fab5f7..8c25ff4 100644 --- a/app/views/error404.html.twig +++ b/app/views/error404.html.twig @@ -8,4 +8,4 @@

404:

Page not found on this server

- \ No newline at end of file + diff --git a/app/views/index.html.twig b/app/views/index.html.twig index 6769dd6..cd08755 100644 --- a/app/views/index.html.twig +++ b/app/views/index.html.twig @@ -1 +1 @@ -Hello world! \ No newline at end of file +Hello world! diff --git a/app/views/tim.html.twig b/app/views/tim.html.twig index 971977b..a4bd2e0 100644 --- a/app/views/tim.html.twig +++ b/app/views/tim.html.twig @@ -10,4 +10,4 @@

- \ No newline at end of file + diff --git a/app/views/tims.html.twig b/app/views/tims.html.twig index 2b0a449..5972266 100644 --- a/app/views/tims.html.twig +++ b/app/views/tims.html.twig @@ -11,4 +11,4 @@ and new Champoin

- \ No newline at end of file + diff --git a/composer.json b/composer.json index a9e13b5..45d7543 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "require": { "symfony/http-foundation": "2.5.6", "twig/twig": "1.16.2", -"phroute/phroute": "1.4.1" +"phroute/phroute": "1.4.1", +"phpunit/phpunit": "4.3.4" } } diff --git a/composer.lock b/composer.lock index 30a49ba..685c304 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,438 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "c2943e721b080ea027bf7043156f4b64", + "hash": "e97d1bc3d3286339752ba8db1435d022", "packages": [ + { + "name": "doctrine/instantiator", + "version": "1.0.4", + "source": { + "type": "git", + "url": "https://github.com/doctrine/instantiator.git", + "reference": "f976e5de371104877ebc89bd8fecb0019ed9c119" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/f976e5de371104877ebc89bd8fecb0019ed9c119", + "reference": "f976e5de371104877ebc89bd8fecb0019ed9c119", + "shasum": "" + }, + "require": { + "php": ">=5.3,<8.0-DEV" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "ext-pdo": "*", + "ext-phar": "*", + "phpunit/phpunit": "~4.0", + "squizlabs/php_codesniffer": "2.0.*@ALPHA" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Instantiator\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/" + } + ], + "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", + "homepage": "https://github.com/doctrine/instantiator", + "keywords": [ + "constructor", + "instantiate" + ], + "time": "2014-10-13 12:58:55" + }, + { + "name": "phpunit/php-code-coverage", + "version": "2.0.11", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-code-coverage.git", + "reference": "53603b3c995f5aab6b59c8e08c3a663d2cc810b7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/53603b3c995f5aab6b59c8e08c3a663d2cc810b7", + "reference": "53603b3c995f5aab6b59c8e08c3a663d2cc810b7", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "phpunit/php-file-iterator": "~1.3", + "phpunit/php-text-template": "~1.2", + "phpunit/php-token-stream": "~1.3", + "sebastian/environment": "~1.0", + "sebastian/version": "~1.0" + }, + "require-dev": { + "ext-xdebug": ">=2.1.4", + "phpunit/phpunit": "~4.1" + }, + "suggest": { + "ext-dom": "*", + "ext-xdebug": ">=2.2.1", + "ext-xmlwriter": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", + "homepage": "https://github.com/sebastianbergmann/php-code-coverage", + "keywords": [ + "coverage", + "testing", + "xunit" + ], + "time": "2014-08-31 06:33:04" + }, + { + "name": "phpunit/php-file-iterator", + "version": "1.3.4", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/acd690379117b042d1c8af1fafd61bde001bf6bb", + "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "File/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "FilterIterator implementation that filters files based on a list of suffixes.", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", + "keywords": [ + "filesystem", + "iterator" + ], + "time": "2013-10-10 15:34:57" + }, + { + "name": "phpunit/php-text-template", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "206dfefc0ffe9cebf65c413e3d0e809c82fbf00a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/206dfefc0ffe9cebf65c413e3d0e809c82fbf00a", + "reference": "206dfefc0ffe9cebf65c413e3d0e809c82fbf00a", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "Text/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Simple template engine.", + "homepage": "https://github.com/sebastianbergmann/php-text-template/", + "keywords": [ + "template" + ], + "time": "2014-01-30 17:20:04" + }, + { + "name": "phpunit/php-timer", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-timer.git", + "reference": "19689d4354b295ee3d8c54b4f42c3efb69cbc17c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/19689d4354b295ee3d8c54b4f42c3efb69cbc17c", + "reference": "19689d4354b295ee3d8c54b4f42c3efb69cbc17c", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "PHP/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Utility class for timing", + "homepage": "https://github.com/sebastianbergmann/php-timer/", + "keywords": [ + "timer" + ], + "time": "2013-08-02 07:42:54" + }, + { + "name": "phpunit/php-token-stream", + "version": "1.3.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-token-stream.git", + "reference": "f8d5d08c56de5cfd592b3340424a81733259a876" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/f8d5d08c56de5cfd592b3340424a81733259a876", + "reference": "f8d5d08c56de5cfd592b3340424a81733259a876", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Wrapper around PHP's tokenizer extension.", + "homepage": "https://github.com/sebastianbergmann/php-token-stream/", + "keywords": [ + "tokenizer" + ], + "time": "2014-08-31 06:12:13" + }, + { + "name": "phpunit/phpunit", + "version": "4.3.4", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit.git", + "reference": "23e4e0310f037aae873cc81b8658dbbb82878f71" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/23e4e0310f037aae873cc81b8658dbbb82878f71", + "reference": "23e4e0310f037aae873cc81b8658dbbb82878f71", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-json": "*", + "ext-pcre": "*", + "ext-reflection": "*", + "ext-spl": "*", + "php": ">=5.3.3", + "phpunit/php-code-coverage": "~2.0", + "phpunit/php-file-iterator": "~1.3.2", + "phpunit/php-text-template": "~1.2", + "phpunit/php-timer": "~1.0.2", + "phpunit/phpunit-mock-objects": "~2.3", + "sebastian/comparator": "~1.0", + "sebastian/diff": "~1.1", + "sebastian/environment": "~1.0", + "sebastian/exporter": "~1.0", + "sebastian/version": "~1.0", + "symfony/yaml": "~2.0" + }, + "suggest": { + "phpunit/php-invoker": "~1.1" + }, + "bin": [ + "phpunit" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.3.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "", + "../../symfony/yaml/" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "The PHP Unit Testing framework.", + "homepage": "http://www.phpunit.de/", + "keywords": [ + "phpunit", + "testing", + "xunit" + ], + "time": "2014-10-22 11:43:12" + }, + { + "name": "phpunit/phpunit-mock-objects", + "version": "2.3.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", + "reference": "c63d2367247365f688544f0d500af90a11a44c65" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/c63d2367247365f688544f0d500af90a11a44c65", + "reference": "c63d2367247365f688544f0d500af90a11a44c65", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "~1.0,>=1.0.1", + "php": ">=5.3.3", + "phpunit/php-text-template": "~1.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.3" + }, + "suggest": { + "ext-soap": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.3.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Mock Object library for PHPUnit", + "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", + "keywords": [ + "mock", + "xunit" + ], + "time": "2014-10-03 05:12:11" + }, { "name": "phroute/phroute", "version": "v1.4.1", @@ -53,6 +483,272 @@ ], "time": "2014-10-19 13:33:03" }, + { + "name": "sebastian/comparator", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/comparator.git", + "reference": "e54a01c0da1b87db3c5a3c4c5277ddf331da4aef" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/e54a01c0da1b87db3c5a3c4c5277ddf331da4aef", + "reference": "e54a01c0da1b87db3c5a3c4c5277ddf331da4aef", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "sebastian/diff": "~1.1", + "sebastian/exporter": "~1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides the functionality to compare PHP values for equality", + "homepage": "http://www.github.com/sebastianbergmann/comparator", + "keywords": [ + "comparator", + "compare", + "equality" + ], + "time": "2014-05-11 23:00:21" + }, + { + "name": "sebastian/diff", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/diff.git", + "reference": "5843509fed39dee4b356a306401e9dd1a931fec7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/5843509fed39dee4b356a306401e9dd1a931fec7", + "reference": "5843509fed39dee4b356a306401e9dd1a931fec7", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Kore Nordmann", + "email": "mail@kore-nordmann.de" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Diff implementation", + "homepage": "http://www.github.com/sebastianbergmann/diff", + "keywords": [ + "diff" + ], + "time": "2014-08-15 10:29:00" + }, + { + "name": "sebastian/environment", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/environment.git", + "reference": "0d9bf79554d2a999da194a60416c15cf461eb67d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/0d9bf79554d2a999da194a60416c15cf461eb67d", + "reference": "0d9bf79554d2a999da194a60416c15cf461eb67d", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides functionality to handle HHVM/PHP environments", + "homepage": "http://www.github.com/sebastianbergmann/environment", + "keywords": [ + "Xdebug", + "environment", + "hhvm" + ], + "time": "2014-10-22 06:38:05" + }, + { + "name": "sebastian/exporter", + "version": "1.0.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/exporter.git", + "reference": "c7d59948d6e82818e1bdff7cadb6c34710eb7dc0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/c7d59948d6e82818e1bdff7cadb6c34710eb7dc0", + "reference": "c7d59948d6e82818e1bdff7cadb6c34710eb7dc0", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides the functionality to export PHP variables for visualization", + "homepage": "http://www.github.com/sebastianbergmann/exporter", + "keywords": [ + "export", + "exporter" + ], + "time": "2014-09-10 00:51:36" + }, + { + "name": "sebastian/version", + "version": "1.0.3", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/version.git", + "reference": "b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43", + "reference": "b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43", + "shasum": "" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that helps with managing the version number of Git-hosted PHP projects", + "homepage": "https://github.com/sebastianbergmann/version", + "time": "2014-03-07 15:35:33" + }, { "name": "symfony/http-foundation", "version": "v2.5.6", @@ -106,6 +802,53 @@ "homepage": "http://symfony.com", "time": "2014-10-24 05:49:22" }, + { + "name": "symfony/yaml", + "version": "v2.5.6", + "target-dir": "Symfony/Component/Yaml", + "source": { + "type": "git", + "url": "https://github.com/symfony/Yaml.git", + "reference": "2d9f527449cabfa8543dd7fa3a466d6ae83d6726" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/2d9f527449cabfa8543dd7fa3a466d6ae83d6726", + "reference": "2d9f527449cabfa8543dd7fa3a466d6ae83d6726", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.5-dev" + } + }, + "autoload": { + "psr-0": { + "Symfony\\Component\\Yaml\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + }, + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Symfony Yaml Component", + "homepage": "http://symfony.com", + "time": "2014-10-01 05:50:18" + }, { "name": "twig/twig", "version": "v1.16.2", diff --git a/index.php b/index.php index 55120df..225b431 100755 --- a/index.php +++ b/index.php @@ -8,12 +8,10 @@ use mvc\Controller\IndexController; use Phroute\RouteCollector; - $request = Request::createFromGlobals(); $loader = new Twig_Loader_Filesystem(__DIR__ . '/app/views'); $twig = new Twig_Environment($loader); - $TimController = new TimController($twig); $indexController = new IndexController($twig); @@ -23,7 +21,6 @@ $router->get('/tim', [$TimController, 'getTimsAction']); $router->get('/tim/{timId}', [$TimController, 'getTimAction']); - $dispatcher = new Phroute\Dispatcher($router); try { $response = $dispatcher->dispatch($request->getMethod(), parse_url($request->getPathInfo(), PHP_URL_PATH)); diff --git a/src/mvc/Controller/IndexController.php b/src/mvc/Controller/IndexController.php index 313843a..b70f4cf 100644 --- a/src/mvc/Controller/IndexController.php +++ b/src/mvc/Controller/IndexController.php @@ -15,4 +15,4 @@ public function indexAction() { return new Response($this->twig->render('index.html.twig')); } -} \ No newline at end of file +} diff --git a/src/mvc/Controller/TimController.php b/src/mvc/Controller/TimController.php index e85d337..34df3df 100644 --- a/src/mvc/Controller/TimController.php +++ b/src/mvc/Controller/TimController.php @@ -22,7 +22,7 @@ public function getTimsAction() return new Response($this->twig->render('tims.html.twig')); } /** - * @param string $id + * @param string $id * @return Response */ public function getTimAction($timId) @@ -31,8 +31,6 @@ public function getTimAction($timId) case 'barsa': $Barcelona = new Barcelona('Luis Enrique', 'Camp Nou', 'Xavier '); echo $Barcelona->show(); - - break; case 'real': $RealMadrid = new RealMadrid('Carlo Ancelotti', 'Santiago Bernabeu', 'Casillas'); @@ -43,10 +41,11 @@ public function getTimAction($timId) default: return new Response(sprintf('

Error:

Method not found for tim "%s"

', $timId)); } + return new Response($this->twig->render('tim.html.twig', array('show' => $timId)) . '
' . $timId); } /** - * @param string $id + * @param string $id * @return Response */ public function putArticleAction($id) @@ -54,7 +53,7 @@ public function putArticleAction($id) return new Response($this->twig->render('article.html.twig', ['method' => 'Put', 'articleId' => $id])); } /** - * @param string $id + * @param string $id * @return Response */ public function postArticleAction($id) @@ -62,11 +61,11 @@ public function postArticleAction($id) return new Response($this->twig->render('article.html.twig', ['method' => 'Post', 'articleId' => $id])); } /** - * @param string $id + * @param string $id * @return Response */ public function deleteArticleAction($id) { return new Response($this->twig->render('article.html.twig', ['method' => 'Delete', 'articleId' => $id])); } -} \ No newline at end of file +} diff --git a/src/mvc/Kernel.php b/src/mvc/Kernel.php new file mode 100644 index 0000000..c70c4a4 --- /dev/null +++ b/src/mvc/Kernel.php @@ -0,0 +1,39 @@ +handleRoutes($this->getRoutes()); + $dispatcher = new Dispatcher($router); + try { + $response = $dispatcher->dispatch($request->getMethod(), parse_url($request->getPathInfo(), PHP_URL_PATH)); + } catch (Phroute\Exception\HttpRouteNotFoundException $e) { + $response = new Response($twig->render('error404.html.twig')); + } catch (Phroute\Exception\HttpMethodNotAllowedException $e) { + $response = new Response(sprintf('

Error 405:

Url was matched but method "%s" is not allowed', $e)); + } + return $response; + } + protected function handleRoutes(array $routes) + { + $TimController = $this->getControllers($routes); + $router = new RouteCollector(); + + return $router; + } + protected function getControllers($routes) + { + $loader = new Twig_Loader_Filesystem(__DIR__ . '/app/views'); + $twig = new Twig_Environment($loader); + $TimController = new TimController($twig); + $indexController = new IndexController($twig); + return $TimController; + } +} \ No newline at end of file diff --git a/src/mvc/KernelInterface.php b/src/mvc/KernelInterface.php new file mode 100644 index 0000000..7c0475f --- /dev/null +++ b/src/mvc/KernelInterface.php @@ -0,0 +1,12 @@ +captain; } -} \ No newline at end of file +} diff --git a/src/mvc/Models/Barcelona.php b/src/mvc/Models/Barcelona.php index 677ef64..5801e44 100644 --- a/src/mvc/Models/Barcelona.php +++ b/src/mvc/Models/Barcelona.php @@ -8,10 +8,9 @@ namespace mvc\Models; - class Barcelona extends AbstractFootballTim { - function __construct($trainer, $stadium, $captain) + public function __construct($trainer, $stadium, $captain) { parent::__construct($trainer, $stadium, $captain); } @@ -20,4 +19,4 @@ public function show() return sprintf('trainer %s stadium in %s captain %s',$this->getTrainer(), $this->getStadium(), $this->getCaptain()); } -} \ No newline at end of file +} diff --git a/src/mvc/Models/LCInterface.php b/src/mvc/Models/LCInterface.php index 45c962d..619d892 100644 --- a/src/mvc/Models/LCInterface.php +++ b/src/mvc/Models/LCInterface.php @@ -10,4 +10,4 @@ interface LCInterface { public function LC(); -} \ No newline at end of file +} diff --git a/src/mvc/Models/RealMadrid.php b/src/mvc/Models/RealMadrid.php index 4e83cd5..2c3e06e 100644 --- a/src/mvc/Models/RealMadrid.php +++ b/src/mvc/Models/RealMadrid.php @@ -8,10 +8,9 @@ namespace mvc\Models; - class RealMadrid extends AbstractFootballTim implements LCInterface { - function __construct($trainer, $stadium, $captain) + public function __construct($trainer, $stadium, $captain) { parent::__construct($trainer, $stadium, $captain); } @@ -24,4 +23,4 @@ public function LC() return "Chempion"; } -} \ No newline at end of file +} diff --git a/src/mvc/MvcKernel.php b/src/mvc/MvcKernel.php new file mode 100644 index 0000000..f714215 --- /dev/null +++ b/src/mvc/MvcKernel.php @@ -0,0 +1,23 @@ + Date: Wed, 5 Nov 2014 11:10:19 +0200 Subject: [PATCH 3/7] error kernel --- index.php | 7 ++++++- src/mvc/Kernel.php | 16 ++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/index.php b/index.php index 225b431..64e36d5 100755 --- a/index.php +++ b/index.php @@ -7,8 +7,9 @@ use mvc\Controller\TimController; use mvc\Controller\IndexController; use Phroute\RouteCollector; +use mvc\MvcKernel; -$request = Request::createFromGlobals(); +/*$request = Request::createFromGlobals(); $loader = new Twig_Loader_Filesystem(__DIR__ . '/app/views'); $twig = new Twig_Environment($loader); @@ -30,4 +31,8 @@ $response = new Response(sprintf('

Error 405:

Url was matched but method "%s" is not allowed', $e)); } +$response->send();*/ +$request = Request::createFromGlobals(); +$kernel = new MvcKernel(); +$response = $kernel->handle($request); $response->send(); diff --git a/src/mvc/Kernel.php b/src/mvc/Kernel.php index c70c4a4..a4be91d 100644 --- a/src/mvc/Kernel.php +++ b/src/mvc/Kernel.php @@ -5,35 +5,39 @@ use Phroute\RouteCollector; use Phroute\Dispatcher; use Symfony\Component\HttpFoundation\Request; +use mvc\Controller\TimController; +use mvc\Controller\IndexController; abstract class Kernel implements KernelInterface { public function handle(Request $request) { + $loader = new \Twig_Loader_Filesystem(__DIR__ . '/app/views'); + $twig = new \Twig_Environment($loader); $router = $this->handleRoutes($this->getRoutes()); $dispatcher = new Dispatcher($router); try { $response = $dispatcher->dispatch($request->getMethod(), parse_url($request->getPathInfo(), PHP_URL_PATH)); - } catch (Phroute\Exception\HttpRouteNotFoundException $e) { + } catch ( \Phroute\Exception\HttpRouteNotFoundException $e) { $response = new Response($twig->render('error404.html.twig')); - } catch (Phroute\Exception\HttpMethodNotAllowedException $e) { + } catch ( \Phroute\Exception\HttpMethodNotAllowedException $e) { $response = new Response(sprintf('

Error 405:

Url was matched but method "%s" is not allowed', $e)); } return $response; } protected function handleRoutes(array $routes) { - $TimController = $this->getControllers($routes); $router = new RouteCollector(); - + $TimController = $this->getControllers($routes); return $router; } protected function getControllers($routes) { - $loader = new Twig_Loader_Filesystem(__DIR__ . '/app/views'); - $twig = new Twig_Environment($loader); + $loader = new \Twig_Loader_Filesystem(__DIR__ . '/app/views'); + $twig = new \Twig_Environment($loader); $TimController = new TimController($twig); $indexController = new IndexController($twig); return $TimController; + } } \ No newline at end of file From 42bb3f55325ead3710ced31c4ce0960e5d8873be Mon Sep 17 00:00:00 2001 From: shubaivan Date: Wed, 5 Nov 2014 15:51:53 +0200 Subject: [PATCH 4/7] problem with try cathc $twig --- src/mvc/Controller/IndexController.php | 4 ++++ src/mvc/Kernel.php | 28 +++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/mvc/Controller/IndexController.php b/src/mvc/Controller/IndexController.php index b70f4cf..c30e4f3 100644 --- a/src/mvc/Controller/IndexController.php +++ b/src/mvc/Controller/IndexController.php @@ -15,4 +15,8 @@ public function indexAction() { return new Response($this->twig->render('index.html.twig')); } + public function errorAction() + { + return new Response($this->twig->render('error404.html.twig')); + } } diff --git a/src/mvc/Kernel.php b/src/mvc/Kernel.php index a4be91d..89324a5 100644 --- a/src/mvc/Kernel.php +++ b/src/mvc/Kernel.php @@ -12,32 +12,36 @@ abstract class Kernel implements KernelInterface { public function handle(Request $request) { - $loader = new \Twig_Loader_Filesystem(__DIR__ . '/app/views'); - $twig = new \Twig_Environment($loader); $router = $this->handleRoutes($this->getRoutes()); $dispatcher = new Dispatcher($router); try { $response = $dispatcher->dispatch($request->getMethod(), parse_url($request->getPathInfo(), PHP_URL_PATH)); - } catch ( \Phroute\Exception\HttpRouteNotFoundException $e) { + } catch (\Phroute\Exception\HttpRouteNotFoundException $e) { $response = new Response($twig->render('error404.html.twig')); - } catch ( \Phroute\Exception\HttpMethodNotAllowedException $e) { + } catch (\Phroute\Exception\HttpMethodNotAllowedException $e) { $response = new Response(sprintf('

Error 405:

Url was matched but method "%s" is not allowed', $e)); } return $response; } protected function handleRoutes(array $routes) { + $controllers = $this->getControllers($routes); $router = new RouteCollector(); - $TimController = $this->getControllers($routes); + foreach ($routes as $routeDefinition) { + list($controller, $method) = explode(':', $routeDefinition[2]); + $router->{strtolower($routeDefinition[0])}($routeDefinition[1], [$controllers[$controller],$method]); + } return $router; } protected function getControllers($routes) { - $loader = new \Twig_Loader_Filesystem(__DIR__ . '/app/views'); - $twig = new \Twig_Environment($loader); - $TimController = new TimController($twig); - $indexController = new IndexController($twig); - return $TimController; - + $controllers = array(); + foreach ($routes as $routeDefinition) { + $controllerMethod = array_pop($routeDefinition); + list($controller, $method) = explode(':', $controllerMethod); + $controllers[$controller] = new $controller($this->getTemplateHandler()); + } + return $controllers; } -} \ No newline at end of file +} + From 1777cdab355b7f26824c89a6e08f9dfee6690d37 Mon Sep 17 00:00:00 2001 From: shubaivan Date: Wed, 5 Nov 2014 20:35:23 +0200 Subject: [PATCH 5/7] if getRuotes --- src/mvc/Controller/TimController.php | 4 ++++ src/mvc/Kernel.php | 1 + src/mvc/MvcKernel.php | 23 ++++++++++++++++++----- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/mvc/Controller/TimController.php b/src/mvc/Controller/TimController.php index 34df3df..1f2cc29 100644 --- a/src/mvc/Controller/TimController.php +++ b/src/mvc/Controller/TimController.php @@ -21,6 +21,10 @@ public function getTimsAction() { return new Response($this->twig->render('tims.html.twig')); } + public function getErrorAction() + { + return new Response($this->twig->render('error404.html.twig')); + } /** * @param string $id * @return Response diff --git a/src/mvc/Kernel.php b/src/mvc/Kernel.php index 89324a5..be7c589 100644 --- a/src/mvc/Kernel.php +++ b/src/mvc/Kernel.php @@ -7,6 +7,7 @@ use Symfony\Component\HttpFoundation\Request; use mvc\Controller\TimController; use mvc\Controller\IndexController; +use Symfony\Component\HttpFoundation\Response; abstract class Kernel implements KernelInterface { diff --git a/src/mvc/MvcKernel.php b/src/mvc/MvcKernel.php index f714215..5962ce8 100644 --- a/src/mvc/MvcKernel.php +++ b/src/mvc/MvcKernel.php @@ -2,17 +2,30 @@ namespace mvc; +use mvc\Controller\TimController; + class MvcKernel extends Kernel { public function getRoutes() { - return array( - ['GET', '/', 'mvc\Controller\IndexController:indexAction'], - ['GET', '/tim', 'mvc\Controller\TimController:getTimsAction'], - ['GET', '/tim/{timId}', 'mvc\Controller\TimController:getTimAction'], + if (!empty($arr)) + { + return array( + + + ['GET', '/', 'mvc\Controller\IndexController:indexAction'], + ['GET', '/tim', 'mvc\Controller\TimController:getTimsAction'], + ['GET', '/tim/{timId}', 'mvc\Controller\TimController:getTimAction'], + + ); + } + else + return array( + ['GET', '/error', 'mvc\Controller\TimController:geterrorAction'], + ); + - ); } public function getTemplateHandler() { From fa32e4a2830fe52dc8720f59b65032dc86ba4fbb Mon Sep 17 00:00:00 2001 From: shubaivan Date: Wed, 5 Nov 2014 21:18:18 +0200 Subject: [PATCH 6/7] add twig in catch --- src/mvc/Kernel.php | 2 ++ src/mvc/MvcKernel.php | 11 ----------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/src/mvc/Kernel.php b/src/mvc/Kernel.php index be7c589..9f1c60e 100644 --- a/src/mvc/Kernel.php +++ b/src/mvc/Kernel.php @@ -18,6 +18,8 @@ public function handle(Request $request) try { $response = $dispatcher->dispatch($request->getMethod(), parse_url($request->getPathInfo(), PHP_URL_PATH)); } catch (\Phroute\Exception\HttpRouteNotFoundException $e) { + $loader = new \Twig_Loader_Filesystem(__DIR__ . '/../../app/views'); + $twig = new \Twig_Environment($loader); $response = new Response($twig->render('error404.html.twig')); } catch (\Phroute\Exception\HttpMethodNotAllowedException $e) { $response = new Response(sprintf('

Error 405:

Url was matched but method "%s" is not allowed', $e)); diff --git a/src/mvc/MvcKernel.php b/src/mvc/MvcKernel.php index 5962ce8..a8cc76c 100644 --- a/src/mvc/MvcKernel.php +++ b/src/mvc/MvcKernel.php @@ -8,23 +8,12 @@ class MvcKernel extends Kernel { public function getRoutes() { - - if (!empty($arr)) - { return array( - - ['GET', '/', 'mvc\Controller\IndexController:indexAction'], ['GET', '/tim', 'mvc\Controller\TimController:getTimsAction'], ['GET', '/tim/{timId}', 'mvc\Controller\TimController:getTimAction'], ); - } - else - return array( - ['GET', '/error', 'mvc\Controller\TimController:geterrorAction'], - ); - } public function getTemplateHandler() From b8c1a646a5d7b2f169949e2bf7e3af7627347322 Mon Sep 17 00:00:00 2001 From: shubaivan Date: Wed, 5 Nov 2014 21:44:17 +0200 Subject: [PATCH 7/7] this is work -> $response = new Response($this->getTemplateHandler()->render('error404.html.twig')); --- src/mvc/Kernel.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mvc/Kernel.php b/src/mvc/Kernel.php index 9f1c60e..78db66b 100644 --- a/src/mvc/Kernel.php +++ b/src/mvc/Kernel.php @@ -18,9 +18,8 @@ public function handle(Request $request) try { $response = $dispatcher->dispatch($request->getMethod(), parse_url($request->getPathInfo(), PHP_URL_PATH)); } catch (\Phroute\Exception\HttpRouteNotFoundException $e) { - $loader = new \Twig_Loader_Filesystem(__DIR__ . '/../../app/views'); - $twig = new \Twig_Environment($loader); - $response = new Response($twig->render('error404.html.twig')); + + $response = new Response($this->getTemplateHandler()->render('error404.html.twig')); } catch (\Phroute\Exception\HttpMethodNotAllowedException $e) { $response = new Response(sprintf('

Error 405:

Url was matched but method "%s" is not allowed', $e)); }