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

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

https://bitbucket.org/SallyCMS/trunk
PHP | 148 lines | 86 code | 32 blank | 30 comment | 11 complexity | 48bd5193cd8b1371f86f09f231f7a973 MD5 | raw file
  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. public static function init() {
  16. // add core classes
  17. $list = sly_Util_YAML::load(SLY_COREFOLDER.'/config/bootcache.yml');
  18. self::$classes = $list;
  19. // add current cache instance
  20. $cacheClass = get_class(sly_Core::cache());
  21. // add current database driver
  22. $driver = sly_Core::config()->get('DATABASE/DRIVER');
  23. $driver = strtoupper($driver);
  24. self::addClass('sly_DB_PDO_Driver_'.$driver);
  25. self::addClass('sly_DB_PDO_SQLBuilder_'.$driver);
  26. // TODO: Remove these dependency hacks with a more elegant solution (Reflection?)
  27. if ($cacheClass === 'BabelCache_Memcached') {
  28. self::addClass('BabelCache_Memcache');
  29. }
  30. if ($cacheClass === 'BabelCache_Filesystem_Plain') {
  31. self::addClass('BabelCache_Filesystem');
  32. }
  33. self::addClass($cacheClass);
  34. }
  35. public static function recreate() {
  36. // when in developer mode, only remove a possibly existing cache file
  37. if (sly_Core::isDeveloperMode() || !sly_Core::config()->get('bootcache')) {
  38. $target = self::getCacheFile();
  39. if (file_exists($target)) {
  40. unlink($target);
  41. }
  42. return;
  43. }
  44. // create the file
  45. self::init();
  46. sly_Core::dispatcher()->notify('SLY_BOOTCACHE_CLASSES');
  47. self::createCacheFile();
  48. }
  49. /**
  50. * @param string $className
  51. */
  52. public static function addClass($className) {
  53. self::$classes[] = $className;
  54. self::$classes = array_unique(self::$classes);
  55. }
  56. /**
  57. * @return string
  58. */
  59. public static function getCacheFile() {
  60. return SLY_DYNFOLDER.'/internal/sally/bootcache.php';
  61. }
  62. public static function createCacheFile() {
  63. $target = self::getCacheFile();
  64. if (file_exists($target)) {
  65. unlink($target);
  66. }
  67. file_put_contents($target, "<?php\n");
  68. foreach (self::$classes as $class) {
  69. $filename = sly_Loader::findClass($class);
  70. if (!$filename) continue;
  71. $code = self::getCode($filename);
  72. file_put_contents($target, $code."\n", FILE_APPEND);
  73. }
  74. // add functions
  75. $functionFiles = array(
  76. 'lib/compatibility.php',
  77. 'lib/functions.php'
  78. );
  79. foreach ($functionFiles as $fctFile) {
  80. $code = self::getCode(SLY_COREFOLDER.'/'.$fctFile);
  81. file_put_contents($target, $code."\n", FILE_APPEND);
  82. }
  83. }
  84. /**
  85. * @param string $filename
  86. * @return string
  87. */
  88. private static function getCode($filename) {
  89. $code = file_get_contents($filename);
  90. $result = trim($code);
  91. // remove comments
  92. if (function_exists('token_get_all')) {
  93. $tokens = token_get_all($code);
  94. $result = '';
  95. foreach ($tokens as $token) {
  96. if (is_string($token)) {
  97. $result .= $token;
  98. }
  99. else {
  100. list($id, $text) = $token;
  101. switch ($id) {
  102. case T_COMMENT:
  103. case T_DOC_COMMENT:
  104. break;
  105. default:
  106. $result .= $text;
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. // remove starting php tag
  113. $result = preg_replace('#^<\?(php)?#is', '', $result);
  114. return trim($result);
  115. }
  116. }