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; |
||||
} |
||||
} |
||||
@ -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