PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/SallyCMS/0.6
PHP | 195 lines | 84 code | 16 blank | 95 comment | 5 complexity | 10dc562c8fc1ac852bb5ea29a689364b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Constants
  4. *
  5. * Allows you to use constants within your css by defining them
  6. * within @constants and then using a property list.
  7. *
  8. * You can set CSScaffold constants using XML. This allows you to create
  9. * constants using a CMS or by any other means to tie it in with your CSS.
  10. *
  11. * XML must be in this format:
  12. <?xml version="1.0" ?>
  13. <constants>
  14. <constant>
  15. <name>Foo</name>
  16. <value>Bar</value>
  17. </constant>
  18. </constants>
  19. *
  20. * By default, it requires a constants.xml file in the root of the CSS directory.
  21. * You can change this in the plugins config.
  22. *
  23. * @author Anthony Short
  24. * @license BSD License
  25. */
  26. class Constants
  27. {
  28. /**
  29. * Stores all of the constants for the app
  30. *
  31. * @var array
  32. */
  33. public static $constants = array();
  34. /**
  35. * The pre-processing function occurs after the importing,
  36. * but before any real processing. This is usually the stage
  37. * where we set variables and the like, getting the css ready
  38. * for processing.
  39. *
  40. * @author Anthony Short
  41. * @param $css
  42. */
  43. public static function pre_process()
  44. {
  45. # Global Constants
  46. self::set_global_constants();
  47. # XML Constants
  48. self::load_xml_constants( Scaffold::$config['Constants']['xml_path'] );
  49. # If there are some constants, let do it.
  50. if( $found = Scaffold::$css->find_at_group('constants') )
  51. {
  52. # Create our template style constants
  53. foreach($found['values'] as $key => $value)
  54. {
  55. # Check if this contains other constants
  56. $value = self::replace($value);
  57. # Set it
  58. self::set($key, $value);
  59. }
  60. }
  61. }
  62. /**
  63. * Replaces the constants
  64. *
  65. * @return void
  66. */
  67. public static function process()
  68. {
  69. Scaffold::$css->string = self::replace(Scaffold::$css->string);
  70. }
  71. /**
  72. * Sets the global constants
  73. *
  74. * @return void
  75. */
  76. private static function set_global_constants()
  77. {
  78. foreach(Scaffold::$config['Constants']['global'] as $key => $value)
  79. {
  80. self::set($key,$value);
  81. }
  82. }
  83. /**
  84. * Sets constants
  85. *
  86. * @author Anthony Short
  87. * @param $key
  88. * @param $value
  89. * @return null
  90. */
  91. public static function set($key, $value = "")
  92. {
  93. # So we can pass through a whole array
  94. # and set them all at once
  95. if(is_array($key))
  96. {
  97. foreach($key as $name => $val)
  98. {
  99. self::$constants[$name] = $val;
  100. }
  101. }
  102. else
  103. {
  104. self::$constants[$key] = $value;
  105. }
  106. }
  107. /**
  108. * Unsets a constant
  109. *
  110. * @param $key
  111. * @return void
  112. */
  113. public static function remove($key)
  114. {
  115. unset(self::$constants[$key]);
  116. }
  117. /**
  118. * Returns the constant value
  119. *
  120. * @author Anthony Short
  121. * @param $key
  122. * @return string
  123. */
  124. public static function get($key)
  125. {
  126. return self::$constants[$key];
  127. }
  128. /**
  129. * Replaces all of the constants in a CSS string
  130. * with the constants defined in the member variable $constants
  131. * using PHP's interpolation.
  132. */
  133. public static function replace($css)
  134. {
  135. # Pull the constants into the local scope as variables
  136. extract(self::$constants, EXTR_SKIP);
  137. # Remove unset variables from the string, so errors aren't thrown
  138. foreach(array_unique( Scaffold_Utils::match('/\{?\$([A-Za-z0-9_-]+)\}?/', $css, 1) ) as $value)
  139. {
  140. if(!isset($$value))
  141. {
  142. Scaffold::error('Missing constant - ' . $value);
  143. }
  144. }
  145. $css = stripslashes( eval('return "' . addslashes($css) . '";') );
  146. # Replace the variables within the string like a normal PHP string
  147. return $css;
  148. }
  149. /**
  150. * Loads constants from an XML file
  151. *
  152. * @param $param
  153. * @return return type
  154. */
  155. private static function load_xml_constants($file)
  156. {
  157. if($file === false)
  158. return;
  159. # If the xml file doesn't exist
  160. if(!file_exists($file))
  161. {
  162. Scaffold::log("Missing constants XML file. The file ($file) doesn't exist.",1);
  163. return;
  164. }
  165. # Load the xml
  166. $xml = simplexml_load_file($file);
  167. # Loop through them and set them as constants
  168. foreach($xml->constant as $key => $value)
  169. {
  170. self::set((string)$value->name, (string)$value->value);
  171. }
  172. }
  173. }