PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel/laravel/section.php

http://github.com/eryx/php-framework-benchmark
PHP | 136 lines | 55 code | 12 blank | 69 comment | 3 complexity | ac10c4941823cb62f807941f5b8cb9bf MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  1. <?php namespace Laravel;
  2. class Section {
  3. /**
  4. * All of the captured sections.
  5. *
  6. * @var array
  7. */
  8. public static $sections = array();
  9. /**
  10. * The last section on which injection was started.
  11. *
  12. * @var array
  13. */
  14. public static $last = array();
  15. /**
  16. * Start injecting content into a section.
  17. *
  18. * <code>
  19. * // Start injecting into the "header" section
  20. * Section::start('header');
  21. *
  22. * // Inject a raw string into the "header" section without buffering
  23. * Section::start('header', '<title>Laravel</title>');
  24. * </code>
  25. *
  26. * @param string $section
  27. * @param string|Closure $content
  28. * @return void
  29. */
  30. public static function start($section, $content = '')
  31. {
  32. if ($content === '')
  33. {
  34. ob_start() and static::$last[] = $section;
  35. }
  36. else
  37. {
  38. static::extend($section, $content);
  39. }
  40. }
  41. /**
  42. * Inject inline content into a section.
  43. *
  44. * This is helpful for injecting simple strings such as page titles.
  45. *
  46. * <code>
  47. * // Inject inline content into the "header" section
  48. * Section::inject('header', '<title>Laravel</title>');
  49. * </code>
  50. *
  51. * @param string $section
  52. * @param string $content
  53. * @return void
  54. */
  55. public static function inject($section, $content)
  56. {
  57. static::start($section, $content);
  58. }
  59. /**
  60. * Stop injecting content into a section and return its contents.
  61. *
  62. * @return string
  63. */
  64. public static function yield_section()
  65. {
  66. return static::yield(static::stop());
  67. }
  68. /**
  69. * Stop injecting content into a section.
  70. *
  71. * @return string
  72. */
  73. public static function stop()
  74. {
  75. static::extend($last = array_pop(static::$last), ob_get_clean());
  76. return $last;
  77. }
  78. /**
  79. * Extend the content in a given section.
  80. *
  81. * @param string $section
  82. * @param string $content
  83. * @return void
  84. */
  85. protected static function extend($section, $content)
  86. {
  87. if (isset(static::$sections[$section]))
  88. {
  89. static::$sections[$section] = str_replace('@parent', $content, static::$sections[$section]);
  90. }
  91. else
  92. {
  93. static::$sections[$section] = $content;
  94. }
  95. }
  96. /**
  97. * Append content to a given section.
  98. *
  99. * @param string $section
  100. * @param string $content
  101. * @return void
  102. */
  103. public static function append($section, $content)
  104. {
  105. if (isset(static::$sections[$section]))
  106. {
  107. static::$sections[$section] .= $content;
  108. }
  109. else
  110. {
  111. static::$sections[$section] = $content;
  112. }
  113. }
  114. /**
  115. * Get the string contents of a section.
  116. *
  117. * @param string $section
  118. * @return string
  119. */
  120. public static function yield($section)
  121. {
  122. return (isset(static::$sections[$section])) ? static::$sections[$section] : '';
  123. }
  124. }