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

/sally/core/lib/Scaffold/modules/Extensions/Extensions.php

https://bitbucket.org/mediastuttgart/sallycms-0.6
PHP | 138 lines | 69 code | 15 blank | 54 comment | 6 complexity | b864287b8ad6c97849f809f7485f5de7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Custom_Properties
  4. *
  5. * Allows you to create new properties by dumping a function into
  6. * the properties folder.
  7. *
  8. * @author Anthony Short
  9. * @license BSD License
  10. */
  11. class Extensions
  12. {
  13. /**
  14. * The list of created properties
  15. * @var array
  16. */
  17. public static $extensions = array();
  18. /**
  19. * Post Process
  20. *
  21. * @param $css
  22. * @return string
  23. */
  24. public static function post_process()
  25. {
  26. self::load_extensions('extensions/properties','find_property');
  27. self::load_extensions('extensions/functions','find_functions',true);
  28. }
  29. /**
  30. * Loads each of the property functions and parses them.
  31. *
  32. * @param $name The location of the extension files
  33. * @param $function The CSS function to call to look for instances of it in the CSS
  34. * @param $split_params Explode the params before sending them off to the user function
  35. * @return $css string
  36. */
  37. public static function load_extensions($location,$function,$split_params = false)
  38. {
  39. $files = Scaffold::list_files($location,true);
  40. foreach ($files as $path)
  41. {
  42. if(is_dir($path))
  43. continue;
  44. /**
  45. * If the functions or properties ARE unique, they will
  46. * be parsed as such. If not, properties or functions that
  47. * are found to be exactly the same will be merged.
  48. */
  49. $unique = false;
  50. /**
  51. * The name of the property that can be used in Scaffold CSS
  52. */
  53. //this extension_name ends with '.php' -> need without it
  54. //$extension_name = basename($path); //pathinfo($path, PATHINFO_FILENAME) -> >= PHP 5.2.x
  55. $extension_name = substr(basename($path), 0 ,-4); //pathinfo($path, PATHINFO_FILENAME) -> >= PHP 5.2.x
  56. /**
  57. * Include the function we'll use as a callback
  58. */
  59. if(!isset(self::$extensions[$extension_name]))
  60. {
  61. include_once $path;
  62. }
  63. else
  64. {
  65. $unique = self::$extensions[$extension_name]['unique'];
  66. }
  67. /**
  68. * The name of the function we'll call for this property
  69. */
  70. $callback = 'Scaffold_'.str_replace('-','_',$extension_name);
  71. /**
  72. * Save this extension
  73. */
  74. self::$extensions[$extension_name] = array
  75. (
  76. 'unique' => $unique,
  77. 'path' => $path,
  78. 'callback' => $callback,
  79. 'function' => $function,
  80. 'split_params' => $split_params
  81. );
  82. /**
  83. * Find an replace them
  84. */
  85. if($found = Scaffold::$css->$function($extension_name))
  86. {
  87. // Make the list unique or not
  88. $originals = ($unique === false) ? array_unique($found[0]) : $found[0];
  89. // Loop through each found instance
  90. foreach($originals as $key => $value)
  91. {
  92. // Explode the params to send them as function params or as a single param
  93. if($split_params === true)
  94. {
  95. $result = call_user_func_array($callback,explode(',',$found[2][$key]));
  96. }
  97. else
  98. {
  99. $result = call_user_func($callback,$found[2][$key]);
  100. }
  101. // Run the user callback
  102. if($result === false)
  103. {
  104. Scaffold::error('Invalid Extension Syntax - <strong>' . $originals[$key] . '</strong>');
  105. }
  106. // Just replace the first match if they are unique
  107. elseif($unique === true)
  108. {
  109. $pos = strpos(Scaffold::$css->string,$originals[$key]);
  110. if($pos !== false)
  111. {
  112. Scaffold::$css->string = substr_replace(Scaffold::$css->string,$result,$pos,strlen($originals[$key]));
  113. }
  114. }
  115. else
  116. {
  117. Scaffold::$css->string = str_replace($originals[$key],$result,Scaffold::$css->string);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }