Last active
December 27, 2015 17:39
-
-
Save tristanlins/7363628 to your computer and use it in GitHub Desktop.
Hacking the Contao 2 classes cache and overwrite with a class_exists check, so Controller::classFileExists() will return true, even if the class file does not exist in any system/modules/* directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Hack the Contao2 Controller::classFileExists() | |
* | |
* (c) Tristan Lins <[email protected]> | |
* Christian Schiffler <[email protected]> | |
* | |
* @author Tristan Lins <[email protected]> | |
* @license MIT | |
*/ | |
/** | |
* Class Contao2ClassLoaderHack | |
* | |
* @author Tristan Lins <[email protected]> | |
* @author Christian Schiffler <[email protected]> | |
*/ | |
if (version_compare(VERSION, '3', '<')) { | |
class Contao2ClassFileExistsHack extends FileCache | |
{ | |
public static function register() | |
{ | |
$cache = FileCache::getInstance('classes'); | |
if (!$cache instanceof Contao2ClassFileExistsHack) { | |
FileCache::$arrInstances['classes'] = new Contao2ClassFileExistsHack($cache); | |
} | |
} | |
/** | |
* The internal cache. | |
* | |
* @var FileCache | |
*/ | |
protected $cache; | |
/** | |
* @param FileCache $cache | |
*/ | |
public function __construct(FileCache $cache) | |
{ | |
$this->cache = $cache; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __destruct() | |
{ | |
// no op | |
} | |
/** | |
* Trigger all class loaders and try to load the class through them. | |
* | |
* @param string $strKey | |
* | |
* @return bool | |
*/ | |
protected function classExists($strKey) | |
{ | |
$exists = class_exists($strKey, false); | |
if (!$exists) { | |
$functions = spl_autoload_functions(); | |
while (!$exists && count($functions)) { | |
$function = array_shift($functions); | |
if ($function == '__autoload') { | |
continue; | |
} | |
call_user_func($function, $strKey); | |
$exists = class_exists($strKey, false); | |
} | |
} | |
return $exists; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __isset($strKey) | |
{ | |
$isset = $this->cache->__isset($strKey); | |
return $isset | |
? $isset | |
: $this->classExists($strKey); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __get($strKey) | |
{ | |
$value = $this->cache->__get($strKey); | |
return $value | |
? $value | |
: $this->classExists($strKey); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __set($strKey, $varValue) | |
{ | |
$this->cache->__set($strKey, $varValue); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __unset($strKey) | |
{ | |
$this->cache->__unset($strKey); | |
} | |
} | |
} else { | |
class Contao2ClassFileExistsHack | |
{ | |
public static function register() | |
{ | |
// no op | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment