summaryrefslogtreecommitdiffstats
path: root/lib/private/Template
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@owncloud.com>2016-05-12 14:01:14 +0200
committerRoeland Jago Douma <rullzer@owncloud.com>2016-05-13 08:54:07 +0200
commit05cc0dd47881816ef9c91c8883dafc0716cd6d6c (patch)
tree28f05d7e7c09a3656911a9af4965f41b2d573d8f /lib/private/Template
parenteb79b8383107c71f8def60d195de09a1152af263 (diff)
downloadnextcloud-server-05cc0dd47881816ef9c91c8883dafc0716cd6d6c.tar.gz
nextcloud-server-05cc0dd47881816ef9c91c8883dafc0716cd6d6c.zip
Move \OC\Template to PSR-4
Diffstat (limited to 'lib/private/Template')
-rw-r--r--lib/private/Template/Base.php184
-rw-r--r--lib/private/Template/CSSResourceLocator.php55
-rw-r--r--lib/private/Template/JSResourceLocator.php78
-rw-r--r--lib/private/Template/ResourceLocator.php135
-rw-r--r--lib/private/Template/ResourceNotFoundException.php45
-rw-r--r--lib/private/Template/TemplateFileLocator.php61
6 files changed, 558 insertions, 0 deletions
diff --git a/lib/private/Template/Base.php b/lib/private/Template/Base.php
new file mode 100644
index 00000000000..cfe629b5fbf
--- /dev/null
+++ b/lib/private/Template/Base.php
@@ -0,0 +1,184 @@
+<?php
+/**
+ * @author Bart Visscher <bartv@thisnet.nl>
+ * @author Björn Schießle <schiessle@owncloud.com>
+ * @author Christopher Schäpers <kondou@ts.unde.re>
+ * @author Jörn Friedrich Dreyer <jfd@butonic.de>
+ * @author Lukas Reschke <lukas@owncloud.com>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OC\Template;
+
+class Base {
+ private $template; // The template
+ private $vars; // Vars
+
+ /** @var \OCP\IL10N */
+ private $l10n;
+
+ /** @var \OC_Defaults */
+ private $theme;
+
+ /**
+ * @param string $template
+ * @param string $requestToken
+ * @param \OCP\IL10N $l10n
+ * @param \OC_Defaults $theme
+ */
+ public function __construct($template, $requestToken, $l10n, $theme ) {
+ $this->vars = array();
+ $this->vars['requesttoken'] = $requestToken;
+ $this->l10n = $l10n;
+ $this->template = $template;
+ $this->theme = $theme;
+ }
+
+ /**
+ * @param string $serverRoot
+ * @param string|false $app_dir
+ * @param string $theme
+ * @param string $app
+ * @return string[]
+ */
+ protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
+ // Check if the app is in the app folder or in the root
+ if( file_exists($app_dir.'/templates/' )) {
+ return [
+ $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
+ $app_dir.'/templates/',
+ ];
+ }
+ return [
+ $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/',
+ $serverRoot.'/'.$app.'/templates/',
+ ];
+ }
+
+ /**
+ * @param string $serverRoot
+ * @param string $theme
+ * @return string[]
+ */
+ protected function getCoreTemplateDirs($theme, $serverRoot) {
+ return [
+ $serverRoot.'/themes/'.$theme.'/core/templates/',
+ $serverRoot.'/core/templates/',
+ ];
+ }
+
+ /**
+ * Assign variables
+ * @param string $key key
+ * @param array|bool|integer|string $value value
+ * @return bool
+ *
+ * This function assigns a variable. It can be accessed via $_[$key] in
+ * the template.
+ *
+ * If the key existed before, it will be overwritten
+ */
+ public function assign( $key, $value) {
+ $this->vars[$key] = $value;
+ return true;
+ }
+
+ /**
+ * Appends a variable
+ * @param string $key key
+ * @param mixed $value value
+ * @return boolean|null
+ *
+ * This function assigns a variable in an array context. If the key already
+ * exists, the value will be appended. It can be accessed via
+ * $_[$key][$position] in the template.
+ */
+ public function append( $key, $value ) {
+ if( array_key_exists( $key, $this->vars )) {
+ $this->vars[$key][] = $value;
+ }
+ else{
+ $this->vars[$key] = array( $value );
+ }
+ }
+
+ /**
+ * Prints the proceeded template
+ * @return bool
+ *
+ * This function proceeds the template and prints its output.
+ */
+ public function printPage() {
+ $data = $this->fetchPage();
+ if( $data === false ) {
+ return false;
+ }
+ else{
+ print $data;
+ return true;
+ }
+ }
+
+ /**
+ * Process the template
+ *
+ * @param array|null $additionalParams
+ * @return string This function processes the template.
+ *
+ * This function processes the template.
+ */
+ public function fetchPage($additionalParams = null) {
+ return $this->load($this->template, $additionalParams);
+ }
+
+ /**
+ * doing the actual work
+ *
+ * @param string $file
+ * @param array|null $additionalParams
+ * @return string content
+ *
+ * Includes the template file, fetches its output
+ */
+ protected function load($file, $additionalParams = null) {
+ // Register the variables
+ $_ = $this->vars;
+ $l = $this->l10n;
+ $theme = $this->theme;
+
+ if( !is_null($additionalParams)) {
+ $_ = array_merge( $additionalParams, $this->vars );
+ }
+
+ // Include
+ ob_start();
+ try {
+ include $file;
+ $data = ob_get_contents();
+ } catch (\Exception $e) {
+ @ob_end_clean();
+ throw $e;
+ }
+ @ob_end_clean();
+
+ // Return data
+ return $data;
+ }
+
+}
diff --git a/lib/private/Template/CSSResourceLocator.php b/lib/private/Template/CSSResourceLocator.php
new file mode 100644
index 00000000000..6a547931ee3
--- /dev/null
+++ b/lib/private/Template/CSSResourceLocator.php
@@ -0,0 +1,55 @@
+<?php
+/**
+ * @author Bart Visscher <bartv@thisnet.nl>
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OC\Template;
+
+class CSSResourceLocator extends ResourceLocator {
+ /**
+ * @param string $style
+ */
+ public function doFind($style) {
+ if (strpos($style, '3rdparty') === 0
+ && $this->appendIfExist($this->thirdpartyroot, $style.'.css')
+ || $this->appendIfExist($this->serverroot, $style.'.css')
+ || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css')
+ ) {
+ return;
+ }
+ $app = substr($style, 0, strpos($style, '/'));
+ $style = substr($style, strpos($style, '/')+1);
+ $app_path = \OC_App::getAppPath($app);
+ $app_url = \OC_App::getAppWebPath($app);
+ $this->append($app_path, $style.'.css', $app_url);
+ }
+
+ /**
+ * @param string $style
+ */
+ public function doFindTheme($style) {
+ $theme_dir = 'themes/'.$this->theme.'/';
+ $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css')
+ || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css')
+ || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css');
+ }
+}
diff --git a/lib/private/Template/JSResourceLocator.php b/lib/private/Template/JSResourceLocator.php
new file mode 100644
index 00000000000..6ea7b6291c0
--- /dev/null
+++ b/lib/private/Template/JSResourceLocator.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * @author Bart Visscher <bartv@thisnet.nl>
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OC\Template;
+
+class JSResourceLocator extends ResourceLocator {
+ /**
+ * @param string $script
+ */
+ public function doFind($script) {
+ $theme_dir = 'themes/'.$this->theme.'/';
+ if (strpos($script, '3rdparty') === 0
+ && $this->appendIfExist($this->thirdpartyroot, $script.'.js')) {
+ return;
+ }
+
+ if (strpos($script, '/l10n/') !== false) {
+ // For language files we try to load them all, so themes can overwrite
+ // single l10n strings without having to translate all of them.
+ $found = 0;
+ $found += $this->appendIfExist($this->serverroot, 'core/'.$script.'.js');
+ $found += $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js');
+ $found += $this->appendIfExist($this->serverroot, $script.'.js');
+ $found += $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js');
+ $found += $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js');
+
+ if ($found) {
+ return;
+ }
+ } else if ($this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js')
+ || $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js')
+ || $this->appendIfExist($this->serverroot, $script.'.js')
+ || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js')
+ || $this->appendIfExist($this->serverroot, 'core/'.$script.'.js')
+ ) {
+ return;
+ }
+
+ $app = substr($script, 0, strpos($script, '/'));
+ $script = substr($script, strpos($script, '/')+1);
+ $app_path = \OC_App::getAppPath($app);
+ $app_url = \OC_App::getAppWebPath($app);
+
+ // missing translations files fill be ignored
+ if (strpos($script, 'l10n/') === 0) {
+ $this->appendIfExist($app_path, $script . '.js', $app_url);
+ return;
+ }
+ $this->append($app_path, $script . '.js', $app_url);
+ }
+
+ /**
+ * @param string $script
+ */
+ public function doFindTheme($script) {
+ }
+}
diff --git a/lib/private/Template/ResourceLocator.php b/lib/private/Template/ResourceLocator.php
new file mode 100644
index 00000000000..e64fce81afc
--- /dev/null
+++ b/lib/private/Template/ResourceLocator.php
@@ -0,0 +1,135 @@
+<?php
+/**
+ * @author Bart Visscher <bartv@thisnet.nl>
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Jörn Friedrich Dreyer <jfd@butonic.de>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Robin McCorkell <robin@mccorkell.me.uk>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OC\Template;
+
+abstract class ResourceLocator {
+ protected $theme;
+
+ protected $mapping;
+ protected $serverroot;
+ protected $thirdpartyroot;
+ protected $webroot;
+
+ protected $resources = array();
+
+ /** @var \OCP\ILogger */
+ protected $logger;
+
+ /**
+ * @param \OCP\ILogger $logger
+ * @param string $theme
+ * @param array $core_map
+ * @param array $party_map
+ */
+ public function __construct(\OCP\ILogger $logger, $theme, $core_map, $party_map) {
+ $this->logger = $logger;
+ $this->theme = $theme;
+ $this->mapping = $core_map + $party_map;
+ $this->serverroot = key($core_map);
+ $this->thirdpartyroot = key($party_map);
+ $this->webroot = $this->mapping[$this->serverroot];
+ }
+
+ /**
+ * @param string $resource
+ */
+ abstract public function doFind($resource);
+
+ /**
+ * @param string $resource
+ */
+ abstract public function doFindTheme($resource);
+
+ /**
+ * Finds the resources and adds them to the list
+ *
+ * @param array $resources
+ */
+ public function find($resources) {
+ foreach ($resources as $resource) {
+ try {
+ $this->doFind($resource);
+ } catch (ResourceNotFoundException $e) {
+ $resourceApp = substr($resource, 0, strpos($resource, '/'));
+ $this->logger->error('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
+ }
+ }
+ if (!empty($this->theme)) {
+ foreach ($resources as $resource) {
+ try {
+ $this->doFindTheme($resource);
+ } catch (ResourceNotFoundException $e) {
+ $resourceApp = substr($resource, 0, strpos($resource, '/'));
+ $this->logger->error('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
+ }
+ }
+ }
+ }
+
+ /**
+ * append the $file resource if exist at $root
+ *
+ * @param string $root path to check
+ * @param string $file the filename
+ * @param string|null $webRoot base for path, default map $root to $webRoot
+ * @return bool True if the resource was found, false otherwise
+ */
+ protected function appendIfExist($root, $file, $webRoot = null) {
+ if (is_file($root.'/'.$file)) {
+ $this->append($root, $file, $webRoot, false);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * append the $file resource at $root
+ *
+ * @param string $root path to check
+ * @param string $file the filename
+ * @param string|null $webRoot base for path, default map $root to $webRoot
+ * @param bool $throw Throw an exception, when the route does not exist
+ * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing
+ */
+ protected function append($root, $file, $webRoot = null, $throw = true) {
+ if (!$webRoot) {
+ $webRoot = $this->mapping[$root];
+ }
+ $this->resources[] = array($root, $webRoot, $file);
+
+ if ($throw && !is_file($root . '/' . $file)) {
+ throw new ResourceNotFoundException($file, $webRoot);
+ }
+ }
+
+ /**
+ * Returns the list of all resources that should be loaded
+ * @return array
+ */
+ public function getResources() {
+ return $this->resources;
+ }
+}
diff --git a/lib/private/Template/ResourceNotFoundException.php b/lib/private/Template/ResourceNotFoundException.php
new file mode 100644
index 00000000000..8c7f1f14175
--- /dev/null
+++ b/lib/private/Template/ResourceNotFoundException.php
@@ -0,0 +1,45 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OC\Template;
+
+class ResourceNotFoundException extends \LogicException {
+ protected $resource;
+ protected $webPath;
+
+ /**
+ * @param string $resource
+ * @param string $webPath
+ */
+ public function __construct($resource, $webPath) {
+ parent::__construct('Resource not found');
+ $this->resource = $resource;
+ $this->webPath = $webPath;
+ }
+
+ /**
+ * @return string
+ */
+ public function getResourcePath() {
+ return $this->webPath . '/' . $this->resource;
+ }
+}
diff --git a/lib/private/Template/TemplateFileLocator.php b/lib/private/Template/TemplateFileLocator.php
new file mode 100644
index 00000000000..f8553156914
--- /dev/null
+++ b/lib/private/Template/TemplateFileLocator.php
@@ -0,0 +1,61 @@
+<?php
+/**
+ * @author Bart Visscher <bartv@thisnet.nl>
+ * @author Jörn Friedrich Dreyer <jfd@butonic.de>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OC\Template;
+
+class TemplateFileLocator {
+ protected $dirs;
+ private $path;
+
+ /**
+ * @param string[] $dirs
+ */
+ public function __construct( $dirs ) {
+ $this->dirs = $dirs;
+ }
+
+ /**
+ * @param string $template
+ * @return string
+ * @throws \Exception
+ */
+ public function find( $template ) {
+ if ($template === '') {
+ throw new \InvalidArgumentException('Empty template name');
+ }
+
+ foreach($this->dirs as $dir) {
+ $file = $dir.$template.'.php';
+ if (is_file($file)) {
+ $this->path = $dir;
+ return $file;
+ }
+ }
+ throw new \Exception('template file not found: template:'.$template);
+ }
+
+ public function getPath() {
+ return $this->path;
+ }
+}