Types.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @package Grav\Common\Page
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Page;
  9. use Grav\Common\Data\Blueprint;
  10. use Grav\Common\Filesystem\Folder;
  11. use Grav\Common\Grav;
  12. use Grav\Common\Utils;
  13. use InvalidArgumentException;
  14. use RocketTheme\Toolbox\ArrayTraits\ArrayAccess;
  15. use RocketTheme\Toolbox\ArrayTraits\Constructor;
  16. use RocketTheme\Toolbox\ArrayTraits\Countable;
  17. use RocketTheme\Toolbox\ArrayTraits\Export;
  18. use RocketTheme\Toolbox\ArrayTraits\Iterator;
  19. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  20. use function is_string;
  21. /**
  22. * Class Types
  23. * @package Grav\Common\Page
  24. */
  25. class Types implements \ArrayAccess, \Iterator, \Countable
  26. {
  27. use ArrayAccess, Constructor, Iterator, Countable, Export;
  28. /** @var array */
  29. protected $items;
  30. /** @var array */
  31. protected $systemBlueprints = [];
  32. /**
  33. * @param string $type
  34. * @param Blueprint|null $blueprint
  35. * @return void
  36. */
  37. public function register($type, $blueprint = null)
  38. {
  39. if (!isset($this->items[$type])) {
  40. $this->items[$type] = [];
  41. } elseif (null === $blueprint) {
  42. return;
  43. }
  44. if (null === $blueprint) {
  45. $blueprint = $this->systemBlueprints[$type] ?? $this->systemBlueprints['default'] ?? null;
  46. }
  47. if ($blueprint) {
  48. array_unshift($this->items[$type], $blueprint);
  49. }
  50. }
  51. /**
  52. * @return void
  53. */
  54. public function init()
  55. {
  56. if (empty($this->systemBlueprints)) {
  57. // Register all blueprints from the blueprints stream.
  58. $this->systemBlueprints = $this->findBlueprints('blueprints://pages');
  59. foreach ($this->systemBlueprints as $type => $blueprint) {
  60. $this->register($type);
  61. }
  62. }
  63. }
  64. /**
  65. * @param string $uri
  66. * @return void
  67. */
  68. public function scanBlueprints($uri)
  69. {
  70. if (!is_string($uri)) {
  71. throw new InvalidArgumentException('First parameter must be URI');
  72. }
  73. foreach ($this->findBlueprints($uri) as $type => $blueprint) {
  74. $this->register($type, $blueprint);
  75. }
  76. }
  77. /**
  78. * @param string $uri
  79. * @return void
  80. */
  81. public function scanTemplates($uri)
  82. {
  83. if (!is_string($uri)) {
  84. throw new InvalidArgumentException('First parameter must be URI');
  85. }
  86. $options = [
  87. 'compare' => 'Filename',
  88. 'pattern' => '|\.html\.twig$|',
  89. 'filters' => [
  90. 'value' => '|\.html\.twig$|'
  91. ],
  92. 'value' => 'Filename',
  93. 'recursive' => false
  94. ];
  95. foreach (Folder::all($uri, $options) as $type) {
  96. $this->register($type);
  97. }
  98. $modular_uri = rtrim($uri, '/') . '/modular';
  99. if (is_dir($modular_uri)) {
  100. foreach (Folder::all($modular_uri, $options) as $type) {
  101. $this->register('modular/' . $type);
  102. }
  103. }
  104. }
  105. /**
  106. * @return array
  107. */
  108. public function pageSelect()
  109. {
  110. $list = [];
  111. foreach ($this->items as $name => $file) {
  112. if (strpos($name, '/')) {
  113. continue;
  114. }
  115. $list[$name] = ucfirst(str_replace('_', ' ', $name));
  116. }
  117. ksort($list);
  118. return $list;
  119. }
  120. /**
  121. * @return array
  122. */
  123. public function modularSelect()
  124. {
  125. $list = [];
  126. foreach ($this->items as $name => $file) {
  127. if (strpos($name, 'modular/') !== 0) {
  128. continue;
  129. }
  130. $list[$name] = ucfirst(trim(str_replace('_', ' ', Utils::basename($name))));
  131. }
  132. ksort($list);
  133. return $list;
  134. }
  135. /**
  136. * @param string $uri
  137. * @return array
  138. */
  139. private function findBlueprints($uri)
  140. {
  141. $options = [
  142. 'compare' => 'Filename',
  143. 'pattern' => '|\.yaml$|',
  144. 'filters' => [
  145. 'key' => '|\.yaml$|'
  146. ],
  147. 'key' => 'SubPathName',
  148. 'value' => 'PathName',
  149. ];
  150. /** @var UniformResourceLocator $locator */
  151. $locator = Grav::instance()['locator'];
  152. if ($locator->isStream($uri)) {
  153. $options['value'] = 'Url';
  154. }
  155. return Folder::all($uri, $options);
  156. }
  157. }