Types.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @package Grav\Common\Page
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 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\Filesystem\Folder;
  10. use Grav\Common\Grav;
  11. use RocketTheme\Toolbox\ArrayTraits\ArrayAccess;
  12. use RocketTheme\Toolbox\ArrayTraits\Constructor;
  13. use RocketTheme\Toolbox\ArrayTraits\Countable;
  14. use RocketTheme\Toolbox\ArrayTraits\Export;
  15. use RocketTheme\Toolbox\ArrayTraits\Iterator;
  16. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  17. class Types implements \ArrayAccess, \Iterator, \Countable
  18. {
  19. use ArrayAccess, Constructor, Iterator, Countable, Export;
  20. protected $items;
  21. protected $systemBlueprints;
  22. public function register($type, $blueprint = null)
  23. {
  24. if (!isset($this->items[$type])) {
  25. $this->items[$type] = [];
  26. } elseif (!$blueprint) {
  27. return;
  28. }
  29. if (!$blueprint && $this->systemBlueprints) {
  30. $blueprint = $this->systemBlueprints[$type] ?? $this->systemBlueprints['default'];
  31. }
  32. if ($blueprint) {
  33. array_unshift($this->items[$type], $blueprint);
  34. }
  35. }
  36. public function scanBlueprints($uri)
  37. {
  38. if (!\is_string($uri)) {
  39. throw new \InvalidArgumentException('First parameter must be URI');
  40. }
  41. if (!$this->systemBlueprints) {
  42. $this->systemBlueprints = $this->findBlueprints('blueprints://pages');
  43. // Register default by default.
  44. $this->register('default');
  45. $this->register('external');
  46. }
  47. foreach ($this->findBlueprints($uri) as $type => $blueprint) {
  48. $this->register($type, $blueprint);
  49. }
  50. }
  51. public function scanTemplates($uri)
  52. {
  53. if (!\is_string($uri)) {
  54. throw new \InvalidArgumentException('First parameter must be URI');
  55. }
  56. $options = [
  57. 'compare' => 'Filename',
  58. 'pattern' => '|\.html\.twig$|',
  59. 'filters' => [
  60. 'value' => '|\.html\.twig$|'
  61. ],
  62. 'value' => 'Filename',
  63. 'recursive' => false
  64. ];
  65. foreach (Folder::all($uri, $options) as $type) {
  66. $this->register($type);
  67. }
  68. $modular_uri = rtrim($uri, '/') . '/modular';
  69. if (is_dir($modular_uri)) {
  70. foreach (Folder::all($modular_uri, $options) as $type) {
  71. $this->register('modular/' . $type);
  72. }
  73. }
  74. }
  75. public function pageSelect()
  76. {
  77. $list = [];
  78. foreach ($this->items as $name => $file) {
  79. if (strpos($name, '/')) {
  80. continue;
  81. }
  82. $list[$name] = ucfirst(str_replace('_', ' ', $name));
  83. }
  84. ksort($list);
  85. return $list;
  86. }
  87. public function modularSelect()
  88. {
  89. $list = [];
  90. foreach ($this->items as $name => $file) {
  91. if (strpos($name, 'modular/') !== 0) {
  92. continue;
  93. }
  94. $list[$name] = ucfirst(trim(str_replace('_', ' ', basename($name))));
  95. }
  96. ksort($list);
  97. return $list;
  98. }
  99. private function findBlueprints($uri)
  100. {
  101. $options = [
  102. 'compare' => 'Filename',
  103. 'pattern' => '|\.yaml$|',
  104. 'filters' => [
  105. 'key' => '|\.yaml$|'
  106. ],
  107. 'key' => 'SubPathName',
  108. 'value' => 'PathName',
  109. ];
  110. /** @var UniformResourceLocator $locator */
  111. $locator = Grav::instance()['locator'];
  112. if ($locator->isStream($uri)) {
  113. $options['value'] = 'Url';
  114. }
  115. return Folder::all($uri, $options);
  116. }
  117. }