PageRenderTime 91ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/SallyCMS/0.6
PHP | 106 lines | 50 code | 16 blank | 40 comment | 7 complexity | 6a1b94110cfe14edae96cb7879cc13d7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Import
  4. *
  5. * This allows you to import files before processing for compiling
  6. * into a single file and later cached. This is done via @import ''
  7. *
  8. * @author Anthony Short
  9. * @dependencies None
  10. **/
  11. class Import
  12. {
  13. /**
  14. * Stores which files have already been included
  15. *
  16. * @var array
  17. */
  18. public static $loaded = array();
  19. /**
  20. * This function occurs before everything else
  21. *
  22. * @author Anthony Short
  23. * @param $css
  24. */
  25. public static function import_process()
  26. {
  27. # Add the original file to the loaded array
  28. self::$loaded[] = Scaffold::$css->file;
  29. # Find all the @server imports
  30. Scaffold::$css->string = self::server_import(Scaffold::$css->string,Scaffold::$css->path);
  31. }
  32. /**
  33. * Imports css via @import statements
  34. *
  35. * @author Anthony Short
  36. * @param $css
  37. */
  38. public static function server_import($css,$base)
  39. {
  40. if(preg_match_all('/\@include\s+(?:\'|\")([^\'\"]+)(?:\'|\")\;/', $css, $matches))
  41. {
  42. $unique = array_unique($matches[1]);
  43. $include = str_replace("\\", "/", Scaffold_Utils::unquote($unique[0]));
  44. # If they haven't supplied an extension, we'll assume its a css file
  45. if(pathinfo($include, PATHINFO_EXTENSION) == "")
  46. $include .= '.css';
  47. # Make sure it's a CSS file
  48. if(pathinfo($include, PATHINFO_EXTENSION) != 'css')
  49. {
  50. $css = str_replace($matches[0][0], '', $css);
  51. Scaffold::log('Invalid @include file - ' . $include);
  52. self::server_import($css,$base);
  53. }
  54. # Find the file
  55. if($path = Scaffold::find_file($include,$base))
  56. {
  57. # Make sure it hasn't already been included
  58. if(!in_array($path, self::$loaded))
  59. {
  60. self::$loaded[] = $path;
  61. $contents = file_get_contents($path);
  62. # Check the file again for more imports
  63. $contents = self::server_import($contents, realpath(dirname($path)) . '/');
  64. $css = str_replace($matches[0][0], $contents, $css);
  65. }
  66. # It's already been included, we don't need to import it again
  67. else
  68. {
  69. $css = str_replace($matches[0][0], '', $css);
  70. }
  71. }
  72. else
  73. {
  74. Scaffold::error('Can\'t find the @include file - <strong>' . $unique[0] . '</strong>');
  75. }
  76. $css = self::server_import($css,$base);
  77. }
  78. return $css;
  79. }
  80. /**
  81. * Resets the loaded array
  82. *
  83. * @author Anthony Short
  84. * @return return type
  85. */
  86. public static function reset()
  87. {
  88. self::$loaded = array();
  89. }
  90. }