resourcelocator.php 1.7 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 $form_factor;
  12. protected $mapping;
  13. protected $serverroot;
  14. protected $thirdpartyroot;
  15. protected $webroot;
  16. protected $resources = array();
  17. public function __construct( $theme, $form_factor, $core_map, $party_map ) {
  18. $this->theme = $theme;
  19. $this->form_factor = $form_factor;
  20. $this->mapping = $core_map + $party_map;
  21. $this->serverroot = key($core_map);
  22. $this->thirdpartyroot = key($party_map);
  23. $this->webroot = $this->mapping[$this->serverroot];
  24. }
  25. abstract public function doFind( $resource );
  26. abstract public function doFindTheme( $resource );
  27. public function find( $resources ) {
  28. try {
  29. foreach($resources as $resource) {
  30. $this->doFind($resource);
  31. }
  32. if (!empty($this->theme)) {
  33. foreach($resources as $resource) {
  34. $this->doFindTheme($resource);
  35. }
  36. }
  37. } catch (\Exception $e) {
  38. throw new \Exception($e->getMessage().' formfactor:'.$this->form_factor
  39. .' serverroot:'.$this->serverroot);
  40. }
  41. }
  42. /*
  43. * @brief append the $file resource if exist at $root
  44. * @param $root path to check
  45. * @param $file the filename
  46. * @param $web 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. }