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

Language PHP Lines 149
MD5 Hash 48bd5193cd8b1371f86f09f231f7a973 Estimated Cost $2,030 (why?)
Repository https://bitbucket.org/SallyCMS/trunk View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
 * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

/**
 * @ingroup util
 */
class sly_Util_BootCache {
	protected static $classes = array(); ///< array

	public static function init() {
		// add core classes
		$list = sly_Util_YAML::load(SLY_COREFOLDER.'/config/bootcache.yml');
		self::$classes = $list;

		// add current cache instance
		$cacheClass = get_class(sly_Core::cache());

		// add current database driver
		$driver = sly_Core::config()->get('DATABASE/DRIVER');
		$driver = strtoupper($driver);

		self::addClass('sly_DB_PDO_Driver_'.$driver);
		self::addClass('sly_DB_PDO_SQLBuilder_'.$driver);

		// TODO: Remove these dependency hacks with a more elegant solution (Reflection?)
		if ($cacheClass === 'BabelCache_Memcached') {
			self::addClass('BabelCache_Memcache');
		}

		if ($cacheClass === 'BabelCache_Filesystem_Plain') {
			self::addClass('BabelCache_Filesystem');
		}

		self::addClass($cacheClass);
	}

	public static function recreate() {
		// when in developer mode, only remove a possibly existing cache file

		if (sly_Core::isDeveloperMode() || !sly_Core::config()->get('bootcache')) {
			$target = self::getCacheFile();

			if (file_exists($target)) {
				unlink($target);
			}

			return;
		}

		// create the file

		self::init();
		sly_Core::dispatcher()->notify('SLY_BOOTCACHE_CLASSES');
		self::createCacheFile();
	}

	/**
	 * @param string $className
	 */
	public static function addClass($className) {
		self::$classes[] = $className;
		self::$classes   = array_unique(self::$classes);
	}

	/**
	 * @return string
	 */
	public static function getCacheFile() {
		return SLY_DYNFOLDER.'/internal/sally/bootcache.php';
	}

	public static function createCacheFile() {
		$target = self::getCacheFile();

		if (file_exists($target)) {
			unlink($target);
		}

		file_put_contents($target, "<?php\n");

		foreach (self::$classes as $class) {
			$filename = sly_Loader::findClass($class);
			if (!$filename) continue;

			$code = self::getCode($filename);
			file_put_contents($target, $code."\n", FILE_APPEND);
		}

		// add functions

		$functionFiles = array(
			'lib/compatibility.php',
			'lib/functions.php'
		);

		foreach ($functionFiles as $fctFile) {
			$code = self::getCode(SLY_COREFOLDER.'/'.$fctFile);
			file_put_contents($target, $code."\n", FILE_APPEND);
		}
	}

	/**
	 * @param  string $filename
	 * @return string
	 */
	private static function getCode($filename) {
		$code   = file_get_contents($filename);
		$result = trim($code);

		// remove comments

		if (function_exists('token_get_all')) {
			$tokens = token_get_all($code);
			$result = '';

			foreach ($tokens as $token) {
				if (is_string($token)) {
					$result .= $token;
				}
				else {
					list($id, $text) = $token;

					switch ($id) {
						case T_COMMENT:
						case T_DOC_COMMENT:
							break;

						default:
							$result .= $text;
							break;
					}
				}
			}
		}

		// remove starting php tag
		$result = preg_replace('#^<\?(php)?#is', '', $result);

		return trim($result);
	}
}
Back to Top