PageRenderTime 88ms CodeModel.GetById 40ms app.highlight 6ms RepoModel.GetById 29ms app.codeStats 1ms

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