resourcelocator.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Template;
  9. abstract class ResourceLocator {
  10. protected $theme;
  11. protected $mapping;
  12. protected $serverroot;
  13. protected $thirdpartyroot;
  14. protected $webroot;
  15. protected $resources = array();
  16. /**
  17. * @param string $theme
  18. */
  19. public function __construct( $theme, $core_map, $party_map ) {
  20. $this->theme = $theme;
  21. $this->mapping = $core_map + $party_map;
  22. $this->serverroot = key($core_map);
  23. $this->thirdpartyroot = key($party_map);
  24. $this->webroot = $this->mapping[$this->serverroot];
  25. }
  26. abstract public function doFind( $resource );
  27. abstract public function doFindTheme( $resource );
  28. public function find( $resources ) {
  29. try {
  30. foreach($resources as $resource) {
  31. $this->doFind($resource);
  32. }
  33. if (!empty($this->theme)) {
  34. foreach($resources as $resource) {
  35. $this->doFindTheme($resource);
  36. }
  37. }
  38. } catch (\Exception $e) {
  39. throw new \Exception($e->getMessage().' serverroot:'.$this->serverroot);
  40. }
  41. }
  42. /*
  43. * append the $file resource if exist at $root
  44. * @param string $root path to check
  45. * @param string $file the filename
  46. * @param string|null $webroot base for path, default map $root to $webroot
  47. */
  48. protected function appendIfExist($root, $file, $webroot = null) {
  49. if (is_file($root.'/'.$file)) {
  50. if (!$webroot) {
  51. $webroot = $this->mapping[$root];
  52. }
  53. $this->resources[] = array($root, $webroot, $file);
  54. return true;
  55. }
  56. return false;
  57. }
  58. public function getResources() {
  59. return $this->resources;
  60. }
  61. }