summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBart Visscher <bartv@thisnet.nl>2013-03-16 23:02:51 +0100
committerBart Visscher <bartv@thisnet.nl>2013-07-21 21:36:15 +0200
commit5965f3ecead424bb969261a33279dd592e5b799e (patch)
tree7d47897dbdca8fce1dd4f4fa074bd3d222d6efd7
parent37a731bcad4636f682a2cbcc59dabc9e60494b01 (diff)
downloadnextcloud-server-5965f3ecead424bb969261a33279dd592e5b799e.tar.gz
nextcloud-server-5965f3ecead424bb969261a33279dd592e5b799e.zip
Split locating JS and CSS files to their own class
-rw-r--r--lib/template/cssresourcelocator.php43
-rw-r--r--lib/template/jsresourcelocator.php43
-rw-r--r--lib/template/resourcelocator.php70
-rw-r--r--lib/templatelayout.php114
-rw-r--r--tests/lib/template/resourcelocator.php69
5 files changed, 235 insertions, 104 deletions
diff --git a/lib/template/cssresourcelocator.php b/lib/template/cssresourcelocator.php
new file mode 100644
index 00000000000..e27296d9f5f
--- /dev/null
+++ b/lib/template/cssresourcelocator.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Template;
+
+class CSSResourceLocator extends ResourceLocator {
+ public function doFind( $style ) {
+ if (strpos($style, '3rdparty') === 0
+ && $this->appendIfExist($this->thirdpartyroot, $style.'.css')
+ || $this->appendIfExist($this->serverroot, $style.$this->form_factor.'.css')
+ || $this->appendIfExist($this->serverroot, $style.'.css')
+ || $this->appendIfExist($this->serverroot, 'core/'.$style.$this->form_factor.'.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 = $this->webroot . '/index.php/apps/' . $app;
+ if ($this->appendIfExist($app_path, $style.$this->form_factor.'.css', $app_url)
+ || $this->appendIfExist($app_path, $style.'.css', $ap_url)
+ ) {
+ return;
+ }
+ throw new \Exception('css file not found: style:'.$style);
+ }
+
+ public function doFindTheme( $style ) {
+ $theme_dir = 'themes/'.$this->theme.'/';
+ $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.$this->form_factor.'.css')
+ || $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css')
+ || $this->appendIfExist($this->serverroot, $theme_dir.$style.$this->form_factor.'.css')
+ || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css')
+ || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.$this->form_factor.'.css')
+ || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css');
+ }
+}
diff --git a/lib/template/jsresourcelocator.php b/lib/template/jsresourcelocator.php
new file mode 100644
index 00000000000..a1382edf2d5
--- /dev/null
+++ b/lib/template/jsresourcelocator.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Template;
+
+class JSResourceLocator extends ResourceLocator {
+ public function doFind( $script ) {
+ $theme_dir = 'themes/'.$this->theme.'/';
+ if (strpos($script, '3rdparty') === 0
+ && $this->appendIfExist($this->thirdpartyroot, $script.'.js')
+ || $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.$this->form_factor.'.js')
+ || $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js')
+ || $this->appendIfExist($this->serverroot, $theme_dir.$script.$this->form_factor.'.js')
+ || $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js')
+ || $this->appendIfExist($this->serverroot, $script.$this->form_factor.'.js')
+ || $this->appendIfExist($this->serverroot, $script.'.js')
+ || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.$this->form_factor.'.js')
+ || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js')
+ || $this->appendIfExist($this->serverroot, 'core/'.$script.$this->form_factor.'.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);
+ if ($this->appendIfExist($app_path, $script.$this->form_factor.'.js', $app_url)
+ || $this->appendIfExist($app_path, $script.'.js', $ap_url)
+ ) {
+ return;
+ }
+ throw new \Exception('js file not found: script:'.$script);
+ }
+
+ public function doFindTheme( $script ) {
+ }
+}
diff --git a/lib/template/resourcelocator.php b/lib/template/resourcelocator.php
new file mode 100644
index 00000000000..9f83673664d
--- /dev/null
+++ b/lib/template/resourcelocator.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Template;
+
+abstract class ResourceLocator {
+ protected $theme;
+ protected $form_factor;
+
+ protected $mapping;
+ protected $serverroot;
+ protected $thirdpartyroot;
+ protected $webroot;
+
+ protected $resources = array();
+
+ public function __construct( $theme, $form_factor, $core_map, $party_map ) {
+ $this->theme = $theme;
+ $this->form_factor = $form_factor;
+ $this->mapping = $core_map + $party_map;
+ $this->serverroot = key($core_map);
+ $this->thirdpartyroot = key($party_map);
+ $this->webroot = $this->mapping[$this->serverroot];
+ }
+
+ abstract public function doFind( $resource );
+ abstract public function doFindTheme( $resource );
+
+ public function find( $resources ) {
+ try {
+ foreach($resources as $resource) {
+ $this->doFind($resource);
+ }
+ if (!empty($this->theme)) {
+ foreach($resources as $resource) {
+ $this->doFindTheme($resource);
+ }
+ }
+ } catch (\Exception $e) {
+ throw new \Exception($e->getMessage().' formfactor:'.$this->form_factor
+ .' serverroot:'.$this->serverroot);
+ }
+ }
+
+ /*
+ * @brief append the $file resource if exist at $root
+ * @param $root path to check
+ * @param $file the filename
+ * @param $web base for path, default map $root to $webroot
+ */
+ protected function appendIfExist($root, $file, $webroot = null) {
+ if (is_file($root.'/'.$file)) {
+ if (!$webroot) {
+ $webroot = $this->mapping[$root];
+ }
+ $this->resources[] = array($root, $webroot, $file);
+ return true;
+ }
+ return false;
+ }
+
+ public function getResources() {
+ return $this->resources;
+ }
+}
diff --git a/lib/templatelayout.php b/lib/templatelayout.php
index 7115b8f0306..0024c9d4960 100644
--- a/lib/templatelayout.php
+++ b/lib/templatelayout.php
@@ -83,21 +83,6 @@ class OC_TemplateLayout extends OC_Template {
}
}
- /*
- * @brief append the $file-url if exist at $root
- * @param $files array to append file info to
- * @param $root path to check
- * @param $web base for path
- * @param $file the filename
- */
- static public function appendIfExist(&$files, $root, $webroot, $file) {
- if (is_file($root.'/'.$file)) {
- $files[] = array($root, $webroot, $file);
- return true;
- }
- return false;
- }
-
static public function findStylesheetFiles($styles) {
// Read the selected theme from the config file
$theme = OC_Util::getTheme();
@@ -105,51 +90,11 @@ class OC_TemplateLayout extends OC_Template {
// Read the detected formfactor and use the right file name.
$fext = self::getFormFactorExtension();
- $files = array();
- foreach($styles as $style) {
- // is it in 3rdparty?
- if(strpos($style, '3rdparty') === 0 &&
- self::appendIfExist($files, OC::$THIRDPARTYROOT, OC::$THIRDPARTYWEBROOT, $style.'.css')) {
-
- // or in the owncloud root?
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "$style$fext.css" )) {
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "$style.css" )) {
-
- // or in core ?
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "core/$style$fext.css" )) {
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "core/$style.css" )) {
-
- }else{
- $app = substr($style, 0, strpos($style, '/'));
- $style = substr($style, strpos($style, '/')+1);
- $app_path = OC_App::getAppPath($app);
- $app_url = OC::$WEBROOT . '/index.php/apps/' . $app;
- if(self::appendIfExist($files, $app_path, $app_url, "$style$fext.css")) {
- }
- elseif(self::appendIfExist($files, $app_path, $app_url, "$style.css")) {
- }
- else {
- echo('css file not found: style:'.$style.' formfactor:'.$fext
- .' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
- die();
- }
- }
- }
- // Add the theme css files. you can override the default values here
- if(!empty($theme)) {
- foreach($styles as $style) {
- if(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$style$fext.css" )) {
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$style.css" )) {
-
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$style$fext.css" )) {
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$style.css" )) {
-
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$style$fext.css" )) {
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$style.css" )) {
- }
- }
- }
- return $files;
+ $locator = new \OC\Template\CSSResourceLocator( $theme, $fext,
+ array( OC::$SERVERROOT => OC::$WEBROOT ),
+ array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
+ $locator->find($styles);
+ return $locator->getResources();
}
static public function findJavascriptFiles($scripts) {
@@ -159,49 +104,10 @@ class OC_TemplateLayout extends OC_Template {
// Read the detected formfactor and use the right file name.
$fext = self::getFormFactorExtension();
- $files = array();
- foreach($scripts as $script) {
- // Is it in 3rd party?
- if(strpos($script, '3rdparty') === 0 &&
- self::appendIfExist($files, OC::$THIRDPARTYROOT, OC::$THIRDPARTYWEBROOT, $script.'.js')) {
-
- // Is it in apps and overwritten by the theme?
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$script$fext.js" )) {
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$script.js" )) {
-
- // Is it in the owncloud root but overwritten by the theme?
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$script$fext.js" )) {
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$script.js" )) {
-
- // Is it in the owncloud root ?
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "$script$fext.js" )) {
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "$script.js" )) {
-
- // Is in core but overwritten by a theme?
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$script$fext.js" )) {
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$script.js" )) {
-
- // Is it in core?
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "core/$script$fext.js" )) {
- }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "core/$script.js" )) {
-
- }else{
- // Is it part of an app?
- $app = substr($script, 0, strpos($script, '/'));
- $script = substr($script, strpos($script, '/')+1);
- $app_path = OC_App::getAppPath($app);
- $app_url = OC_App::getAppWebPath($app);
- if(self::appendIfExist($files, $app_path, $app_url, "$script$fext.js")) {
- }
- elseif(self::appendIfExist($files, $app_path, $app_url, "$script.js")) {
- }
- else {
- echo('js file not found: script:'.$script.' formfactor:'.$fext
- .' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
- die();
- }
- }
- }
- return $files;
+ $locator = new \OC\Template\JSResourceLocator( $theme, $fext,
+ array( OC::$SERVERROOT => OC::$WEBROOT ),
+ array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
+ $locator->find($scripts);
+ return $locator->getResources();
}
}
diff --git a/tests/lib/template/resourcelocator.php b/tests/lib/template/resourcelocator.php
new file mode 100644
index 00000000000..d80d222e2c9
--- /dev/null
+++ b/tests/lib/template/resourcelocator.php
@@ -0,0 +1,69 @@
+<?php
+/**
+ * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+class Test_ResourceLocator extends PHPUnit_Framework_TestCase {
+ public function getResourceLocator( $theme, $form_factor, $core_map, $party_map, $appsroots ) {
+ return $this->getMockForAbstractClass('OC\Template\ResourceLocator',
+ array( $theme, $form_factor, $core_map, $party_map, $appsroots ),
+ '', true, true, true, array());
+ }
+
+ public function testConstructor() {
+ $locator = $this->getResourceLocator('theme', 'form_factor',
+ array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar'));
+ $this->assertAttributeEquals('theme', 'theme', $locator);
+ $this->assertAttributeEquals('form_factor', 'form_factor', $locator);
+ $this->assertAttributeEquals('core', 'serverroot', $locator);
+ $this->assertAttributeEquals(array('core'=>'map','3rd'=>'party'), 'mapping', $locator);
+ $this->assertAttributeEquals('3rd', 'thirdpartyroot', $locator);
+ $this->assertAttributeEquals('map', 'webroot', $locator);
+ $this->assertAttributeEquals(array(), 'resources', $locator);
+ }
+
+ public function testFind() {
+ $locator = $this->getResourceLocator('theme', 'form_factor',
+ array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar'));
+ $locator->expects($this->once())
+ ->method('doFind')
+ ->with('foo');
+ $locator->expects($this->once())
+ ->method('doFindTheme')
+ ->with('foo');
+ $locator->find(array('foo'));
+
+ $locator = $this->getResourceLocator('theme', 'form_factor',
+ array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar'));
+ $locator->expects($this->once())
+ ->method('doFind')
+ ->with('foo')
+ ->will($this->throwException(new Exception('test')));
+ try {
+ $locator->find(array('foo'));
+ } catch (\Exception $e) {
+ $this->assertEquals('test formfactor:form_factor serverroot:core', $e->getMessage());
+ }
+ }
+
+ public function testAppendIfExist() {
+ $locator = $this->getResourceLocator('theme', 'form_factor',
+ array(__DIR__=>'map'), array('3rd'=>'party'), array('foo'=>'bar'));
+ $method = new ReflectionMethod($locator, 'appendIfExist');
+ $method->setAccessible(true);
+
+ $method->invoke($locator, __DIR__, basename(__FILE__), 'webroot');
+ $resource1 = array(__DIR__, 'webroot', basename(__FILE__));
+ $this->assertEquals(array($resource1), $locator->getResources());
+
+ $method->invoke($locator, __DIR__, basename(__FILE__));
+ $resource2 = array(__DIR__, 'map', basename(__FILE__));
+ $this->assertEquals(array($resource1, $resource2), $locator->getResources());
+
+ $method->invoke($locator, __DIR__, 'does-not-exist');
+ $this->assertEquals(array($resource1, $resource2), $locator->getResources());
+ }
+}