Types.php 4.4 KB

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