/** * Get/set parsed file contents. * * @param mixed $var * @return string */ public function content($var = null) { // Set some options $this->settings(['native' => true, 'compat' => true]); // If nothing has been loaded, attempt to get pre-compiled version of the file first. if ($var === null && $this->raw === null && $this->content === null) { $key = md5($this->filename); $file = PhpFile::instance(CACHE_DIR . "compiled/files/{$key}{$this->extension}.php"); $modified = $this->modified(); if (!$modified) { return $this->decode($this->raw()); } $class = get_class($this); $cache = $file->exists() ? $file->content() : null; // Load real file if cache isn't up to date (or is invalid). if (!isset($cache['@class']) || $cache['@class'] != $class || $cache['modified'] != $modified || $cache['filename'] != $this->filename) { // Attempt to lock the file for writing. $file->lock(false); // Decode RAW file into compiled array. $data = (array) $this->decode($this->raw()); $cache = ['@class' => $class, 'filename' => $this->filename, 'modified' => $modified, 'data' => $data]; // If compiled file wasn't already locked by another process, save it. if ($file->locked() !== false) { $file->save($cache); $file->unlock(); } } $this->content = $cache['data']; } return parent::content($var); }
protected function loadCompiledBlueprints($blueprints, $blueprintFiles) { $checksum = md5(serialize($blueprints)); $filename = CACHE_DIR . 'compiled/blueprints/' . $checksum . '.php'; $checksum .= ':' . md5(serialize($blueprintFiles)); $class = get_class($this); $file = PhpFile::instance($filename); if ($file->exists()) { $cache = $file->exists() ? $file->content() : null; } else { $cache = null; } // Load real file if cache isn't up to date (or is invalid). if (!is_array($cache) || empty($cache['checksum']) || empty($cache['$class']) || $cache['checksum'] != $checksum || $cache['@class'] != $class) { // Attempt to lock the file for writing. $file->lock(false); // Load blueprints. $this->blueprints = new Blueprints(); foreach ($blueprintFiles as $key => $files) { $this->loadBlueprints($key); } $cache = ['@class' => $class, 'checksum' => $checksum, 'files' => $blueprintFiles, 'data' => $this->blueprints->toArray()]; // If compiled file wasn't already locked by another process, save it. if ($file->locked() !== false) { $file->save($cache); $file->unlock(); } } else { $this->blueprints = new Blueprints($cache['data']); } }
/** * Get/set parsed file contents. * * @param mixed $var * @return string */ public function content($var = null) { if (!static::$cachePath) { throw new \BadMethodCallException('Cache path not defined for compiled files!'); } // If nothing has been loaded, attempt to get pre-compiled version of the file first. if ($var === null && $this->raw === null && $this->content === null) { $key = md5($this->filename); $file = PhpFile::instance(static::$cachePath . "/{$key}{$this->extension}.php"); $modified = $this->modified(); if (!$modified) { return $this->decode($this->raw()); } $class = get_class($this); $cache = $file->exists() ? $file->content() : null; // Load real file if cache isn't up to date (or is invalid). if (!isset($cache['@class']) || $cache['@class'] != $class || $cache['modified'] != $modified || $cache['filename'] != $this->filename) { // Attempt to lock the file for writing. $file->lock(false); // Decode RAW file into compiled array. $data = $this->decode($this->raw()); $cache = ['@class' => $class, 'filename' => $this->filename, 'modified' => $modified, 'data' => $data]; // If compiled file wasn't already locked by another process, save it. if ($file->locked() !== false) { $file->save($cache); $file->unlock(); } } $this->content = $cache['data']; } return parent::content($var); }
protected function createMeta($out, $md5) { $gantry = Gantry::instance(); if ($this->production) { return; } /** @var UniformResourceLocator $locator */ $locator = $gantry['locator']; $uri = basename($out); $metaFile = PhpFile::instance($locator->findResource("gantry-cache://theme/scss/{$uri}.php", true, true)); $metaFile->save(['file' => $out, 'timestamp' => filemtime($locator->findResource($out)), 'md5' => $md5, 'variables' => $this->getVariables(), 'imports' => $this->compiler->getParsedFiles()]); $metaFile->free(); }
protected function loadCompiledConfig($configs, $plugins, $filename = null) { $checksum = md5(json_encode($configs)); $filename = $filename ? CACHE_DIR . 'compiled/config/' . $filename . '-' . $this->environment . '.php' : CACHE_DIR . 'compiled/config/' . $checksum . '-' . $this->environment . '.php'; $file = PhpFile::instance($filename); $cache = $file->exists() ? $file->content() : null; $configFiles = $this->finder->locateConfigFiles($configs, $plugins); $checksum .= ':' . md5(json_encode($configFiles)); $class = get_class($this); // Load real file if cache isn't up to date (or is invalid). if (!is_array($cache) || !isset($cache['checksum']) || !isset($cache['@class']) || $cache['checksum'] != $checksum || $cache['@class'] != $class) { // Attempt to lock the file for writing. $file->lock(false); // Load configuration. foreach ($configFiles as $files) { $this->loadConfigFiles($files); } $cache = ['@class' => $class, 'timestamp' => time(), 'checksum' => $this->checksum(), 'data' => $this->toArray()]; // If compiled file wasn't already locked by another process, save it. if ($file->locked() !== false) { $this->messages[] = 'Saving compiled configuration.'; $file->save($cache); $file->unlock(); } } $this->items = $cache['data']; }
/** * Save compiled file. * * @param string $filename * @throws \RuntimeException * @internal */ protected function saveCompiledFile($filename) { $file = PhpFile::instance($filename); // Attempt to lock the file for writing. try { $file->lock(false); } catch (\Exception $e) { // Another process has locked the file; we will check this in a bit. } if ($file->locked() === false) { // File was already locked by another process. return; } $cache = ['@class' => get_class($this), 'timestamp' => time(), 'checksum' => $this->checksum(), 'files' => $this->files, 'data' => $this->getState()]; $file->save($cache); $file->unlock(); $file->free(); $this->modified(); }
protected function loadCompiledConfig($configs, $plugins, $filename = null) { $filename = $filename ? CACHE_DIR . 'compiled/config/' . $filename . '-' . $this->environment . '.php' : CACHE_DIR . 'compiled/config/' . $checksum . '-' . $this->environment . '.php'; $file = PhpFile::instance($filename); $cache = $file->exists() ? $file->content() : null; $class = get_class($this); $checksum = $this->checksum(); if (!is_array($cache) || !isset($cache['checksum']) || !isset($cache['@class']) || $cache['@class'] != $class) { $this->messages[] = 'No cached configuration, compiling new configuration..'; } else { if ($cache['checksum'] !== $checksum) { $this->messages[] = 'Configuration checksum mismatch, reloading configuration..'; } else { $this->messages[] = 'Configuration checksum matches, using cached version.'; $this->items = $cache['data']; return; } } $configFiles = $this->finder->locateConfigFiles($configs, $plugins); // Attempt to lock the file for writing. $file->lock(false); // Load configuration. foreach ($configFiles as $files) { $this->loadConfigFiles($files); } $cache = ['@class' => $class, 'timestamp' => time(), 'checksum' => $checksum, 'data' => $this->toArray()]; // If compiled file wasn't already locked by another process, save it. if ($file->locked() !== false) { $this->messages[] = 'Saving compiled configuration.'; $file->save($cache); $file->unlock(); } $this->items = $cache['data']; }
/** * @param $languages * @param $plugins * @param null $filename */ protected function loadCompiledLanguages($languages, $plugins, $filename = null) { $checksum = md5(json_encode($languages)); $filename = $filename ? CACHE_DIR . 'compiled/languages/' . $filename . '-' . $this->environment . '.php' : CACHE_DIR . 'compiled/languages/' . $checksum . '-' . $this->environment . '.php'; $file = PhpFile::instance($filename); $cache = $file->exists() ? $file->content() : null; $languageFiles = $this->finder->locateLanguageFiles($languages, $plugins); $checksum .= ':' . md5(json_encode($languageFiles)); $class = get_class($this); // Load real file if cache isn't up to date (or is invalid). if (!is_array($cache) || !isset($cache['checksum']) || !isset($cache['@class']) || $cache['checksum'] != $checksum || $cache['@class'] != $class) { // Attempt to lock the file for writing. $file->lock(false); // Load languages. $this->languages = new Languages(); if (isset($languageFiles['user/plugins'])) { foreach ((array) $languageFiles['user/plugins'] as $plugin => $item) { $lang_file = CompiledYamlFile::instance($item['file']); $content = $lang_file->content(); $this->languages->mergeRecursive($content); } unset($languageFiles['user/plugins']); } foreach ($languageFiles as $location) { foreach ($location as $lang => $item) { $lang_file = CompiledYamlFile::instance($item['file']); $content = $lang_file->content(); $this->languages->join($lang, $content, '/'); } } $cache = ['@class' => $class, 'checksum' => $checksum, 'files' => $languageFiles, 'data' => $this->languages->toArray()]; // If compiled file wasn't already locked by another process, save it. if ($file->locked() !== false) { $this->messages[] = 'Saving compiled languages.'; $file->save($cache); $file->unlock(); } } else { $this->languages = new Languages($cache['data']); } }
protected function createMeta($out, $md5) { $gantry = Gantry::instance(); if ($this->production) { return; } /** @var UniformResourceLocator $locator */ $locator = $gantry['locator']; $uri = basename($out); $metaFile = PhpFile::instance($locator->findResource("gantry-cache://theme/scss/{$uri}.php", true, true)); $data = ['file' => $out, 'timestamp' => filemtime($locator->findResource($out)), 'md5' => $md5, 'variables' => $this->getVariables(), 'imports' => $this->compiler->getParsedFiles()]; // Attempt to lock the file for writing. try { $metaFile->lock(false); } catch (\Exception $e) { // Another process has locked the file; we will check this in a bit. } // If meta file wasn't already locked by another process, save it. if ($metaFile->locked() !== false) { $metaFile->save($data); $metaFile->unlock(); } $metaFile->free(); }
/** * Save compiled file. * * @param string $filename * @throws \RuntimeException * @internal */ protected function saveCompiledFile($filename) { $file = PhpFile::instance($filename); // Attempt to lock the file for writing. $file->lock(false); if ($file->locked() === false) { // File was already locked by another process. return; } $cache = ['@class' => get_class($this), 'timestamp' => time(), 'checksum' => $this->checksum(), 'files' => $this->files, 'data' => $this->object->toArray()]; $file->save($cache); $file->unlock(); }
/** * Save compiled file. * * @param string $filename * @throws \RuntimeException * @internal */ protected function saveCompiledFile($filename) { $gantry = Gantry::instance(); /** @var Config $global */ $global = $gantry['global']; if (!$global->get('compile_yaml', 1)) { return; } $file = PhpFile::instance($filename); // Attempt to lock the file for writing. try { $file->lock(false); } catch (\Exception $e) { // Another process has locked the file; we will check this in a bit. } if ($file->locked() === false) { // File was already locked by another process. return; } $cache = ['@class' => get_class($this), 'timestamp' => time(), 'checksum' => $this->checksum(), 'files' => $this->files, 'data' => $this->object->toArray()]; $file->save($cache); $file->unlock(); $file->free(); $this->modified(); }