Blueprints.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @package Grav\Common\Data
  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\Data;
  9. use DirectoryIterator;
  10. use Grav\Common\Grav;
  11. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  12. use RuntimeException;
  13. use function is_array;
  14. use function is_object;
  15. /**
  16. * Class Blueprints
  17. * @package Grav\Common\Data
  18. */
  19. class Blueprints
  20. {
  21. /** @var array|string */
  22. protected $search;
  23. /** @var array */
  24. protected $types;
  25. /** @var array */
  26. protected $instances = [];
  27. /**
  28. * @param string|array $search Search path.
  29. */
  30. public function __construct($search = 'blueprints://')
  31. {
  32. $this->search = $search;
  33. }
  34. /**
  35. * Get blueprint.
  36. *
  37. * @param string $type Blueprint type.
  38. * @return Blueprint
  39. * @throws RuntimeException
  40. */
  41. public function get($type)
  42. {
  43. if (!isset($this->instances[$type])) {
  44. $blueprint = $this->loadFile($type);
  45. $this->instances[$type] = $blueprint;
  46. }
  47. return $this->instances[$type];
  48. }
  49. /**
  50. * Get all available blueprint types.
  51. *
  52. * @return array List of type=>name
  53. */
  54. public function types()
  55. {
  56. if ($this->types === null) {
  57. $this->types = [];
  58. $grav = Grav::instance();
  59. /** @var UniformResourceLocator $locator */
  60. $locator = $grav['locator'];
  61. // Get stream / directory iterator.
  62. if ($locator->isStream($this->search)) {
  63. $iterator = $locator->getIterator($this->search);
  64. } else {
  65. $iterator = new DirectoryIterator($this->search);
  66. }
  67. foreach ($iterator as $file) {
  68. if (!$file->isFile() || '.' . $file->getExtension() !== YAML_EXT) {
  69. continue;
  70. }
  71. $name = $file->getBasename(YAML_EXT);
  72. $this->types[$name] = ucfirst(str_replace('_', ' ', $name));
  73. }
  74. }
  75. return $this->types;
  76. }
  77. /**
  78. * Load blueprint file.
  79. *
  80. * @param string $name Name of the blueprint.
  81. * @return Blueprint
  82. */
  83. protected function loadFile($name)
  84. {
  85. $blueprint = new Blueprint($name);
  86. if (is_array($this->search) || is_object($this->search)) {
  87. // Page types.
  88. $blueprint->setOverrides($this->search);
  89. $blueprint->setContext('blueprints://pages');
  90. } else {
  91. $blueprint->setContext($this->search);
  92. }
  93. try {
  94. $blueprint->load()->init();
  95. } catch (RuntimeException $e) {
  96. $log = Grav::instance()['log'];
  97. $log->error(sprintf('Blueprint %s cannot be loaded: %s', $name, $e->getMessage()));
  98. throw $e;
  99. }
  100. return $blueprint;
  101. }
  102. }