[Beispiel] Block System

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • [Beispiel] Block System

      Hallo liebes MCore Team,
      wie schon angedroht werde ich versuchen den Webcoding Bereich etwas aufleben zu lassen.

      Da ich gestern von einem Kollegen gefragt wurde wie man ein Blocksystem in PHP realisieren könnte welches dynamisch auf eventuelle Gegebenheiten reagiert.

      Lange Rede kurze Sinn. Ich habe es mit Ihm zusammen gebaut und wollte es euch nicht vorenthalten.

      Was ist ein Block System?

      Das Block System wird benötigt, um aus einem Controller oder wo auch immer, diverse Parameter oder ganze Programmteile nachzuladen.

      Das ganze System ist natürlich weder ausgereift, noch out of box nutzbar. Es ging für meinen Kollegen viel mehr darum einen Anhaltspunkt zuhaben und zu verstehen wie man eine Klasse bauen kann, welche ohne komplett definierte Methoden, dennoch dynamisch benutzt werden kann.

      Was bedeutet ohne definierte Methoden?

      Ohne fefinierte Methoden bedeutet, das wir die PHP eigene Magic Methode

      Quellcode

      1. __callStatic
      benutzen werden.

      Wie wird die Klasse benutzt?

      Die Klasse kann man ganz einfach in ein bestehendes System integrieren und muss noch eine Anpassung bezüglich der Verarbeitung der Blöcke machen.

      Code der Klasse

      PHP-Quellcode

      1. <?php
      2. class Block {
      3. /**
      4. * Beinhaltete alle Sections
      5. *
      6. * @var array
      7. */
      8. private static $sections=array();
      9. /**
      10. * Dynamischer Aufruf von nicht definierten Methodennamen
      11. *
      12. * @param string $method
      13. * @param array $args
      14. * @return void
      15. */
      16. public static function __callStatic($method, $args=array())
      17. {
      18. array_set(static::$sections, $method, array());
      19. foreach($args as $k => $v)
      20. {
      21. if(is_array($v))
      22. {
      23. foreach($v as $key => $value)
      24. {
      25. array_set(static::$sections[$method], $key, $value);
      26. }
      27. }
      28. }
      29. }
      30. /**
      31. * Aufruf einer Section egal in welcher Tiefe
      32. *
      33. * @param string $section
      34. * @param string $default
      35. * @return string|array
      36. */
      37. public static function get($section=false, $default=null)
      38. {
      39. if(!$section)
      40. {
      41. return static::$sections;
      42. }
      43. else
      44. {
      45. return array_get(static::$sections, $section, $default);
      46. }
      47. }
      48. }
      Alles anzeigen


      Beispiel Implementation

      PHP-Quellcode

      1. <?php
      2. require 'helpers.php';
      3. require 'blocks.php';
      4. Block::sidebar(array(
      5. array('social.facebook' => 'social/facebook'),
      6. array('social.twitter' => 'social/twitter')
      7. ));
      8. // Wenn man nun einen Block Segment auslesen will, dann reicht es wenn man das Array mit der Dot Syntax durchläuft
      9. Block::get(); // Gibt alle definierten Blöcke als Array aus
      10. Block::get('sidebar'); // Gibt alle Blöcke der Sidebar Kategorie aus als Array wenn es mehrere sind
      11. Block::get('sidebar.social'); // Alle Blöcke der Kategorie Sidebar und Unterkategorie Social als Array wenn es mehrere sind
      12. Block::get('sidebar.social.facebook'); // Gibt alle Blöcke der Kategorie Sidebar, Social, Facebook aus
      13. // Ich hoffe ich konnte mit dieser Erklärung alles klarstellen
      Alles anzeigen


      Helfer Datei damit die Array Dot Syntax funktioniert, diese wurde aus dem Laravel Framework übernommen

      PHP-Quellcode

      1. <?php
      2. /**
      3. * Get an item from an array using "dot" notation.
      4. *
      5. * <code>
      6. * // Get the $array['user]['name] value from the array
      7. * $name=array_get($array, 'user.name');
      8. *
      9. * // Return a default from if the specified item doesn't exist
      10. * $name=array_get($array, 'user.name', 'Taylor');
      11. * </code>
      12. *
      13. * @param array $array
      14. * @param string $key
      15. * @param mixed $default
      16. * @return mixed
      17. */
      18. function array_get($array, $key, $default=null)
      19. {
      20. if (is_null($key)) return $array;
      21. // To retrieve the array item using dot syntax, we'll iterate through
      22. // each segment in the key and look for that value. If it exists, we
      23. // will return it, otherwise we will set the depth of the array and
      24. // look for the next segment.
      25. foreach (explode('.', $key) as $segment)
      26. {
      27. if ( ! is_array($array) or ! array_key_exists($segment, $array))
      28. {
      29. return value($default);
      30. }
      31. $array=$array[$segment];
      32. }
      33. return $array;
      34. }
      35. /**
      36. * Set an array item to a given value using "dot" notation.
      37. *
      38. * If no key is given to the method, the entire array will be replaced.
      39. *
      40. * <code>
      41. * // Set the $array['user]['name] value on the array
      42. * array_set($array, 'user.name', 'Taylor');
      43. *
      44. * // Set the $array['user]['name]['first] value on the array
      45. * array_set($array, 'user.name.first', 'Michael');
      46. * </code>
      47. *
      48. * @param array $array
      49. * @param string $key
      50. * @param mixed $value
      51. * @return void
      52. */
      53. function array_set(&$array, $key, $value)
      54. {
      55. if (is_null($key)) return $array=$value;
      56. $keys=explode('.', $key);
      57. // This loop allows us to dig down into the array to a dynamic depth by
      58. // setting the array value for each level that we dig into. Once there
      59. // is one key left, we can fall out of the loop and set the value as
      60. // we should be at the proper depth.
      61. while (count($keys) > 1)
      62. {
      63. $key=array_shift($keys);
      64. // If the key doesn't exist at this depth, we will just create an
      65. // empty array to hold the next value, allowing us to create the
      66. // arrays to hold the final value.
      67. if ( ! isset($array[$key]) or ! is_array($array[$key]))
      68. {
      69. $array[$key]=array();
      70. }
      71. $array =& $array[$key];
      72. }
      73. $array[array_shift($keys)]=$value;
      74. }
      75. /**
      76. * Remove an array item from a given array using "dot" notation.
      77. *
      78. * <code>
      79. * // Remove the $array['user]['name] item from the array
      80. * array_forget($array, 'user.name');
      81. *
      82. * // Remove the $array['user]['name]['first] item from the array
      83. * array_forget($array, 'user.name.first');
      84. * </code>
      85. *
      86. * @param array $array
      87. * @param string $key
      88. * @return void
      89. */
      90. function array_forget(&$array, $key)
      91. {
      92. $keys=explode('.', $key);
      93. // This loop functions very similarly to the loop in the "set" method.
      94. // We will iterate over the keys, setting the array value to the new
      95. // depth at each iteration. Once there is only one key left, we will
      96. // be at the proper depth in the array.
      97. while (count($keys) > 1)
      98. {
      99. $key=array_shift($keys);
      100. // Since this method is supposed to remove a value from the array,
      101. // if a value higher up in the chain doesn't exist, there is no
      102. // need to keep digging into the array, since it is impossible
      103. // for the final value to even exist.
      104. if ( ! isset($array[$key]) or ! is_array($array[$key]))
      105. {
      106. return;
      107. }
      108. $array =& $array[$key];
      109. }
      110. unset($array[array_shift($keys)]);
      111. }
      112. /**
      113. * Return the first element in an array which passes a given truth test.
      114. *
      115. * <code>
      116. * // Return the first array element that equals "Taylor"
      117. * $value=array_first($array, function($k, $v) {return $v == 'Taylor';});
      118. *
      119. * // Return a default value if no matching element is found
      120. * $value=array_first($array, function($k, $v) {return $v == 'Taylor'}, 'Default');
      121. * </code>
      122. *
      123. * @param array $array
      124. * @param Closure $callback
      125. * @param mixed $default
      126. * @return mixed
      127. */
      128. function array_first($array, $callback, $default=null)
      129. {
      130. foreach ($array as $key => $value)
      131. {
      132. if (call_user_func($callback, $key, $value)) return $value;
      133. }
      134. return value($default);
      135. }
      136. /**
      137. * Recursively remove slashes from array keys and values.
      138. *
      139. * @param array $array
      140. * @return array
      141. */
      142. function array_strip_slashes($array)
      143. {
      144. $result=array();
      145. foreach($array as $key => $value)
      146. {
      147. $key=stripslashes($key);
      148. // If the value is an array, we will just recurse back into the
      149. // function to keep stripping the slashes out of the array,
      150. // otherwise we will set the stripped value.
      151. if (is_array($value))
      152. {
      153. $result[$key]=array_strip_slashes($value);
      154. }
      155. else
      156. {
      157. $result[$key]=stripslashes($value);
      158. }
      159. }
      160. return $result;
      161. }
      162. /**
      163. * Divide an array into two arrays. One with keys and the other with values.
      164. *
      165. * @param array $array
      166. * @return array
      167. */
      168. function array_divide($array)
      169. {
      170. return array(array_keys($array), array_values($array));
      171. }
      172. /**
      173. * Return the value of the given item.
      174. *
      175. * If the given item is a Closure the result of the Closure will be returned.
      176. *
      177. * @param mixed $value
      178. * @return mixed
      179. */
      180. function value($value)
      181. {
      182. return ($value instanceof Closure) ? call_user_func($value) : $value;
      183. }
      Alles anzeigen


      Ich hoffe ich kann damit dem ein oder anderen helfen. Bei Frage und konstruktiver Kritik, einfach hier im Thread posten.

      Bitte bedenkt das es nur eine Beispiel Verwendung ist und natürlich ausgebaut werden muss/kann.
    • Werbung zur Unterstützung des Forums ( Bitte AddBlocker deaktivieren )