diff --git a/core/bootstrap.php b/core/bootstrap.php index 6e121cfe9..8a8362a35 100644 --- a/core/bootstrap.php +++ b/core/bootstrap.php @@ -5,9 +5,10 @@ * @file * Functions needed for Thelia bootstrap */ -define('THELIA_ROOT', __DIR__ .'/../'); +define('THELIA_ROOT', realpath(__DIR__ .'/../') . "/"); define('THELIA_CONF_DIR', THELIA_ROOT . '/local/config'); define('THELIA_PLUGIN_DIR', THELIA_ROOT . '/local/plugins'); +define('THELIA_TEMPLATE_DIR', THELIA_ROOT . 'templates/'); $loader = require __DIR__ . "/vendor/autoload.php"; diff --git a/core/lib/Thelia/Action/BaseAction.php b/core/lib/Thelia/Action/BaseAction.php index 0871f609c..3739f234d 100644 --- a/core/lib/Thelia/Action/BaseAction.php +++ b/core/lib/Thelia/Action/BaseAction.php @@ -26,34 +26,34 @@ namespace Thelia\Action; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** - * - * + * + * * @author Manuel Raynaud */ -abstract class BaseAction +abstract class BaseAction { /** * * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface */ protected $dispatcher; - + /** - * + * * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */ - public function __construct(EventDispatcherInterface $dispatcher) + public function __construct(EventDispatcherInterface $dispatcher) { $this->dispatcher = $dispatcher; } - + /** - * + * * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface */ public function getDispatcher() { return $this->dispatcher; } -} \ No newline at end of file +} diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index edb8d2a1d..739f3e402 100644 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -29,36 +29,35 @@ use Thelia\Action\BaseAction; class Cart extends BaseAction { /** - * + * * add an article to cart - * + * * @param \Symfony\Component\HttpFoundation\Request $request */ public function addCart(Request $request) { - + } - + /** - * + * * Delete specify article present into cart - * + * * @param \Symfony\Component\HttpFoundation\Request $request */ public function deleteArticle(Request $request) { - + } - + /** - * + * * Modify article's quantity - * + * * @param \Symfony\Component\HttpFoundation\Request $request */ public function modifyArticle(Request $request) { - + } } - diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index b66396289..1fc74f123 100644 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -27,19 +27,19 @@ use Symfony\Component\HttpFoundation\Request; class Customer { - + public function create(Request $request) { - + } - + public function modify(Request $request) { - + } - + public function modifyPassword(Request $request) { - + } } diff --git a/core/lib/Thelia/Controller/DefaultController.php b/core/lib/Thelia/Controller/DefaultController.php index b143af56a..2c8a4ca62 100644 --- a/core/lib/Thelia/Controller/DefaultController.php +++ b/core/lib/Thelia/Controller/DefaultController.php @@ -44,12 +44,13 @@ class DefaultController implements NullControllerInterface */ public function noAction(Request $request) { - if ($request->query->has('view') === false) { - $fond = "index"; + if (! $view = $request->query->get('view')) { + $view = "index"; if ($request->request->has('view')) { - $fond = $request->request->get('view'); + $view = $request->request->get('view'); } - $request->query->set('view', $fond); } + + $request->attributes->set('_view', $view); } } diff --git a/core/lib/Thelia/Core/Bundle/TheliaBundle.php b/core/lib/Thelia/Core/Bundle/TheliaBundle.php index a95082e73..35f735434 100644 --- a/core/lib/Thelia/Core/Bundle/TheliaBundle.php +++ b/core/lib/Thelia/Core/Bundle/TheliaBundle.php @@ -91,7 +91,7 @@ class TheliaBundle extends Bundle */ $container->register('thelia.listener.view','Thelia\Core\EventListener\ViewListener') - ->addArgument(new Reference('parser')) + ->addArgument(new Reference('service_container')) ; diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 8d5bb258c..7b6c9e178 100644 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -40,4 +40,11 @@ final class TheliaEvents * Send if no action are already present in Thelia action process ( see Thelia\Routing\Matcher\ActionMatcher) */ const ACTION = "thelia.action"; + + /** + * INCLUDE event + * + * Send before starting thelia inclusion + */ + const INCLUSION = "thelia.include"; } \ No newline at end of file diff --git a/core/lib/Thelia/Core/EventListener/ViewListener.php b/core/lib/Thelia/Core/EventListener/ViewListener.php index 208050e64..e1e417504 100644 --- a/core/lib/Thelia/Core/EventListener/ViewListener.php +++ b/core/lib/Thelia/Core/EventListener/ViewListener.php @@ -25,7 +25,10 @@ namespace Thelia\Core\EventListener; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Thelia\Core\Template\ParserInterface; /** @@ -39,15 +42,19 @@ use Thelia\Core\Template\ParserInterface; class ViewListener implements EventSubscriberInterface { - private $parser; + /** + * + * @var Symfony\Component\DependencyInjection\ContainerInterface + */ + private $container; /** * * @param \Thelia\Core\Template\ParserInterface $parser */ - public function __construct(ParserInterface $parser) + public function __construct(ContainerInterface $container) { - $this->parser = $parser; + $this->container = $container; } /** @@ -60,13 +67,41 @@ class ViewListener implements EventSubscriberInterface */ public function onKernelView(GetResponseForControllerResultEvent $event) { - $content = $this->parser->getContent(); + $parser = $this->container->get('parser'); + try { + $content = $parser->getContent(); - if ($content instanceof Response) { - $event->setResponse($content); - } else { - $event->setResponse(new Response($content, $this->parser->getStatus() ?: 200)); + if ($content instanceof Response) { + $event->setResponse($content); + } else { + $event->setResponse(new Response($content, $parser->getStatus() ?: 200)); + } + } catch(ResourceNotFoundException $e) { + $event->setResponse(new Response($e->getMessage(), 404)); } + + } + + public function beforeKernelView(GetResponseForControllerResultEvent $event) + { + $request = $this->container->get('request'); + + if (!$view = $request->attributes->get('_view')) { + $request->attributes->set('_view', $this->findView($request)); + } + + } + + public function findView(Request $request) + { + if (! $view = $request->query->get('view')) { + $view = "index"; + if ($request->request->has('view')) { + $view = $request->request->get('view'); + } + } + + return $view; } @@ -79,7 +114,10 @@ class ViewListener implements EventSubscriberInterface public static function getSubscribedEvents() { return array( - KernelEvents::VIEW => array('onKernelView'), + KernelEvents::VIEW =>array( + array('onKernelView', 0), + array('beforeKernelView', 5) + ) ); } } diff --git a/core/lib/Thelia/Core/Template/Parser.php b/core/lib/Thelia/Core/Template/Parser.php index fc2c597b1..05b6e7a59 100644 --- a/core/lib/Thelia/Core/Template/Parser.php +++ b/core/lib/Thelia/Core/Template/Parser.php @@ -25,6 +25,8 @@ namespace Thelia\Core\Template; use Symfony\Component\HttpFoundation\Response; use Thelia\Core\Template\ParserInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; /** * @@ -44,15 +46,18 @@ class Parser implements ParserInterface const SHOW_TIME = true; const ALLOW_DEBUG = true; const USE_CACHE = true; + /** * * @var Symfony\Component\DependencyInjection\ContainerInterface */ protected $container; - + protected $content; protected $status = 200; + + protected $template = "default"; /** * @@ -64,6 +69,24 @@ class Parser implements ParserInterface { $this->container = $container; } + + /** + * + * @return Symfony\Component\HttpFoundation\Request + */ + public function getRequest() + { + return $this->container->get('request'); + } + + /** + * + * @return Symfony\Component\EventDispatcher\EventDispatcher + */ + public function getDispatcher() + { + return $this->container->get('dispatcher'); + } /** * @@ -75,7 +98,6 @@ class Parser implements ParserInterface $this->loadParser(); echo \Thelia\Model\ConfigQuery::read("alfred", "dupont"); - \Thelia\Log\Tlog::getInstance()->debug("tutu"); return $this->content; } @@ -111,8 +133,25 @@ class Parser implements ParserInterface $this->status = $status; } + /** + * Main parser function, load the parser + */ public function loadParser() { + $content = $this->openFile($this->getRequest()); } + + public function openFile(Request $request) + { + $file = $request->attributes->get('_view'); + $fileName = THELIA_TEMPLATE_DIR . rtrim($this->template, "/") . "/" . $file . ".html"; + if (file_exists($fileName)) { + $content = file_get_contents($fileName); + } else { + throw new ResourceNotFoundException(sprintf("%s file not found in %s template", $file, $this->template)); + } + } + + } diff --git a/core/lib/Thelia/Routing/Matcher/ActionMatcher.php b/core/lib/Thelia/Routing/Matcher/ActionMatcher.php index ebd31d1f6..97ba7af59 100644 --- a/core/lib/Thelia/Routing/Matcher/ActionMatcher.php +++ b/core/lib/Thelia/Routing/Matcher/ActionMatcher.php @@ -34,7 +34,7 @@ use Thelia\Action\Cart; /** * Matcher using action param in get or post method to perform actions. For exemple index.php?action=addCart will find the good controller that can perform this action. - * + * * @author Manuel Raynaud */ @@ -42,25 +42,25 @@ class ActionMatcher implements RequestMatcherInterface { /** * - * @var Symfony\Component\EventDispatcher\EventDispatcherInterface + * @var Symfony\Component\EventDispatcher\EventDispatcherInterface */ protected $dispatcher; - + public function setDispatcher(EventDispatcherInterface $dispatcher) { $this->dispatcher = $dispatcher; } - + public function matchRequest(Request $request) { if (false !== $action = $request->get("action")) { //search corresponding action return $this->dispatchAction($request, $action); } - + throw new ResourceNotFoundException("No action parameter found"); } - + protected function dispatchAction(Request $request, $action) { $controller = null; @@ -71,7 +71,7 @@ class ActionMatcher implements RequestMatcherInterface "addCart" ); break; - default : + default : $event = new ActionEvent($request, $action); $this->dispatcher->dispatch(TheliaEvents::ACTION, $event); if ($event->hasController()) { @@ -79,14 +79,14 @@ class ActionMatcher implements RequestMatcherInterface } break; } - + if ($controller) { return array( '_controller' => $controller ); } - + throw new ResourceNotFoundException("No action parameter found"); - + } } diff --git a/templates/default/index.html b/templates/default/index.html new file mode 100644 index 000000000..6bd86f28f --- /dev/null +++ b/templates/default/index.html @@ -0,0 +1,14 @@ + + + + + + + + +
TODO write content
+ +