/** * Will load a serialized map from the storage file, if it exists * * @return bool */ protected function load() { $this->stackableMap = $this->getInitialContext()->getAttribute(self::CLASS_MAP); // Can we read the intended path? if (!isset($this->stackableMap)) { // If the map of our parent is empty we have to start generating it if (empty($this->map)) { // First of all try to use the parent load function, maybe we got it within a file. // If not we have to start generating if (!parent::load()) { // Start generation parent::generate(); } } // Get the map $this->stackableMap = new GenericStackable(); // Copy the contents from the array into our stackable foreach ($this->map as $key => $entry) { $this->stackableMap[$key] = $entry; } // Remove the version entry from the map, we do not need it unset($this->stackableMap['version']); // Save the generated class into our storage $this->getInitialContext()->setAttribute(self::CLASS_MAP, $this->stackableMap); return true; } else { return true; } }
/** * Will load any given structure based on it's availability in our structure map which depends on the configured * project directories. * If the structure cannot be found we will redirect to the composer autoloader which we registered as a fallback * * @param string $className The name of the structure we will try to load * * @return bool */ public function loadClass($className) { // There was no file in our cache dir, so lets hope we know the original path of the file. $autoLoaderConfig = $this->config->getConfig('autoloader'); // Might the class be a omitted one? If so we can require the original. if (isset($autoLoaderConfig['omit'])) { foreach ($autoLoaderConfig['omit'] as $omitted) { // If our class name begins with the omitted part e.g. it's namespace if (strpos($className, str_replace('\\\\', '\\', $omitted)) === 0) { return false; } } } // Do we have the file in our cache dir? If we are in development mode we have to ignore this. $cacheConfig = $this->config->getConfig('cache'); if ($this->config->getConfig('environment') !== 'development') { $cachePath = $cacheConfig['dir'] . DIRECTORY_SEPARATOR . str_replace('\\', '_', $className) . '.php'; if (is_readable($cachePath)) { $res = fopen($cachePath, 'r'); $str = fread($res, 384); $success = preg_match('/' . PBC_ORIGINAL_PATH_HINT . '(.+)' . PBC_ORIGINAL_PATH_HINT . '/', $str, $tmp); if ($success > 0) { $tmp = explode('#', $tmp[1]); $path = $tmp[0]; $mTime = $tmp[1]; if (filemtime($path) == $mTime) { require $cachePath; return true; } } } } // If we are loading something of our own library we can skip to composer if (strpos($className, 'TechDivision\\PBC') === 0 && strpos($className, 'TechDivision\\PBC\\Tests') === false || strpos($className, 'PHP') === 0) { return false; } // If the structure map did not get filled by now we will do so here if ($this->structureMap->isEmpty()) { $this->structureMap->fill(); } // Get the file from the map $file = $this->structureMap->getEntry($className); // Did we get something? If not return false. if ($file === false) { return false; } // We are still here, so we know the class and it is not omitted. Does it contain contracts then? if (!$file->hasContracts() || !$file->isEnforced()) { require $file->getPath(); return true; } // So we have to create a new class definition for this original class. // Get a current cache instance if we do not have one already. if ($this->cache === null) { // We also require the classes of our maps as we do not have proper autoloading in place $this->cache = new CacheMap($cacheConfig['dir'], array(), $this->config); } $this->generator = new Generator($this->structureMap, $this->cache, $this->config); // Create the new class definition if ($this->generator->create($file, $this->config->getValue('enforcement/contract-inheritance')) === true) { // Require the new class, it should have been created now $file = $this->generator->getFileName($className); if (is_readable($file) === true) { require $file; return true; } } else { return false; } // Still here? That sounds like bad news! return false; }
/** * Will test if classes with huge class doc comments can be picked up correctly * * @return void */ public function testWithHugeClassDocBlockClass() { // test if we have the entry for the underscored class $this->assertTrue($this->structureMap->entryExists('Random\\Test\\NamespaceName\\HugeClassDocBlockClass')); }