Navigation.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ g+9aR/HEfb0V/s3OWo8wHeJZf6sGDt6ooiPHFmHzoYk=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.9 [rev.1.9.01]
  11. */
  12. /**
  13. * navigation application resource
  14. */
  15. namespace Cube\Application\Resource;
  16. use Cube\Navigation as NavigationObject,
  17. Cube\Loader\Autoloader;
  18. class Navigation extends AbstractResource
  19. {
  20. const CONFIG_NAMESPACE = '\\Cube\\Config\\';
  21. const DEFAULT_CONFIG = 'ArrayConfig';
  22. /**
  23. *
  24. * the type of the navigation file
  25. *
  26. * @var string
  27. */
  28. protected $_type;
  29. /**
  30. *
  31. * the location of the navigation file
  32. *
  33. * @var string
  34. */
  35. protected $_file;
  36. /**
  37. *
  38. * navigation object
  39. *
  40. * @var \Cube\Navigation
  41. */
  42. protected $_container;
  43. /**
  44. *
  45. * get navigation container
  46. *
  47. * @return \Cube\Navigation
  48. */
  49. public function getContainer()
  50. {
  51. return $this->_container;
  52. }
  53. /**
  54. *
  55. * set navigation container
  56. *
  57. * @param \Cube\Navigation $container
  58. *
  59. * @return $this
  60. * @throws \InvalidArgumentException
  61. */
  62. public function setContainer($container)
  63. {
  64. if (!$container instanceof Navigation) {
  65. throw new \InvalidArgumentException(
  66. sprintf("'%s' must be an instance of \Cube\Navigation.", $container));
  67. }
  68. $this->_container = $container;
  69. return $this;
  70. }
  71. /**
  72. *
  73. * set navigation file type
  74. *
  75. * @param string $type
  76. *
  77. * @return $this
  78. */
  79. public function setType($type)
  80. {
  81. $this->_type = $type;
  82. return $this;
  83. }
  84. /**
  85. *
  86. * set navigation data file
  87. *
  88. * @param string $file
  89. *
  90. * @return $this
  91. * @throws \InvalidArgumentException
  92. */
  93. public function setFile($file)
  94. {
  95. if (isset($file) && file_exists($file)) {
  96. $this->_file = $file;
  97. }
  98. else {
  99. throw new \InvalidArgumentException(sprintf("The navigation file '%s' does not exist.", $file));
  100. }
  101. return $this;
  102. }
  103. /**
  104. *
  105. * the resource will require for the location of a navigation file to be entered
  106. * the location of the view files corresponding to the navigation object can also be entered (optional)
  107. * if the type of the file is not entered, array will be assumed
  108. *
  109. * @1.4: if we have a navigation file in the mods folder, include it automatically as well and extend the original
  110. * navigation file
  111. *
  112. * @1.9: we retrieve all files matching the default file's extension and merge their contents
  113. *
  114. * @return \Cube\Navigation
  115. * @throws \InvalidArgumentException
  116. */
  117. public function init()
  118. {
  119. if (!($this->_container instanceof NavigationObject)) {
  120. $navigationFile = $this->_options['navigation']['data_file'];
  121. $basePath = $this->_options['paths']['base'];
  122. $pattern = str_replace('navigation.', '*.', $navigationFile);
  123. $additionalFiles = array_unique(array_merge(
  124. glob($pattern),
  125. glob(str_replace($basePath, $basePath . DIRECTORY_SEPARATOR . Autoloader::getInstance()->getModsPath(), $pattern))
  126. ));
  127. $configClass = self::CONFIG_NAMESPACE
  128. . ((isset($this->_options['navigation']['data_type'])) ?
  129. ucfirst($this->_options['navigation']['data_type']) : self::DEFAULT_CONFIG);
  130. /** @var \Cube\Config\AbstractConfig $configObject */
  131. if (class_exists($configClass)) {
  132. $configObject = new $configClass(
  133. $navigationFile);
  134. foreach ($additionalFiles as $additionalFile) {
  135. if (file_exists($additionalFile) && $additionalFile != $navigationFile) {
  136. $configObject->addData($additionalFile);
  137. }
  138. }
  139. }
  140. else {
  141. throw new \InvalidArgumentException(sprintf("Class '%s' does not exist.", $configClass));
  142. }
  143. $this->_container = new NavigationObject($configObject);
  144. if (isset($this->_options['navigation']['views_path'])) {
  145. $this->_container->setPath($this->_options['navigation']['views_path']);
  146. }
  147. }
  148. return $this->_container;
  149. }
  150. }