/** * @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')); }
/** * 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; }