Split locating JS and CSS files to their own class
This commit is contained in:
parent
37a731bcad
commit
5965f3ecea
|
@ -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');
|
||||
}
|
||||
}
|
|
@ -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 ) {
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue