/** * Renders the file view * @param boolean $return True to return the contents of the file, false * to passthru the file to the output * @return null|string */ public function render($return = true) { if ($return) { return $this->file->read(); } $this->file->passthru(); }
/** * Gets all the files needed for the provided CSS file. This will extract the imports from the CSS. * @param zibo\core\Zibo $zibo Instance of Zibo * @param zibo\library\filesystem\File $file CSS source file * @return array Array with the path of the file as key and the File object as value */ private function getFilesFromStyle(Zibo $zibo, File $file) { $source = $file->read(); $source = preg_replace(CSSMin::REGEX_COMMENT, '', $source); $files = array(); $parent = $file->getParent(); $lines = explode("\n", $source); foreach ($lines as $line) { $line = trim($line); if (empty($line)) { continue; } if (!preg_match(CSSMin::REGEX_IMPORT, $line)) { break; } $importFileName = $this->getFileNameFromImportLine($line); $importFile = $zibo->getRelativeFile(new File($parent, $importFileName)); $importFile = $zibo->getFile($importFile); if (!$importFile) { continue; } $styleFiles = $this->getFilesFromStyle($zibo, $importFile); $files = Structure::merge($files, $styleFiles); } $files[$file->getPath()] = $file; return $files; }
/** * Loads all the cache types to memory * @return null */ private function readTypes() { $files = $this->path->read(); foreach ($files as $file) { if ($file->isDirectory()) { continue; } $this->readType($file->getName()); } }
/** * Reads the cache objects from the cache file * @return null */ private function readFile() { if ($this->cache !== null) { return; } if (!$this->file->exists()) { $this->cache = array(); return; } $serializedValue = $this->file->read(); $this->cache = unserialize($serializedValue); }
public function testWriteToCache() { $this->setUpWriteCache(); $type = 'type'; $id = 'id'; $value = 'Test value'; $object = new CacheObject($type, $id, $value); $this->cache->writeToCache($object); $cacheFile = new File($this->path, $type . File::DIRECTORY_SEPARATOR . $id); $this->assertEquals(serialize($object), $cacheFile->read()); $this->tearDownCache(); }
public function testSetTranslationWhenTranslationFileExists() { $localeCode = 'locale'; $key = 'translation5'; $translation = 'Translation 5'; $this->io->setTranslation($localeCode, $key, $translation); $fileName = Zibo::DIRECTORY_APPLICATION . File::DIRECTORY_SEPARATOR . Zibo::DIRECTORY_L10N . File::DIRECTORY_SEPARATOR . $localeCode . IniTranslationIO::EXTENSION; $translationFile = new File($fileName); $this->assertTrue($translationFile->exists(), 'No file is being written'); $iniContent = $translationFile->read(); $translations = parse_ini_string($iniContent, false); $this->assertEquals(5, count($translations)); $this->assertTrue(array_key_exists($key, $translations)); $this->assertEquals($translation, $translations[$key]); }
public function read(File $file) { if (!$file->exists()) { return false; } $ini = $file->read(); $ini = parse_ini_string($ini); if (array_key_exists('current', $ini)) { $this->current = $ini['current']; } $this->visited = $ini['visited']; $this->gathered = $ini['gathered']; $this->start = $ini['start']; $this->stop = $ini['stop']; }
/** * Get the base paths of the Zibo filesystem structure. This will return the path of application, the modules and system. * @param boolean $refresh set to true to reread the include paths * @return array array with File instances */ public function getIncludePaths($refresh = false) { if ($this->includePaths && !$refresh) { return $this->includePaths; } $this->includePaths = array(); $this->includePaths[] = new File($this->rootPath, Zibo::DIRECTORY_APPLICATION); $modulePath = new File($this->rootPath, Zibo::DIRECTORY_MODULES); $moduleFiles = $modulePath->read(); foreach ($moduleFiles as $moduleFile) { if (!$moduleFile->exists() || !($moduleFile->isPhar() || $moduleFile->isDirectory())) { continue; } $this->includePaths[] = $moduleFile; } $this->includePaths[] = new File($this->rootPath, Zibo::DIRECTORY_SYSTEM); return $this->includePaths; }
/** * Reads from the filesystem with extra options * @param zibo\library\filesystem\File $path The path of the directory to read * @param array $filters Array with filters * @param boolean $recursive Set to true to read recursivly * @return array Array with the directories and files of the provided path */ public function readDirectory(File $path = null, array $filters = array(), $recursive = false) { $path = new File($this->root, $path); if (!$path->exists()) { throw new FileSystemException($path->getPath() . ' does not exist'); } if (!$path->isDirectory()) { throw new FileSystemException($path->getPath() . ' is not a directory'); } if (!$path->isReadable()) { throw new FileSystemException($path->getPath() . ' is not readable'); } $paths = array(); $files = $path->read($recursive); foreach ($files as $file) { $path = $this->getPath($file, false); $paths[$path->getPath()] = $path; } if ($filters) { $paths = $this->applyFilters($paths, $filters); } return $paths; }
/** * Imports a SQL file on this connection * @param zibo\library\filesystem\File $file SQL file * @return null */ public function import(File $file) { if (!$file->exists()) { throw new DatabaseException('Provided file does not exist'); } $content = $file->read(); $content = preg_replace('#/\\*.*?\\*/#s', '', $content); $sqls = array(); $sql = ''; $lines = explode("\n", $content); foreach ($lines as $line) { $line = trim($line); if (empty($line) || substr($line, 0, 2) == '--') { continue; } $sql .= $line; if (substr($sql, -1, 1) == ';') { $sql = substr($sql, 0, -1); $sqls[] = $sql; $sql = ''; } } $sql[] = $sql; $transactionStarted = $this->startTransaction(); try { foreach ($sqls as $sql) { $this->execute($sql); } if ($transactionStarted) { $this->commitTransaction(); } } catch (Exception $exception) { if ($transactionStarted) { $this->rollbackTransaction(); } } }
/** * Compresses a file into the archive * @param ZipArchive $archive ZipArchive object of PHP * @param zibo\library\filesystem\File $file The file to compress in the archive * @param zibo\library\filesystem\File $prefix The path for the file in the archive * @return null */ private function compressFile(ZipArchive $archive, File $file, File $prefix = null) { if ($prefix == null) { $prefix = new File($file->getName()); } else { $prefix = new File($prefix, $file->getName()); } $children = null; if ($file->exists()) { if ($file->isDirectory()) { $children = $file->read(); } else { $archive->addFile($file->getPath(), $prefix->getPath()); return; } } if (empty($children)) { $archive->addEmptyDir($prefix->getPath()); } else { foreach ($children as $file) { $this->compressFile($archive, $file, $prefix); } } }
/** * Read all the namespaces in a path * @param zibo\library\filesystem\File $path path to read * @param array $namespaces already found namespaces * @param string $prefix namespace prefix for the results * @return array Array with namespaces as key and as value */ private function readNamespacesFromPath(File $path, array $namespaces, $prefix = null) { if (!$path->exists()) { return $namespaces; } $files = $path->read(); foreach ($files as $file) { if (!$file->isDirectory()) { continue; } $name = $prefix . $file->getName(); $namespaces[$name] = $name; $namespaces = $this->readNamespacesFromPath($file, $namespaces, $name . self::NAMESPACE_SEPARATOR); } return $namespaces; }
/** * Updates the module model with the installed modules * @return null */ private function readModules() { $modules = array(); $path = new File(Zibo::DIRECTORY_MODULES); $files = $path->read(); foreach ($files as $file) { // skip hidden files if (strncmp($file->getName(), '.', 1) === 0) { continue; } $fileModules = $this->getModulesFromPath($file); $modules = Structure::merge($modules, $fileModules); } $this->model->addModules($modules); }
/** * Reads the countries from the provided file * @param zibo\library\filesystem\File $file * @return array Array with the country code as key and the name as value */ private function readCountries(File $file) { $countries = array(); $content = $file->read(); $lines = explode("\n", $content); foreach ($lines as $line) { $line = trim($line); if (!$line) { continue; } list($code, $name) = explode('=', $line, 2); $code = trim($code); $name = trim($name); $name = ltrim(rtrim($name, '"'), '"'); $countries[$code] = $name; } return $countries; }
/** * Get the names of the sections in the provided directory * @param zibo\library\filesystem\File $directory * @return array Array with the file names of all the ini files, withouth the extension */ private function getDirectorySections(File $directory) { $sections = array(); if (!$directory->exists()) { return $sections; } $files = $directory->read(); foreach ($files as $file) { if ($file->isDirectory() || $file->getExtension() != 'ini') { continue; } $sectionName = substr($file->getName(), 0, -4); $sections[$sectionName] = $sectionName; } return $sections; }
public function testUncompress() { $archive = new Phar($this->file); $archive->compress($this->compressFiles); $archive->compress($this->compressFiles, $this->prefix); $uncompressDirectory = new File('application/data/uncompress'); if ($uncompressDirectory->exists()) { $uncompressDirectory->delete(); } $uncompressFiles = array(); foreach ($this->compressFiles as $file) { $uncompressFiles[] = new File($file->getName()); $uncompressFiles[] = new File($this->prefix, $file->getName()); } try { $archive->uncompress($uncompressDirectory); } catch (FileSystemException $e) { $this->fail('Uncompress directory was not created'); } $contentBuildXml = $this->compressFiles[1]->read(); $uncompressedFiles = $uncompressDirectory->read(true); foreach ($uncompressFiles as $file) { $uncompressedFile = new File($uncompressDirectory, $file); $this->assertArrayHasKey($uncompressedFile->getPath(), $uncompressedFiles, $uncompressedFile->getPath()); if ($file->getName() == 'build.xml' && $uncompressedFile->read() != $contentBuildXml) { $this->fail('Content of the uncompressed build.xml is not the same as the original file'); } } $uncompressDirectory->delete(); }
/** * Reads the cache object from a file * @param zibo\library\filesystem\File $cacheFile File of the cache object * @return zibo\library\cache\CacheObject|null The read cache object or null */ private function readCacheObject(File $cacheFile) { if (!$cacheFile->exists()) { return null; } try { if ($this->useLock) { $cacheFile->waitForUnlock(); } $serializedValue = $cacheFile->read(); $object = unserialize($serializedValue); } catch (Exception $exception) { $object = null; } if (!$object) { return null; } return $object; }
/** * Get the available themes based on the existance of the theme directory * @param string $themesDirectory name of the theme directory in the view directory (optional) * @return array Array with the names of the available themes */ public static function getThemes($themesDirectory = null) { if ($themesDirectory === null) { $themesDirectory = self::DEFAULT_THEMES_DIRECTORY; } $themes = array(); $includePaths = Zibo::getInstance()->getIncludePaths(); $viewPath = new File(Zibo::DIRECTORY_VIEW, $themesDirectory); foreach ($includePaths as $includePath) { $themesPath = new File($includePath, $viewPath); if ($themesPath->exists() && $themesPath->isDirectory() && $themesPath->isReadable()) { $themesFiles = $themesPath->read(); foreach ($themesFiles as $themesFile) { if ($themesFile->isDirectory() && $themesFile->isReadable()) { $name = $themesFile->getName(); $themes[$name] = $name; } } } } return $themes; }
/** * Action to edit or create a file * * Every argument to this method is a part of the file to edit. eg. * $fb->editAction('application', 'data', 'test.txt') would show the editor for application/data/test.txt * * To create a new file in a directory, give the arguments to a directory instead of a file. * @return null */ public function editAction() { $pieces = func_get_args(); $path = $this->getFileFromPieces($pieces, false); $absolutePath = new File($this->fileBrowser->getRoot(), $path); $basePath = $this->request->getBasePath() . '/'; $saveAction = $basePath . self::ACTION_EDIT . ($path ? '/' . $path : ''); $name = null; $content = null; if ($path) { if (!$absolutePath->exists()) { $this->addError(self::TRANSLATION_ERROR_EXIST_NOT, array('path' => $this->fileBrowser->getPath($path))); $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND); $this->response->setView(new BaseView()); return; } if (!$absolutePath->isDirectory()) { $name = $absolutePath->getName(); $path = $absolutePath->getParent(); $content = $absolutePath->read(); } else { $path = $absolutePath; } } else { $path = $absolutePath; } $isWritable = $absolutePath->isWritable(); $path = $this->fileBrowser->getPath($path, false); $form = new EditorForm($saveAction, $path, $name, $content); if ($form->isSubmitted()) { $path = $path->getPath(); $redirectUrl = $basePath . self::ACTION_PATH . ($path != '.' ? '/' . $path : ''); if ($form->isCancelled()) { $this->response->setRedirect($redirectUrl); return; } try { $form->validate(); $content = $form->getFileContent(); $name = $form->getFileName(); $path = $form->getFilePath(); $file = new File($this->fileBrowser->getRoot(), $path . '/' . $name); if ($file->isWritable()) { $file->write($content); $this->addInformation(self::TRANSLATION_INFORMATION_SAVED, array('path' => $this->fileBrowser->getPath($file, false))); $this->response->setRedirect($redirectUrl); } else { $this->addError(self::TRANSLATION_ERROR_WRITABLE, array('path' => $this->fileBrowser->getPath($file, false))); $form->setIsDisabled(true, EditorForm::BUTTON_SUBMIT); $isWritable = true; } } catch (ValidationException $exception) { } catch (Exception $exception) { Zibo::getInstance()->runEvent(Zibo::EVENT_LOG, $exception->getMessage(), $exception->getTraceAsString(), 1); $this->addError(self::TRANSLATION_ERROR, array('error' => $exception->getMessage())); $this->response->setStatusCode(Response::STATUS_CODE_SERVER_ERROR); } } if (!$isWritable) { $form->setIsDisabled(true, EditorForm::BUTTON_SUBMIT); $this->addWarning(self::TRANSLATION_ERROR_WRITABLE, array('path' => $path . ($name ? '/' . $name : ''))); } $view = new EditorView($form, $path); $view->setPageTitle(Module::TRANSLATION_FILE_BROWSER, true); $this->response->setView($view); }
/** * Reads the cache object from a file * @param zibo\library\filesystem\File $cacheFile File of the cache object * @return zibo\library\cache\CacheObject|null The read cache object or null */ private function readCacheObject(File $cacheFile) { if (!$cacheFile->exists()) { return null; } try { if ($this->useLock) { $cacheFile->waitForUnlock(); } $serializedValue = $cacheFile->read(); $object = unserialize($serializedValue); } catch (Exception $exception) { Zibo::getInstance()->runEvent(Zibo::EVENT_LOG, $exception->getMessage(), $exception->getTraceAsString(), 1); $object = null; } if (!$object) { return null; } return $object; }
public function reportAction($id, $url = null) { $spider = null; $url = null; if (isset($_GET['url'])) { $url = $_GET['url']; } $fileSpider = new File(self::PATH_DATA, $id . self::SUFFIX_SPIDER); if ($fileSpider->exists()) { $fileSpiderContent = $fileSpider->read(); $spider = unserialize($fileSpiderContent); } if ($url) { $url = urldecode($url); if (!$spider) { $node = new WebNode($url); } else { $web = $spider->getWeb(); $node = $web->getNode($url); } $view = new ReportDetailView($node); $this->response->setView($view); return; } if ($spider) { $web = $spider->getWeb(); $reports = $spider->getReports(); foreach ($reports as $report) { $report->setWeb($web); } } else { $reports = array(); } $view = new ReportView($reports); $this->response->setView($view); }
/** * Add the files of a directory recursively to the index * @param zibo\library\filesystem\File $path * @param string $prefix * @return null */ private function indexDirectory(File $path, $prefix = null) { $files = $path->read(); foreach ($files as $file) { $name = $file->getName(); if (in_array($name, $this->exclude)) { continue; } $name = $prefix . $name; if (in_array($name, $this->exclude)) { continue; } if ($file->isDirectory()) { $this->indexDirectory($file, $name . File::DIRECTORY_SEPARATOR); } else { $this->indexFile($file, $name); } } }
/** * Clears the result cache for all the queries which use the provided model * @param string $modelName Name of the model * @return null */ public function clearResults($modelName) { $directoryIndex = new File($this->directoryResultIndex, $modelName); if (!$directoryIndex->exists()) { return; } $files = $directoryIndex->read(); foreach ($files as $file) { $this->cache->clear(self::CACHE_TYPE_RESULT, $file->getName()); } $directoryIndex->delete(); }
/** * Extracts a source to destination * @param zibo\library\filesystem\File $source The source phar or directory * @param zibo\library\filesystem\File $destination The destination directory * @return null */ private function extract(File $source, File $destination) { $destination->create(); $files = $source->read(); if (!$files) { return; } foreach ($files as $sourceFile) { $destinationFile = new File($destination, $sourceFile->getName()); if ($sourceFile->isDirectory()) { $this->extract($sourceFile, $destinationFile); } else { $destinationFile->write($sourceFile->read()); } } }