PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Zikula/Framework/Controller/AbstractController.php

https://github.com/antoniom/core
PHP | 163 lines | 62 code | 15 blank | 86 comment | 2 complexity | 02b223daf8a6aade3d64b7b31e7e1f5c MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MIT
  1. <?php
  2. /**
  3. * Copyright 2010 Zikula Foundation.
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Zikula
  10. * @subpackage \Zikula\Core\Core
  11. *
  12. * Please see the NOTICE file distributed with this source code for further
  13. * information regarding copyright and licensing.
  14. */
  15. namespace Zikula\Framework\Controller;
  16. use Zikula\Framework\AbstractBase;
  17. use Zikula\Framework\Exception\NotFoundException;
  18. use Zikula\Component\HookDispatcher\Hook;
  19. use Zikula\Core\Event\GenericEvent;
  20. use Zikula_View;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\HttpFoundation\RedirectResponse;
  23. /**
  24. * Abstract controller for modules.
  25. */
  26. abstract class AbstractController extends AbstractBase
  27. {
  28. /**
  29. * Instance of Zikula_View.
  30. *
  31. * @var Zikula_View
  32. */
  33. protected $view;
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function initialize()
  38. {
  39. $this->configureView();
  40. }
  41. /**
  42. * Create and configure the view for controllers.
  43. *
  44. * @return void
  45. */
  46. protected function configureView()
  47. {
  48. $this->setView();
  49. $this->view->setController($this);
  50. $this->view->assign('controller', $this);
  51. }
  52. /**
  53. * Set view property.
  54. *
  55. * @param Zikula_View $view Default null means new Render instance for this module name.
  56. *
  57. * @return AbstractController
  58. */
  59. protected function setView(Zikula_View $view = null)
  60. {
  61. if (is_null($view)) {
  62. $view = Zikula_View::getInstance($this->getName());
  63. }
  64. $this->view = $view;
  65. return $this;
  66. }
  67. /**
  68. * Get Zikula_View object for this controller.
  69. *
  70. * @return Zikula_View
  71. */
  72. public function getView()
  73. {
  74. return $this->view;
  75. }
  76. /**
  77. * Notify any hookable events.
  78. *
  79. * @param string $name Name of the hook event.
  80. * @param Hook $hook Hook.
  81. *
  82. * @return Hook
  83. */
  84. public function dispatchHooks($name, Hook $hook)
  85. {
  86. return $this->get('hook_dispatcher')->dispatch($name, $hook);
  87. }
  88. /**
  89. * Magic method for method_not_found events.
  90. *
  91. * @param string $method Method name called.
  92. * @param array $args Arguments passed to method call.
  93. *
  94. * @throws \Zikula_Exception_NotFound If method handler cannot be found..
  95. *
  96. * @return mixed Data.
  97. */
  98. public function __call($method, $args)
  99. {
  100. $event = new GenericEvent($this, array('method' => $method, 'args' => $args));
  101. $this->dispatcher->dispatch('controller.method_not_found', $event);
  102. if ($event->isPropagationStopped()) {
  103. return $event->getData();
  104. }
  105. throw new NotFoundException(__f('%1$s::%2$s() does not exist.', array(get_class($this), $method)));
  106. }
  107. /**
  108. * Predispatch hook, invoked just before requested controller method is dispatched.
  109. *
  110. * @return void
  111. */
  112. public function preDispatch()
  113. {
  114. }
  115. /**
  116. * Postdispatch hook, invoked just after requested controller method dispatch returns.
  117. *
  118. * @return void
  119. */
  120. public function postDispatch()
  121. {
  122. }
  123. /**
  124. * Return a response.
  125. *
  126. * @param string $content
  127. * @param integer $status
  128. * @param array $headers
  129. *
  130. * @return Response
  131. */
  132. public function response($content = '', $status = 200, $headers = array())
  133. {
  134. return new Response($content, $status, $headers);
  135. }
  136. /**
  137. * Cause redirect by throwing exception which passes to front controller.
  138. *
  139. * @param string $url Url to redirect to.
  140. * @param integer $type Redirect code, 302 default.
  141. *
  142. * @return RedirectResponse
  143. */
  144. protected function redirect($url, $type = 302)
  145. {
  146. return new RedirectResponse($url, $type);
  147. }
  148. }