PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/cms/classes/PartialStack.php

https://gitlab.com/gideonmarked/yovelife
PHP | 93 lines | 52 code | 12 blank | 29 comment | 6 complexity | 2e67d1a6c774ec0d70072204e4a413a3 MD5 | raw file
  1. <?php namespace Cms\Classes;
  2. /**
  3. * Manager class for stacking nested partials and keeping track
  4. * of their components. Partial "objects" store the components
  5. * used by that partial for deferred retrieval.
  6. *
  7. * @package october\cms
  8. * @author Alexey Bobkov, Samuel Georges
  9. */
  10. class PartialStack
  11. {
  12. /**
  13. * @var array The current partial "object" being rendered.
  14. */
  15. public $activePartial = null;
  16. /**
  17. * @var array Collection of previously rendered partial "objects".
  18. */
  19. protected $partialStack = [];
  20. /**
  21. * Partial entry point, appends a new partial to the stack.
  22. */
  23. public function stackPartial()
  24. {
  25. if ($this->activePartial !== null) {
  26. array_unshift($this->partialStack, $this->activePartial);
  27. }
  28. $this->activePartial = [
  29. 'components' => []
  30. ];
  31. }
  32. /**
  33. * Partial exit point, removes the active partial from the stack.
  34. */
  35. public function unstackPartial()
  36. {
  37. $this->activePartial = array_shift($this->partialStack);
  38. }
  39. /**
  40. * Adds a component to the active partial stack.
  41. */
  42. public function addComponent($alias, $componentObj)
  43. {
  44. array_push($this->activePartial['components'], [
  45. 'name' => $alias,
  46. 'obj' => $componentObj
  47. ]);
  48. }
  49. /**
  50. * Returns a component by its alias from the partial stack.
  51. */
  52. public function getComponent($name)
  53. {
  54. if (!$this->activePartial) {
  55. return null;
  56. }
  57. $component = $this->findComponentFromStack($name, $this->activePartial);
  58. if ($component !== null) {
  59. return $component;
  60. }
  61. foreach ($this->partialStack as $stack) {
  62. $component = $this->findComponentFromStack($name, $stack);
  63. if ($component !== null) {
  64. return $component;
  65. }
  66. }
  67. return null;
  68. }
  69. /**
  70. * Locates a component by its alias from the supplied stack.
  71. */
  72. protected function findComponentFromStack($name, $stack)
  73. {
  74. foreach ($stack['components'] as $componentInfo) {
  75. if ($componentInfo['name'] == $name) {
  76. return $componentInfo['obj'];
  77. }
  78. }
  79. return null;
  80. }
  81. }