From 8c0f19c7971e1a4534347ce9d6d82a0a45799711 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Mon, 3 Dec 2018 00:46:04 +0100 Subject: namespacing: \Shaarli\Render\{PageBuilder,ThemeUtils} Signed-off-by: VirtualTam --- application/PageBuilder.php | 205 ----------------------------------- application/ThemeUtils.php | 34 ------ application/render/PageBuilder.php | 213 +++++++++++++++++++++++++++++++++++++ application/render/ThemeUtils.php | 34 ++++++ composer.json | 1 + index.php | 4 +- tests/ThemeUtilsTest.php | 55 ---------- tests/render/ThemeUtilsTest.php | 55 ++++++++++ 8 files changed, 305 insertions(+), 296 deletions(-) delete mode 100644 application/PageBuilder.php delete mode 100644 application/ThemeUtils.php create mode 100644 application/render/PageBuilder.php create mode 100644 application/render/ThemeUtils.php delete mode 100644 tests/ThemeUtilsTest.php create mode 100644 tests/render/ThemeUtilsTest.php diff --git a/application/PageBuilder.php b/application/PageBuilder.php deleted file mode 100644 index 2ca95832..00000000 --- a/application/PageBuilder.php +++ /dev/null @@ -1,205 +0,0 @@ -assign('myfield','myvalue'); - * $p->renderPage('mytemplate'); - */ -class PageBuilder -{ - /** - * @var RainTPL RainTPL instance. - */ - private $tpl; - - /** - * @var ConfigManager $conf Configuration Manager instance. - */ - protected $conf; - - /** - * @var array $_SESSION - */ - protected $session; - - /** - * @var LinkDB $linkDB instance. - */ - protected $linkDB; - - /** - * @var null|string XSRF token - */ - protected $token; - - /** @var bool $isLoggedIn Whether the user is logged in **/ - protected $isLoggedIn = false; - - /** - * PageBuilder constructor. - * $tpl is initialized at false for lazy loading. - * - * @param ConfigManager $conf Configuration Manager instance (reference). - * @param array $session $_SESSION array - * @param LinkDB $linkDB instance. - * @param string $token Session token - * @param bool $isLoggedIn - */ - public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false) - { - $this->tpl = false; - $this->conf = $conf; - $this->session = $session; - $this->linkDB = $linkDB; - $this->token = $token; - $this->isLoggedIn = $isLoggedIn; - } - - /** - * Initialize all default tpl tags. - */ - private function initialize() - { - $this->tpl = new RainTPL(); - - try { - $version = ApplicationUtils::checkUpdate( - SHAARLI_VERSION, - $this->conf->get('resource.update_check'), - $this->conf->get('updates.check_updates_interval'), - $this->conf->get('updates.check_updates'), - $this->isLoggedIn, - $this->conf->get('updates.check_updates_branch') - ); - $this->tpl->assign('newVersion', escape($version)); - $this->tpl->assign('versionError', ''); - } catch (Exception $exc) { - logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage()); - $this->tpl->assign('newVersion', ''); - $this->tpl->assign('versionError', escape($exc->getMessage())); - } - - $this->tpl->assign('is_logged_in', $this->isLoggedIn); - $this->tpl->assign('feedurl', escape(index_url($_SERVER))); - $searchcrits = ''; // Search criteria - if (!empty($_GET['searchtags'])) { - $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']); - } - if (!empty($_GET['searchterm'])) { - $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']); - } - $this->tpl->assign('searchcrits', $searchcrits); - $this->tpl->assign('source', index_url($_SERVER)); - $this->tpl->assign('version', SHAARLI_VERSION); - $this->tpl->assign( - 'version_hash', - ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt')) - ); - $this->tpl->assign('index_url', index_url($_SERVER)); - $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : ''; - $this->tpl->assign('visibility', $visibility); - $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly'])); - $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli')); - if ($this->conf->exists('general.header_link')) { - $this->tpl->assign('titleLink', $this->conf->get('general.header_link')); - } - $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli')); - $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false)); - $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true)); - $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss'); - $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false)); - $this->tpl->assign('token', $this->token); - - if ($this->linkDB !== null) { - $this->tpl->assign('tags', $this->linkDB->linksCountPerTag()); - } - - $this->tpl->assign( - 'thumbnails_enabled', - $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE - ); - $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width')); - $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height')); - - if (! empty($_SESSION['warnings'])) { - $this->tpl->assign('global_warnings', $_SESSION['warnings']); - unset($_SESSION['warnings']); - } - - // To be removed with a proper theme configuration. - $this->tpl->assign('conf', $this->conf); - } - - /** - * The following assign() method is basically the same as RainTPL (except lazy loading) - * - * @param string $placeholder Template placeholder. - * @param mixed $value Value to assign. - */ - public function assign($placeholder, $value) - { - if ($this->tpl === false) { - $this->initialize(); - } - $this->tpl->assign($placeholder, $value); - } - - /** - * Assign an array of data to the template builder. - * - * @param array $data Data to assign. - * - * @return false if invalid data. - */ - public function assignAll($data) - { - if ($this->tpl === false) { - $this->initialize(); - } - - if (empty($data) || !is_array($data)) { - return false; - } - - foreach ($data as $key => $value) { - $this->assign($key, $value); - } - return true; - } - - /** - * Render a specific page (using a template file). - * e.g. $pb->renderPage('picwall'); - * - * @param string $page Template filename (without extension). - */ - public function renderPage($page) - { - if ($this->tpl === false) { - $this->initialize(); - } - - $this->tpl->draw($page); - } - - /** - * Render a 404 page (uses the template : tpl/404.tpl) - * usage : $PAGE->render404('The link was deleted') - * - * @param string $message A messate to display what is not found - */ - public function render404($message = '') - { - if (empty($message)) { - $message = t('The page you are trying to reach does not exist or has been deleted.'); - } - header($_SERVER['SERVER_PROTOCOL'] .' '. t('404 Not Found')); - $this->tpl->assign('error_message', $message); - $this->renderPage('404'); - } -} diff --git a/application/ThemeUtils.php b/application/ThemeUtils.php deleted file mode 100644 index 16f2f6a2..00000000 --- a/application/ThemeUtils.php +++ /dev/null @@ -1,34 +0,0 @@ -assign('myfield','myvalue'); + * $p->renderPage('mytemplate'); + */ +class PageBuilder +{ + /** + * @var RainTPL RainTPL instance. + */ + private $tpl; + + /** + * @var ConfigManager $conf Configuration Manager instance. + */ + protected $conf; + + /** + * @var array $_SESSION + */ + protected $session; + + /** + * @var LinkDB $linkDB instance. + */ + protected $linkDB; + + /** + * @var null|string XSRF token + */ + protected $token; + + /** + * @var bool $isLoggedIn Whether the user is logged in + */ + protected $isLoggedIn = false; + + /** + * PageBuilder constructor. + * $tpl is initialized at false for lazy loading. + * + * @param ConfigManager $conf Configuration Manager instance (reference). + * @param array $session $_SESSION array + * @param LinkDB $linkDB instance. + * @param string $token Session token + * @param bool $isLoggedIn + */ + public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false) + { + $this->tpl = false; + $this->conf = $conf; + $this->session = $session; + $this->linkDB = $linkDB; + $this->token = $token; + $this->isLoggedIn = $isLoggedIn; + } + + /** + * Initialize all default tpl tags. + */ + private function initialize() + { + $this->tpl = new RainTPL(); + + try { + $version = ApplicationUtils::checkUpdate( + SHAARLI_VERSION, + $this->conf->get('resource.update_check'), + $this->conf->get('updates.check_updates_interval'), + $this->conf->get('updates.check_updates'), + $this->isLoggedIn, + $this->conf->get('updates.check_updates_branch') + ); + $this->tpl->assign('newVersion', escape($version)); + $this->tpl->assign('versionError', ''); + } catch (Exception $exc) { + logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage()); + $this->tpl->assign('newVersion', ''); + $this->tpl->assign('versionError', escape($exc->getMessage())); + } + + $this->tpl->assign('is_logged_in', $this->isLoggedIn); + $this->tpl->assign('feedurl', escape(index_url($_SERVER))); + $searchcrits = ''; // Search criteria + if (!empty($_GET['searchtags'])) { + $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']); + } + if (!empty($_GET['searchterm'])) { + $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']); + } + $this->tpl->assign('searchcrits', $searchcrits); + $this->tpl->assign('source', index_url($_SERVER)); + $this->tpl->assign('version', SHAARLI_VERSION); + $this->tpl->assign( + 'version_hash', + ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt')) + ); + $this->tpl->assign('index_url', index_url($_SERVER)); + $visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : ''; + $this->tpl->assign('visibility', $visibility); + $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly'])); + $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli')); + if ($this->conf->exists('general.header_link')) { + $this->tpl->assign('titleLink', $this->conf->get('general.header_link')); + } + $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli')); + $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false)); + $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true)); + $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss'); + $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false)); + $this->tpl->assign('token', $this->token); + + if ($this->linkDB !== null) { + $this->tpl->assign('tags', $this->linkDB->linksCountPerTag()); + } + + $this->tpl->assign( + 'thumbnails_enabled', + $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE + ); + $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width')); + $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height')); + + if (!empty($_SESSION['warnings'])) { + $this->tpl->assign('global_warnings', $_SESSION['warnings']); + unset($_SESSION['warnings']); + } + + // To be removed with a proper theme configuration. + $this->tpl->assign('conf', $this->conf); + } + + /** + * The following assign() method is basically the same as RainTPL (except lazy loading) + * + * @param string $placeholder Template placeholder. + * @param mixed $value Value to assign. + */ + public function assign($placeholder, $value) + { + if ($this->tpl === false) { + $this->initialize(); + } + $this->tpl->assign($placeholder, $value); + } + + /** + * Assign an array of data to the template builder. + * + * @param array $data Data to assign. + * + * @return false if invalid data. + */ + public function assignAll($data) + { + if ($this->tpl === false) { + $this->initialize(); + } + + if (empty($data) || !is_array($data)) { + return false; + } + + foreach ($data as $key => $value) { + $this->assign($key, $value); + } + return true; + } + + /** + * Render a specific page (using a template file). + * e.g. $pb->renderPage('picwall'); + * + * @param string $page Template filename (without extension). + */ + public function renderPage($page) + { + if ($this->tpl === false) { + $this->initialize(); + } + + $this->tpl->draw($page); + } + + /** + * Render a 404 page (uses the template : tpl/404.tpl) + * usage: $PAGE->render404('The link was deleted') + * + * @param string $message A message to display what is not found + */ + public function render404($message = '') + { + if (empty($message)) { + $message = t('The page you are trying to reach does not exist or has been deleted.'); + } + header($_SERVER['SERVER_PROTOCOL'] . ' ' . t('404 Not Found')); + $this->tpl->assign('error_message', $message); + $this->renderPage('404'); + } +} diff --git a/application/render/ThemeUtils.php b/application/render/ThemeUtils.php new file mode 100644 index 00000000..86096c64 --- /dev/null +++ b/application/render/ThemeUtils.php @@ -0,0 +1,34 @@ +assertTrue(in_array($theme, $themes)); - } - $this->assertFalse(in_array('supertheme', $res)); - - foreach ($themes as $theme) { - rmdir('sandbox/tpl/'. $theme); - } - unlink('sandbox/tpl/supertheme'); - rmdir('sandbox/tpl'); - } - - /** - * Test getThemes() without any theme dir. - */ - public function testGetThemesEmpty() - { - mkdir('sandbox/tpl/', 0755, true); - $this->assertEquals([], ThemeUtils::getThemes('sandbox/tpl/')); - rmdir('sandbox/tpl/'); - } - - /** - * Test getThemes() with an invalid path. - */ - public function testGetThemesInvalid() - { - $this->assertEquals([], ThemeUtils::getThemes('nope')); - } -} diff --git a/tests/render/ThemeUtilsTest.php b/tests/render/ThemeUtilsTest.php new file mode 100644 index 00000000..6159a1bd --- /dev/null +++ b/tests/render/ThemeUtilsTest.php @@ -0,0 +1,55 @@ +assertTrue(in_array($theme, $themes)); + } + $this->assertFalse(in_array('supertheme', $res)); + + foreach ($themes as $theme) { + rmdir('sandbox/tpl/'. $theme); + } + unlink('sandbox/tpl/supertheme'); + rmdir('sandbox/tpl'); + } + + /** + * Test getThemes() without any theme dir. + */ + public function testGetThemesEmpty() + { + mkdir('sandbox/tpl/', 0755, true); + $this->assertEquals([], ThemeUtils::getThemes('sandbox/tpl/')); + rmdir('sandbox/tpl/'); + } + + /** + * Test getThemes() with an invalid path. + */ + public function testGetThemesInvalid() + { + $this->assertEquals([], ThemeUtils::getThemes('nope')); + } +} -- cgit v1.2.3