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

/sally/core/lib/sly/Util/BootCache.php

https://bitbucket.org/mediastuttgart/sallycms-0.6
PHP | 166 lines | 92 code | 34 blank | 40 comment | 11 complexity | 9c1d67355b1b4e2ae6484e06a8bfc1b3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
  4. *
  5. * This file is released under the terms of the MIT license. You can find the
  6. * complete text in the attached LICENSE file or online at:
  7. *
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. /**
  11. * @ingroup util
  12. */
  13. class sly_Util_BootCache {
  14. protected static $classes = array(); ///< array
  15. /**
  16. * @param string $environment
  17. */
  18. public static function init($environment) {
  19. // add core classes
  20. $list = sly_Util_YAML::load(SLY_COREFOLDER.'/config/bootcache.yml');
  21. self::$classes = $list['static'];
  22. if (isset($list[$environment])) {
  23. self::$classes = array_merge(self::$classes, $list[$environment]);
  24. }
  25. // add current cache instance
  26. $cacheClass = get_class(sly_Core::cache());
  27. // add current database driver
  28. $driver = sly_Core::config()->get('DATABASE/DRIVER');
  29. $driver = strtoupper($driver);
  30. self::addClass('sly_DB_PDO_Driver_'.$driver);
  31. self::addClass('sly_DB_PDO_SQLBuilder_'.$driver);
  32. // TODO: Remove these dependency hacks with a more elegant solution (Reflection?)
  33. if ($cacheClass === 'BabelCache_Memcached') {
  34. self::addClass('BabelCache_Memcache');
  35. }
  36. if ($cacheClass === 'BabelCache_Filesystem_Plain') {
  37. self::addClass('BabelCache_Filesystem');
  38. }
  39. self::addClass($cacheClass);
  40. }
  41. /**
  42. * @param string $environment
  43. */
  44. public static function recreate($environment) {
  45. // when in developer mode, only remove a possibly existing cache file
  46. if (sly_Core::isDeveloperMode()) {
  47. $target = self::getCacheFile($environment);
  48. if (file_exists($target)) {
  49. unlink($target);
  50. }
  51. return;
  52. }
  53. // create the file
  54. self::init($environment);
  55. sly_Core::dispatcher()->notify('SLY_BOOTCACHE_CLASSES_'.strtoupper($environment));
  56. self::createCacheFile($environment);
  57. }
  58. /**
  59. * @param string $className
  60. */
  61. public static function addClass($className) {
  62. self::$classes[] = $className;
  63. self::$classes = array_unique(self::$classes);
  64. }
  65. /**
  66. * @param string $environment
  67. * @return string
  68. */
  69. public static function getCacheFile($environment) {
  70. return SLY_DYNFOLDER.'/internal/sally/bootcache.'.$environment.'.php';
  71. }
  72. /**
  73. * @param string $environment
  74. */
  75. public static function createCacheFile($environment) {
  76. $target = self::getCacheFile($environment);
  77. if (file_exists($target)) {
  78. unlink($target);
  79. }
  80. file_put_contents($target, "<?php\n");
  81. foreach (self::$classes as $class) {
  82. $filename = sly_Loader::findClass($class);
  83. if (!$filename) continue;
  84. $code = self::getCode($filename);
  85. file_put_contents($target, $code."\n", FILE_APPEND);
  86. }
  87. // add functions
  88. $functionFiles = array(
  89. 'lib/compatibility.php',
  90. 'lib/functions.php'
  91. );
  92. foreach ($functionFiles as $fctFile) {
  93. $code = self::getCode(SLY_COREFOLDER.'/'.$fctFile);
  94. file_put_contents($target, $code."\n", FILE_APPEND);
  95. }
  96. }
  97. /**
  98. * @param string $filename
  99. * @return string
  100. */
  101. private static function getCode($filename) {
  102. $code = file_get_contents($filename);
  103. $result = trim($code);
  104. // remove comments and collapse whitespace into single spaces
  105. if (function_exists('token_get_all')) {
  106. $tokens = token_get_all($code);
  107. $result = '';
  108. foreach ($tokens as $token) {
  109. if (is_string($token)) {
  110. $result .= $token;
  111. }
  112. else {
  113. list($id, $text) = $token;
  114. switch ($id) {
  115. case T_COMMENT:
  116. case T_DOC_COMMENT:
  117. break;
  118. case T_WHITESPACE:
  119. $result .= ' ';
  120. break;
  121. default:
  122. $result .= $text;
  123. break;
  124. }
  125. }
  126. }
  127. }
  128. // remove starting php tag
  129. $result = preg_replace('#^<\?(php)?#is', '', $result);
  130. return trim($result);
  131. }
  132. }