diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2015-03-04 13:24:24 +0100 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2015-03-04 17:56:33 +0100 |
commit | 716ba49a82ab7c0a68f3b4f442ea8ed84c06f317 (patch) | |
tree | 26357cce2f08a3682435d2af931e44b2da371d5f | |
parent | f507601e259da2e6b67237ffcfe8eeda880062ef (diff) | |
download | nextcloud-server-716ba49a82ab7c0a68f3b4f442ea8ed84c06f317.tar.gz nextcloud-server-716ba49a82ab7c0a68f3b4f442ea8ed84c06f317.zip |
Log errors and create 404 in network list when a css or js is missing
-rw-r--r-- | lib/private/template/cssresourcelocator.php | 16 | ||||
-rw-r--r-- | lib/private/template/jsresourcelocator.php | 19 | ||||
-rw-r--r-- | lib/private/template/resourcelocator.php | 82 | ||||
-rw-r--r-- | lib/private/template/resourcenotfoundexception.php | 35 | ||||
-rw-r--r-- | lib/private/templatelayout.php | 8 | ||||
-rw-r--r-- | tests/lib/template/resourcelocator.php | 27 |
6 files changed, 146 insertions, 41 deletions
diff --git a/lib/private/template/cssresourcelocator.php b/lib/private/template/cssresourcelocator.php index cb129261b51..7fcb3d02ba9 100644 --- a/lib/private/template/cssresourcelocator.php +++ b/lib/private/template/cssresourcelocator.php @@ -9,7 +9,10 @@ namespace OC\Template; class CSSResourceLocator extends ResourceLocator { - public function doFind( $style ) { + /** + * @param string $style + */ + public function doFind($style) { if (strpos($style, '3rdparty') === 0 && $this->appendIfExist($this->thirdpartyroot, $style.'.css') || $this->appendIfExist($this->serverroot, $style.'.css') @@ -21,14 +24,13 @@ class CSSResourceLocator extends ResourceLocator { $style = substr($style, strpos($style, '/')+1); $app_path = \OC_App::getAppPath($app); $app_url = \OC_App::getAppWebPath($app); - if ($this->appendIfExist($app_path, $style.'.css', $app_url) - ) { - return; - } - throw new \Exception('css file not found: style:'.$style); + $this->append($app_path, $style.'.css', $app_url); } - public function doFindTheme( $style ) { + /** + * @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') diff --git a/lib/private/template/jsresourcelocator.php b/lib/private/template/jsresourcelocator.php index 5a6672429cf..6ddc5e6ad7d 100644 --- a/lib/private/template/jsresourcelocator.php +++ b/lib/private/template/jsresourcelocator.php @@ -9,7 +9,10 @@ namespace OC\Template; class JSResourceLocator extends ResourceLocator { - public function doFind( $script ) { + /** + * @param string $script + */ + public function doFind($script) { $theme_dir = 'themes/'.$this->theme.'/'; if (strpos($script, '3rdparty') === 0 && $this->appendIfExist($this->thirdpartyroot, $script.'.js') @@ -25,16 +28,18 @@ class JSResourceLocator extends ResourceLocator { $script = substr($script, strpos($script, '/')+1); $app_path = \OC_App::getAppPath($app); $app_url = \OC_App::getAppWebPath($app); - if ($this->appendIfExist($app_path, $script.'.js', $app_url)) { - return; - } + // missing translations files fill be ignored - if (strpos($script, "l10n/") === 0) { + if (strpos($script, 'l10n/') === 0) { + $this->appendIfExist($app_path, $script . '.js', $app_url); return; } - throw new \Exception('js file not found: script:'.$script); + $this->append($app_path, $script . '.js', $app_url); } - public function doFindTheme( $script ) { + /** + * @param string $script + */ + public function doFindTheme($script) { } } diff --git a/lib/private/template/resourcelocator.php b/lib/private/template/resourcelocator.php index 919665df704..cf64f331cc9 100644 --- a/lib/private/template/resourcelocator.php +++ b/lib/private/template/resourcelocator.php @@ -18,10 +18,17 @@ abstract class ResourceLocator { 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( $theme, $core_map, $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); @@ -29,41 +36,82 @@ abstract class ResourceLocator { $this->webroot = $this->mapping[$this->serverroot]; } - abstract public function doFind( $resource ); - abstract public function doFindTheme( $resource ); + /** + * @param string $resource + */ + abstract public function doFind($resource); + + /** + * @param string $resource + */ + abstract public function doFindTheme($resource); - public function find( $resources ) { - try { - foreach($resources as $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) { + } + 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]); } } - } catch (\Exception $e) { - throw new \Exception($e->getMessage().' serverroot:'.$this->serverroot); } } - /* + /** * 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 + * @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) { + protected function appendIfExist($root, $file, $webRoot = null) { if (is_file($root.'/'.$file)) { - if (!$webroot) { - $webroot = $this->mapping[$root]; - } - $this->resources[] = array($root, $webroot, $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..8a728fce2f1 --- /dev/null +++ b/lib/private/template/resourcenotfoundexception.php @@ -0,0 +1,35 @@ +<?php +/** + * ownCloud + * + * @author Joas Schilling + * @copyright 2015 Joas Schilling nickvergessen@owncloud.com + * + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +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->resource . '/' . $this->webPath; + } +} diff --git a/lib/private/templatelayout.php b/lib/private/templatelayout.php index 6f948e38437..3220d9d969c 100644 --- a/lib/private/templatelayout.php +++ b/lib/private/templatelayout.php @@ -135,7 +135,9 @@ class OC_TemplateLayout extends OC_Template { // Read the selected theme from the config file $theme = OC_Util::getTheme(); - $locator = new \OC\Template\CSSResourceLocator( $theme, + $locator = new \OC\Template\CSSResourceLocator( + OC::$server->getLogger(), + $theme, array( OC::$SERVERROOT => OC::$WEBROOT ), array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT )); $locator->find($styles); @@ -150,7 +152,9 @@ class OC_TemplateLayout extends OC_Template { // Read the selected theme from the config file $theme = OC_Util::getTheme(); - $locator = new \OC\Template\JSResourceLocator( $theme, + $locator = new \OC\Template\JSResourceLocator( + OC::$server->getLogger(), + $theme, array( OC::$SERVERROOT => OC::$WEBROOT ), array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT )); $locator->find($scripts); diff --git a/tests/lib/template/resourcelocator.php b/tests/lib/template/resourcelocator.php index f350fd144e1..b0851621fd2 100644 --- a/tests/lib/template/resourcelocator.php +++ b/tests/lib/template/resourcelocator.php @@ -7,13 +7,20 @@ */ class Test_ResourceLocator extends \Test\TestCase { + /** @var PHPUnit_Framework_MockObject_MockObject */ + protected $logger; + + protected function setUp() { + parent::setUp(); + $this->logger = $this->getMock('OCP\ILogger'); + } /** * @param string $theme */ public function getResourceLocator( $theme, $core_map, $party_map, $appsroots ) { return $this->getMockForAbstractClass('OC\Template\ResourceLocator', - array( $theme, $core_map, $party_map, $appsroots ), + array($this->logger, $theme, $core_map, $party_map, $appsroots ), '', true, true, true, array()); } @@ -30,7 +37,7 @@ class Test_ResourceLocator extends \Test\TestCase { public function testFind() { $locator = $this->getResourceLocator('theme', - array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar')); + array('core' => 'map'), array('3rd' => 'party'), array('foo' => 'bar')); $locator->expects($this->once()) ->method('doFind') ->with('foo'); @@ -38,18 +45,22 @@ class Test_ResourceLocator extends \Test\TestCase { ->method('doFindTheme') ->with('foo'); $locator->find(array('foo')); + } + public function testFindNotFound() { $locator = $this->getResourceLocator('theme', 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 serverroot:core', $e->getMessage()); - } + ->will($this->throwException(new \OC\Template\ResourceNotFoundException('foo', 'map'))); + $locator->expects($this->once()) + ->method('doFindTheme') + ->with('foo') + ->will($this->throwException(new \OC\Template\ResourceNotFoundException('foo', 'map'))); + $this->logger->expects($this->exactly(2)) + ->method('error'); + $locator->find(array('foo')); } public function testAppendIfExist() { |