From 06158693c7b846518abfe9d72491fc7376e457f3 Mon Sep 17 00:00:00 2001 From: David King Date: Fri, 19 Oct 2012 19:54:19 -0400 Subject: [feature/controller] Implement a front controller PHPBB3-10864 --- phpBB/includes/controller/helper.php | 142 +++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 phpBB/includes/controller/helper.php (limited to 'phpBB/includes/controller/helper.php') diff --git a/phpBB/includes/controller/helper.php b/phpBB/includes/controller/helper.php new file mode 100644 index 0000000000..1998ea6733 --- /dev/null +++ b/phpBB/includes/controller/helper.php @@ -0,0 +1,142 @@ +container = $container; + + $this->template = $this->container->get('template'); + $this->phpbb_root_path = $this->container->getParameter('core.root_path'); + $this->php_ext = $this->container->getParameter('core.php_ext'); + } + + /** + * Automate setting up the page and creating the response object. + * + * @param string $handle The template handle to render + * @param string $page_title The title of the page to output + * @param int $status_code The status code to be sent to the page header + * @return Response object containing rendered page + */ + public function render($template_file, $page_title = '', $status_code = 200) + { + if (!function_exists('page_header')) + { + include("{$this->phpbb_root_path}includes/functions.{$this->php_ext}"); + } + + page_header($page_title); + + $this->template->set_filenames(array( + 'body' => $template_file, + )); + + page_footer(true, false, false); + + return new Response($this->template->return_display('body'), $status_code); + } + + /** + * Easily generate a URL + * + * @param array $url_parts Each array element is a 'folder' + * i.e. array('my', 'ext') maps to ./app.php/my/ext + * @param mixed $query The Query string, passed directly into the second + * argument of append_sid() + * @return string A URL that has already been run through append_sid() + */ + public function url(array $url_parts, $query = '') + { + return append_sid($this->phpbb_root_path . $this->url_base . implode('/', $url_parts), $query); + } + + /** + * Set base to prepend to urls generated by url() + * This allows extensions to have a certain 'directory' under which + * all their pages are served, but not have to type it every time + * + * @param array $url_parts Each array element is a 'folder' + * i.e. array('my', 'ext') maps to ./app.php/my/ext + * @return null + */ + public function set_url_base(array $url_parts) + { + $this->url_base = !empty($url_parts) ? implode('/', $url_parts) . '/' : ''; + } + + /** + * Output an error, effectively the same thing as trigger_error + * + * @param string $code The error code (e.g. 404, 500, 503, etc.) + * @param string $message The error message + * @return Response A Reponse instance + */ + public function error($code = 500, $message = '') + { + $this->template->assign_vars(array( + 'MESSAGE_TEXT' => $message, + 'MESSAGE_TITLE' => $this->container->get('user')->lang('INFORMATION'), + )); + + return $this->render('message_body.html', $this->container->get('user')->lang('INFORMATION'), $code); + } +} -- cgit v1.2.1 From 4e1f17a87dc6fefa30becc594a7f41e7f6293cad Mon Sep 17 00:00:00 2001 From: David King Date: Sun, 11 Nov 2012 14:17:31 -0500 Subject: [feature/controller] includes/functions.php is included by default PHPBB3-10864 --- phpBB/includes/controller/helper.php | 5 ----- 1 file changed, 5 deletions(-) (limited to 'phpBB/includes/controller/helper.php') diff --git a/phpBB/includes/controller/helper.php b/phpBB/includes/controller/helper.php index 1998ea6733..f3127fd1fc 100644 --- a/phpBB/includes/controller/helper.php +++ b/phpBB/includes/controller/helper.php @@ -79,11 +79,6 @@ class phpbb_controller_helper */ public function render($template_file, $page_title = '', $status_code = 200) { - if (!function_exists('page_header')) - { - include("{$this->phpbb_root_path}includes/functions.{$this->php_ext}"); - } - page_header($page_title); $this->template->set_filenames(array( -- cgit v1.2.1 From 7687f069611b35f81c6d0fc87e46b9bb0821d616 Mon Sep 17 00:00:00 2001 From: David King Date: Tue, 13 Nov 2012 09:26:31 -0500 Subject: [feature/controller] Inject dependencies instead of container PHPBB3-10864 --- phpBB/includes/controller/helper.php | 42 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'phpBB/includes/controller/helper.php') diff --git a/phpBB/includes/controller/helper.php b/phpBB/includes/controller/helper.php index f3127fd1fc..0fc3ba0c67 100644 --- a/phpBB/includes/controller/helper.php +++ b/phpBB/includes/controller/helper.php @@ -15,9 +15,7 @@ if (!defined('IN_PHPBB')) exit; } -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\DependencyInjection\ContainerBuilder; /** * Controller helper class, contains methods that do things for controllers @@ -25,12 +23,6 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; */ class phpbb_controller_helper { - /** - * Container - * @var ContainerBuilder - */ - protected $container; - /** * Template object * @var phpbb_template @@ -38,13 +30,19 @@ class phpbb_controller_helper protected $template; /** - * phpBB Root Path + * User object + * @var phpbb_user + */ + protected $user; + + /** + * phpBB root path * @var string */ - protected $phpbb_root_path; + protected $root_path; /** - * PHP Extension + * PHP extension * @var string */ protected $php_ext; @@ -58,15 +56,17 @@ class phpbb_controller_helper /** * Constructor * - * @param ContainerBuilder $container DI Container + * @param phpbb_template $template Template object + * @param phpbb_user $user User object + * @param string $root_path phpBB root path + * @param string $php_ext PHP extension */ - public function __construct(ContainerBuilder $container) + public function __construct(phpbb_template $template, phpbb_user $user, $root_path, $php_ext) { - $this->container = $container; - - $this->template = $this->container->get('template'); - $this->phpbb_root_path = $this->container->getParameter('core.root_path'); - $this->php_ext = $this->container->getParameter('core.php_ext'); + $this->template = $template; + $this->user = $user; + $this->root_path = $root_path; + $this->php_ext = $php_ext; } /** @@ -101,7 +101,7 @@ class phpbb_controller_helper */ public function url(array $url_parts, $query = '') { - return append_sid($this->phpbb_root_path . $this->url_base . implode('/', $url_parts), $query); + return append_sid($this->root_path . $this->url_base . implode('/', $url_parts), $query); } /** @@ -129,9 +129,9 @@ class phpbb_controller_helper { $this->template->assign_vars(array( 'MESSAGE_TEXT' => $message, - 'MESSAGE_TITLE' => $this->container->get('user')->lang('INFORMATION'), + 'MESSAGE_TITLE' => $this->user->lang('INFORMATION'), )); - return $this->render('message_body.html', $this->container->get('user')->lang('INFORMATION'), $code); + return $this->render('message_body.html', $this->user->lang('INFORMATION'), $code); } } -- cgit v1.2.1 From 2e594504596eaf6ab758240a1cd012dfea79fe77 Mon Sep 17 00:00:00 2001 From: David King Date: Tue, 13 Nov 2012 10:42:20 -0500 Subject: [feature/controller] Use assign_display() instead of return_display() The latter was deemed unnecessary at the moment. PHPBB3-10864 --- phpBB/includes/controller/helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/controller/helper.php') diff --git a/phpBB/includes/controller/helper.php b/phpBB/includes/controller/helper.php index 0fc3ba0c67..8bb4427382 100644 --- a/phpBB/includes/controller/helper.php +++ b/phpBB/includes/controller/helper.php @@ -87,7 +87,7 @@ class phpbb_controller_helper page_footer(true, false, false); - return new Response($this->template->return_display('body'), $status_code); + return new Response($this->template->assign_display('body'), $status_code); } /** -- cgit v1.2.1 From b4eff4f06acce78536a392b733f2438f4d1d1c52 Mon Sep 17 00:00:00 2001 From: David King Date: Thu, 15 Nov 2012 13:54:41 -0500 Subject: [feature/controller] Remove URL Base from helper class I had forgotten that the container sends the same instance of objects to all services that request it, so in this case all controllers would share the same base url path, which is not desired. PHPBB3-10864 --- phpBB/includes/controller/helper.php | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) (limited to 'phpBB/includes/controller/helper.php') diff --git a/phpBB/includes/controller/helper.php b/phpBB/includes/controller/helper.php index 8bb4427382..6b697c7e46 100644 --- a/phpBB/includes/controller/helper.php +++ b/phpBB/includes/controller/helper.php @@ -47,12 +47,6 @@ class phpbb_controller_helper */ protected $php_ext; - /** - * Base URL - * @var array - */ - protected $url_base; - /** * Constructor * @@ -101,21 +95,7 @@ class phpbb_controller_helper */ public function url(array $url_parts, $query = '') { - return append_sid($this->root_path . $this->url_base . implode('/', $url_parts), $query); - } - - /** - * Set base to prepend to urls generated by url() - * This allows extensions to have a certain 'directory' under which - * all their pages are served, but not have to type it every time - * - * @param array $url_parts Each array element is a 'folder' - * i.e. array('my', 'ext') maps to ./app.php/my/ext - * @return null - */ - public function set_url_base(array $url_parts) - { - $this->url_base = !empty($url_parts) ? implode('/', $url_parts) . '/' : ''; + return append_sid($this->root_path . implode('/', $url_parts), $query); } /** -- cgit v1.2.1 From d41b1146e8947919ed9bdd41e6a30ad15e91d262 Mon Sep 17 00:00:00 2001 From: David King Date: Fri, 16 Nov 2012 10:37:07 -0500 Subject: [feature/controller] Rename $root_path class property to $phpbb_root_path PHPBB3-10864 --- phpBB/includes/controller/helper.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'phpBB/includes/controller/helper.php') diff --git a/phpBB/includes/controller/helper.php b/phpBB/includes/controller/helper.php index 6b697c7e46..6dc3ec4626 100644 --- a/phpBB/includes/controller/helper.php +++ b/phpBB/includes/controller/helper.php @@ -39,7 +39,7 @@ class phpbb_controller_helper * phpBB root path * @var string */ - protected $root_path; + protected $phpbb_root_path; /** * PHP extension @@ -52,14 +52,14 @@ class phpbb_controller_helper * * @param phpbb_template $template Template object * @param phpbb_user $user User object - * @param string $root_path phpBB root path + * @param string $phpbb_root_path phpBB root path * @param string $php_ext PHP extension */ - public function __construct(phpbb_template $template, phpbb_user $user, $root_path, $php_ext) + public function __construct(phpbb_template $template, phpbb_user $user, $phpbb_root_path, $php_ext) { $this->template = $template; $this->user = $user; - $this->root_path = $root_path; + $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; } @@ -95,7 +95,7 @@ class phpbb_controller_helper */ public function url(array $url_parts, $query = '') { - return append_sid($this->root_path . implode('/', $url_parts), $query); + return append_sid($this->phpbb_root_path . implode('/', $url_parts), $query); } /** -- cgit v1.2.1 From 1952a4f276930edd16ca51fd690ed3e7965071da Mon Sep 17 00:00:00 2001 From: David King Date: Fri, 16 Nov 2012 10:38:40 -0500 Subject: [feature/controller] Flip method parameters, require $message PHPBB3-10864 --- phpBB/includes/controller/helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/controller/helper.php') diff --git a/phpBB/includes/controller/helper.php b/phpBB/includes/controller/helper.php index 6dc3ec4626..6cacc8fefa 100644 --- a/phpBB/includes/controller/helper.php +++ b/phpBB/includes/controller/helper.php @@ -101,11 +101,11 @@ class phpbb_controller_helper /** * Output an error, effectively the same thing as trigger_error * - * @param string $code The error code (e.g. 404, 500, 503, etc.) * @param string $message The error message + * @param string $code The error code (e.g. 404, 500, 503, etc.) * @return Response A Reponse instance */ - public function error($code = 500, $message = '') + public function error($message, $code = 500) { $this->template->assign_vars(array( 'MESSAGE_TEXT' => $message, -- cgit v1.2.1