ViewComponent.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Elgg\Debug\Inspector;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * @access private
  7. *
  8. * @package Elgg.Core
  9. * @since 1.11
  10. */
  11. class ViewComponent {
  12. /**
  13. * @var string View location. E.g. "/path/to/views/default/"
  14. */
  15. public $location;
  16. /**
  17. * @var string View name. E.g. "css/elgg"
  18. */
  19. public $view;
  20. /**
  21. * @var string View file extension, if known. E.g. "php"
  22. */
  23. public $extension = null;
  24. /**
  25. * @var bool Is this component represent an overridden view?
  26. */
  27. public $overridden = false;
  28. /**
  29. * @return string Return the component as a file path
  30. */
  31. public function getFile() {
  32. $ext = pathinfo($this->view, PATHINFO_EXTENSION);
  33. if ($ext) {
  34. // view is filename
  35. return "{$this->location}{$this->view}";
  36. }
  37. $str = "{$this->location}{$this->view}.{$this->extension}";
  38. if ($this->extension === null) {
  39. // try to guess from filesystem
  40. $files = glob("{$this->location}{$this->view}.*");
  41. if (count($files) === 1) {
  42. $str = $files[0];
  43. } else {
  44. $str = "{$this->location}{$this->view}.?";
  45. }
  46. }
  47. return $str;
  48. }
  49. /**
  50. * Get a component from the path and location
  51. *
  52. * @param string $path Full file path
  53. * @param string $location Base location of view
  54. *
  55. * @return ViewComponent
  56. */
  57. public static function fromPaths($path, $location) {
  58. $component = new self();
  59. $component->location = $location;
  60. // cut location off
  61. $file = substr($path, strlen($location));
  62. $component->file = $file;
  63. $basename = basename($file);
  64. $period = strpos($basename, '.');
  65. if ($period === false) {
  66. // file with no extension? shouldn't happen
  67. $component->view = $file;
  68. $component->extension = '';
  69. } else {
  70. $cut_off_end = strlen($basename) - $period;
  71. $component->view = substr($file, 0, -$cut_off_end);
  72. $component->extension = substr($basename, $period + 1);
  73. }
  74. return $component;
  75. }
  76. }