/** * @return $this */ public function boot() { if (!self::$_is_booted) { if (empty($this->_options['temp_dir'])) { $this->_options['temp_dir'] = DirectoryHelper::slashDirname(dirname($_SERVER['SCRIPT_FILENAME'])) . 'tmp'; } if ($this->isMode('production')) { $this->setOption('convert_error_to_exception', true); } AppKernel::boot($this); $charset = $this->get('response')->getCharset(); if (empty($charset)) { $this->get('response')->setCharset($this->getOption('default_charset')); } $content_type = $this->get('response')->getContentType(); if (empty($content_type)) { $this->get('response')->setContentType($this->getOption('default_content_type')); } $routes = $this->getOption('routes'); if (!empty($routes)) { $this->get('router')->setRoutes($routes); } self::$_is_booted = true; $this->trigger('boot'); } return $this; }
/** * @return mixed */ public function current() { if ($this->getFlags() & self::CURRENT_AS_DOCBOOKFILE) { return new DocBookFile(DirectoryHelper::slashDirname($this->original_path) . $this->getFilename()); } return parent::current(); }
/** * Gets the temporary directory * * @return string * @throws \InvalidArgumentException if the path doesn't exist and can't be created */ public static function getTemporaryDirectory() { $exists = DirectoryHelper::ensureExists(self::$tmp_directory); if (@file_exists(self::$tmp_directory)) { return self::$tmp_directory; } else { throw new \InvalidArgumentException(sprintf('Directory "%s" can not be found and can not be created!', self::$tmp_directory)); } }
public function preferencesAction() { $title = _T('Preferences'); $path = DirectoryHelper::slashDirname(FrontController::DOCBOOK_ASSETS) . 'USER_PREFERENCES.md'; $page_infos = array('name' => 'USER_PREFERENCES.md', 'path' => 'prefs', 'update' => Helper::getDateTimeFromTimestamp(filemtime($path))); $tpl_params = array('breadcrumbs' => array($title), 'title' => $title, 'page' => $page_infos, 'page_tools' => 'false'); $file_content = file_get_contents($path); $md_parser = $this->docbook->getMarkdownParser(); $md_content = $md_parser->transformString($file_content); $output_bag = $md_parser->get('OutputFormatBag'); $menu = $output_bag->getHelper()->getToc($md_content, $output_bag->getFormatter()); $content = $this->docbook->display($md_content->getBody(), 'admin_panel', array('page' => $page_infos, 'page_tools' => 'false', 'page_title' => 'true', 'page_notes' => $md_content->getNotesToString(), 'title' => $title, 'toc' => $menu, 'config' => $this->docbook->getRegistry()->getConfigs())); return array('default', $content, $tpl_params); }
public function fallbackFinder($filename, $filetype = 'template') { $docbook = FrontController::getInstance(); $base_path = 'template' === $filetype ? FrontController::TEMPLATES_DIR : FrontController::CONFIG_DIR; $file_path = DirectoryHelper::slashDirname($base_path) . $filename; // user first $user_path = $docbook->getPath('user_dir'); if (!empty($user_path)) { $user_file_path = DirectoryHelper::slashDirname($docbook->getPath('user_dir')) . $file_path; if (file_exists($user_file_path)) { return $user_file_path; } } // default $def_file_path = DirectoryHelper::slashDirname($docbook->getPath('base_dir')) . $file_path; if (file_exists($def_file_path)) { return $def_file_path; } // else false return false; }
/** * @param string $source The source image path * @param string $source_content The source image content (only used if no path is set) * @param string|array $filter A filter name or a list of filters * @param array $filter_options An array of filter options (if the $filter arg is an array, the options must be ordered as $filters) * @param string $target_filename */ public function __construct($source = null, $source_content = null, $filter = null, $filter_options = null, $target_filename = null) { self::$tmp_directory = DirectoryHelper::slashDirname(MediaProcessor::getTemporaryDirectory()); $this->target_directory = DirectoryHelper::slashDirname(realpath(self::$tmp_directory)); DirectoryHelper::ensureExists($this->target_directory); $this->resetFilters(); if (!is_null($source)) { $this->setSourceFile($source); } elseif (!is_null($source_content)) { $this->buildSourceFileFromContent($source_content); } if (!is_null($target_filename)) { $this->setTargetFilename($target_filename); } if (!is_null($filter)) { if (is_string($filter)) { $this->addFilter($filter, $filter_options); } else { $this->setFilters($filter, $filter_options); } } }
/** * Find an asset file in a package's path * * @param string $filename The asset filename to find * @param string $path The path to search from * @return string|null The web path of the asset if found, `null` otherwise */ public static function findInPath($filename, $path) { $asset_path = DirectoryHelper::slashDirname($path) . $filename; if (file_exists($asset_path)) { return self::buildWebPath($asset_path); } return null; }
/** * @param string $path Relative to `vendor` * @param string $type Type of the original relative path (can be `asset` or `vendor` or `null` - default is `vendor`) * @return self * @throws \InvalidArgumentException if the path doesn't exist */ public function addViewsFunctionsPath($path, $type = 'vendor') { $realpath = $this->getFullPath($path, $type); if (@file_exists($realpath) && is_file($realpath)) { if (!in_array($path, $this->views_functions_paths)) { $this->views_functions_paths[] = $path; } } else { $relative_path = DirectoryHelper::slashDirname($this->getRelativePath()) . $path; $realpath = $this->getFullPath($relative_path, null); if (@file_exists($realpath) && is_file($realpath)) { if (!in_array($relative_path, $this->views_functions_paths)) { $this->views_functions_paths[] = $relative_path; } } else { throw new InvalidArgumentException(sprintf('Views functions file "%s" for package "%s" not found !', $path, $this->getName())); } } return $this; }
/** * Copy file `$file_path` if it exists to `$target_path` * * @param string $file_path * @param string $target_path * @param bool $force * @param array $logs Logs registry passed by reference * @return bool */ public static function copy($file_path = null, $target_path = null, $force = false, array &$logs = array()) { if (is_null($file_path) || is_null($target_path)) { return null; } if (!file_exists($file_path)) { $logs[] = sprintf('File path "%s" to copy was not found.', $file_path); return false; } $target_dir = dirname($target_path); $ok = !file_exists($target_dir) && true === $force ? Directory::create($target_dir) : true; if ($ok) { if (!file_exists($target_path) || true === $force) { if (copy($file_path, $target_path)) { return true; } else { $logs[] = sprintf('Can not copy file "%s" to "%s".', $file_path, $target_path); } } else { $logs[] = sprintf('Can not over-write target file "%s" by copy (use param `$force=true`).', $target_path); } } else { $logs[] = sprintf('Can not create target directory "%s" for copy.', $target_dir); } return false; }
public static function getGitConfig($repo_path) { if (DirectoryHelper::isGitClone($repo_path)) { $repo_config_file = DirectoryHelper::slashDirname($repo_path) . DirectoryHelper::slashDirname('.git') . 'config'; if (file_exists($repo_config_file)) { try { $config = parse_ini_file($repo_config_file, true); return $config; } catch (\Exception $e) { } } } return null; }
/** * Get the spooled mails directory * * @return string */ public function getSpoolDirectory() { return !empty($this->spool_dir) ? DirectoryHelper::slashDirname($this->spool_dir) : ''; }
public function getDbRealpath() { return DirectoryHelper::slashDirname($this->getDbFilepath()) . $this->getDbFilename(); }
/** * Initializer : this method is called after any instance creation * * @param object $request \Library\HttpFundamental\Request * @param object $response \Library\HttpFundamental\Response * @param array $user_options */ protected function init(Request $request = null, Response $response = null, array $user_options = array()) { $this->setUserOptions($this->extendOptions($user_options)); foreach (array('tmp_directory', 'var_directory', 'log_directory') as $_dir) { DirectoryHelper::ensureExists($this->getOption($_dir)); } $this->setLogger(new Logger($this->getLoggerOptions())); if (!is_null($request)) { $this->setRequest($request); } if (!is_null($response)) { $this->setResponse($response); } $this->getResponse()->setContentType('json'); if ($this->getOption('enable_url_rewrite') === true) { $this->getRequest()->setFlag(Request::REWRITE_SEGMENTS_QUERY)->setArguments($this->getRequest()->getArguments()); } /* echo '<pre>'; var_export($this->getRequest()); exit('yo'); */ }
public function distribute($return = false) { $this->processSessionValues(); $routing = $this->request->parseDocBookRequest()->getDocBookRouting(); $this->processQueryArguments(); $input_file = $this->getInputFile(); if (empty($input_file)) { $input_path = $this->getInputPath(); if (!empty($input_path)) { $input_file = DirectoryHelper::slashDirname($this->getPath('base_dir_http')) . trim($input_path, '/'); } } $result = null; if (!empty($routing)) { $ctrl_cls = $routing['controller_classname']; $ctrl_obj = new $ctrl_cls(); $this->setController($ctrl_obj); $result = Helper::fetchArguments($this->getController(), $routing['action'], array('path' => $input_file)); } if (empty($result) || !is_array($result) || count($result) != 2 && count($result) != 3) { $str = gettype($result); if (is_array($result)) { $str .= ' length ' . count($result); } throw new DocBookRuntimeException(sprintf('A controller action must return a two or three entries array like [ template file , content (, params) ]! Received %s from class "%s::%s()".', $str, $routing['controller_classname'], $routing['action'])); } else { $template = $result[0]; $content = $result[1]; $params = isset($result[2]) ? $result[2] : array(); } // for dev if (!empty($_GET) && isset($_GET['dbg'])) { $this->debug(); } $this->display($content, $template, $params, true); }
/** * Gets the object's web path * * @return string The object's web path, without the file name */ public function getWebPath() { return !empty($this->web_path) ? str_replace($this->getRootDir(), '', DirectoryHelper::slashDirname($this->web_path)) : ''; }
public function getDocBookRouting() { $docbook = FrontController::getInstance(); $original_page_type = $docbook->getAction(); $page_type = !empty($original_page_type) ? $original_page_type : 'default'; $input_file = $docbook->getInputFile(); if (empty($input_file)) { $input_path = $docbook->getInputPath(); if (!empty($input_path)) { $input_file = DirectoryHelper::slashDirname($docbook->getPath('base_dir_http')) . trim($input_path, '/'); } } $ctrl_infos = $docbook->getLocator()->findController($page_type); if ($ctrl_infos) { $this->setRouting($ctrl_infos); } else { if (!empty($original_page_type)) { throw new NotFoundException(sprintf('The requested "%s" action was not found!', $original_page_type)); } else { throw new NotFoundException(sprintf('The requested page was not found (searching "%s")!', $input_file)); } } return $this->getRouting(); }
/** * Search a view file in the current file system * * @parame string $path The file path to search * @return string The path of the file found */ public function getTemplate($path) { if (file_exists($path)) { return $path; } elseif (false !== ($path_rp = realpath($path))) { return $path_rp; } else { foreach ($this->getIncludePath() as $_path) { $_f = DirectoryHelper::slashDirname($_path) . $path; if (file_exists($_f)) { return $_f; } } } return null; }
/** * @covers ../../../src/Library/Helper/File::write() */ public function test_write() { $this->checkNoArg('remove'); \Library\Helper\Directory::ensureExists(self::$tmp_dir); \Library\Helper\File::touch(self::$tmp_file); $logs = array(); $this->assertTrue(\Library\Helper\File::write(self::$tmp_file, $this->lorem_ipsum)); $this->assertEmpty($logs); $ctt = trim(file_get_contents(self::$tmp_file)); $this->assertEquals($this->lorem_ipsum, $ctt); }
public function findIndex() { if (!isset($this->cache['docbook_index'])) { $index = DirectoryHelper::slashDirname($this->getRealPath()) . FrontController::INDEX_FILE; $this->cache['docbook_index'] = file_exists($index) ? $index : null; } return $this->cache['docbook_index']; }
/** * Overwriting the default `current()` method to return a `WebFileInfo` object if so * * If the flag `CURRENT_AS_WEBFILEINFO` is active, this will return a `WebFileInfo` object. * * @return mixed The value of the current iteration, as a `WebFileInfo` object if requested or as the default result of the parent's method */ public function current() { if ($this->getFlags() & WebFilesystemIterator::CURRENT_AS_WEBFILEINFO) { /* if ($this->isLink()) { return new WebFileInfo($this->getPathname()); } else { $realpath = $this->getRealPath(); $path = $this->getPathname(); return new WebFileInfo($path, str_replace($path, '', $realpath)); } */ return new WebFileInfo(DirectoryHelper::slashDirname($this->original_path) . $this->getFilename()); } return parent::current(); }
</pre> <h4 id="dirhelper">Library\Helper\Directory</h4> <pre class="code" data-language="php"> <?php $logs = array(); $dir = __DIR__ . '/tmp/tmp_tmp'; echo '$logs = array();' . "\n"; echo '$dir = __DIR__."/tmp/tmp_tmp";' . "\n"; \Library\Helper\Directory::ensureExists($dir); \Library\Helper\File::touch($dir . '/test1'); \Library\Helper\File::touch($dir . '/test2'); \Library\Helper\File::touch($dir . '/test/test1'); \Library\Helper\File::touch($dir . '/test/test2'); \Library\Helper\Directory::chmod($dir, 777, true, 766, $logs); \Library\Helper\Directory::remove($dir, $logs); echo "\n"; echo '\\Library\\Helper\\Directory::ensureExists($dir);' . "\n"; echo '\\Library\\Helper\\File::touch($dir."/test1");' . "\n"; echo '\\Library\\Helper\\File::touch($dir."/test2");' . "\n"; echo '\\Library\\Helper\\File::touch($dir."/test/test1");' . "\n"; echo '\\Library\\Helper\\File::touch($dir."/test/test2");' . "\n"; echo '\\Library\\Helper\\Directory::chmod($dir, 777, true, 766, $logs);' . "\n"; echo '\\Library\\Helper\\Directory::remove($dir, $logs);' . "\n"; echo "\n"; echo 'var_export($logs);' . "\n"; var_export($logs); ?> </pre> <h4 id="codehelper">Library\Helper\Code</h4>
/** * @param \Composer\Package\PackageInterface $package * @return string */ protected function getPackageAssetsBasePath(PackageInterface $package) { return DirectoryHelper::slashDirname($this->getRootPackageAssetsVendorPath()) . $package->getPrettyName(); }
/** * Get the relative path in the package * * @param string $path The relative path to complete * @return string */ public function getRelativeFullPath($path) { return DirectoryHelper::slashDirname($this->getRelativePath()) . $path; }
/** * Get the assets vendor full path * * @return string */ public function getAssetsVendorRealPath() { return DirectoryHelper::slashDirname($this->getAssetsRealPath()) . $this->getAssetsVendorDirectory(); }
/** * Find (and add if needed) a language file from options directories * * @param string $db_filename * @param string $db_directory * @return null|string */ public function findLanguageDBFile($db_filename = null, $db_directory = null) { if (empty($db_filename)) { $db_filename = $this->buildLanguageDBFileName(); } if (empty($db_directory)) { $db_directory = $this->buildLanguageDBDirName(); } $_file = null; $tmp_file = DirectoryHelper::slashDirname($db_directory) . $db_filename; if (file_exists($tmp_file)) { $_file = $tmp_file; } else { foreach ($this->_paths['language_strings_db_directory'] as $dir) { $tmp_file = DirectoryHelper::slashDirname($dir) . $db_filename; if (file_exists($tmp_file)) { $_file = $tmp_file; } } } $this->addPath($db_filename, $db_directory, $_file); return $_file; }
/** * @param \MVCFundamental\Interfaces\FrontControllerInterface $app * @return void * @throws \MVCFundamental\Exception\ErrorException */ public static function boot(FrontControllerInterface $app) { // stores the FrontController self::setFrontController($app); // define internal handlers set_exception_handler(array(__CLASS__, 'handleException')); register_shutdown_function(array(__CLASS__, 'handleShutdown')); if (self::getFrontController()->getOption('convert_error_to_exception') == true) { set_error_handler(array(__CLASS__, 'handleError')); } // the required temporary directory $tmp_dir = self::getFrontController()->getOption('temp_dir'); if (empty($tmp_dir) || !file_exists($tmp_dir) && !@DirectoryHelper::create($tmp_dir) || !is_dir($tmp_dir) || !is_writable($tmp_dir)) { $tmp_dir = DirectoryHelper::slashDirname(sys_get_temp_dir()) . 'mvc-fundamental'; if (!@DirectoryHelper::ensureExists($tmp_dir)) { throw new ErrorException(sprintf('The "%s" temporary directory can not be created or is not writable (and a default one could not be taken)!', $tmp_dir)); } self::getFrontController()->setOption('temp_dir', $tmp_dir); } // the application logger if (self::getFrontController()->getOption('minimum_log_level') == null) { self::getFrontController()->setOption('log_level', self::getFrontController()->isMode('production') ? Logger::WARNING : Logger::DEBUG); } $logger_options = array('duplicate_errors' => false, 'directory' => self::getFrontController()->getOption('temp_dir'), 'minimum_log_level' => self::getFrontController()->getOption('log_level')); $logger_class = self::getFrontController()->getOption('app_logger'); if (!class_exists($logger_class) || !Helper::classImplements($logger_class, self::getApi('logger'))) { throw new ErrorException(sprintf('A logger must exist and implement the "%s" interface (for class "%s")!', self::getApi('logger'), $logger_class)); } self::set('logger', new $logger_class($logger_options)); // load services foreach (self::getFrontController()->getOptions() as $var => $val) { if (array_key_exists($var, self::$_api)) { self::set($var, self::apiFactory($var, $val)); } if (array_key_exists($var, self::$_constructors)) { $cls_index = self::$_constructors[$var]; self::getInstance()->setProvider($var, function ($app, $name, array $arguments = array()) use($val, $cls_index) { return $app::apiFactory($cls_index, $val, $arguments); }); } } self::get('event_manager')->setEventClass(self::getFrontController()->getOption('event_item')); }
/** * @covers ../../../src/Library/Helper/Directory::remove() */ public function test_remove() { $this->checkNoArg('remove'); \Library\Helper\File::touch(self::$tmp_file); $logs = array(); $this->assertTrue(\Library\Helper\Directory::remove(self::$tmp_dir, $logs)); $this->assertEmpty($logs); }