/** * execute tests * * @param string $action * @param Charcoal_IEventContext $context * * @return boolean */ public function test($action, $context) { $action = us($action); // temp file component /** @var Charcoal_TempFileComponent $tf */ $tf = $context->getComponent('temp_file@:charcoal:file'); switch ($action) { case "create": $file = $tf->create("test"); $this->assertTrue($file->exists()); $this->assertTrue($file->canRead()); $this->assertEquals("test", $file->getContents()); return TRUE; case "get_contents": $temp_file = new Charcoal_File(CHARCOAL_TMP_DIR . '/tmpfile.txt'); $temp_file->putContents("test"); $tf->setFile($temp_file); $this->assertEquals("test", $tf->getContents()); return TRUE; case "put_contents": $temp_file = new Charcoal_File(CHARCOAL_TMP_DIR . '/tmpfile.txt'); $tf->setFile($temp_file); $tf->putContents("cat"); $this->assertTrue($temp_file->exists()); $this->assertTrue($temp_file->canRead()); $this->assertEquals("cat", $temp_file->getContents()); return TRUE; } return FALSE; }
/** * イベントを処理する */ public function processEvent($context) { $event = $context->getEvent(); // パラメータを取得 $app_name = $event->getAppName(); $project_name = $event->getProjectName(); $out_dir = $event->getTargetDir(); //======================================= // Confirm input parameters //======================================= if (!preg_match('/^[0-9a-zA-Z_\\-]*$/', $app_name)) { _throw(new Charcoal_InvalidArgumentException($app_name)); } if (!preg_match('/^[0-9a-zA-Z_\\-]*$/', $project_name)) { _throw(new Charcoal_InvalidArgumentException($project_name)); } //======================================= // Make output directory //======================================= $out_dir = new Charcoal_File($out_dir); $out_dir->makeDirectory(self::DIR_MODE); //======================================= // Make web_app directory //======================================= $webapp_dir = new Charcoal_File('web_app', $out_dir); $webapp_dir->makeDirectory(self::DIR_MODE); //======================================= // Make project directory //======================================= $project_dir = new Charcoal_File($project_name, $webapp_dir); $project_dir->makeDirectory(self::DIR_MODE); //======================================= // Make project/app directory //======================================= $project_app_dir = new Charcoal_File('app', $project_dir); $project_app_dir->makeDirectory(self::DIR_MODE); //======================================= // Make application directory //======================================= $application_dir = new Charcoal_File($app_name, $project_app_dir); $application_dir->makeDirectory(self::DIR_MODE); //======================================= // Make application/class directory //======================================= $app_class_dir = new Charcoal_File('class', $application_dir); $app_class_dir->makeDirectory(self::DIR_MODE); //======================================= // Make application/config directory //======================================= $app_config_dir = new Charcoal_File('config', $application_dir); $app_config_dir->makeDirectory(self::DIR_MODE); //======================================= // Make application/module directory //======================================= $module_dir = new Charcoal_File('module', $application_dir); $module_dir->makeDirectory(self::DIR_MODE); echo "Application[{$app_name}] created at: " . $application_dir->getAbsolutePath() . PHP_EOL; return b(true); }
public static function getMime(Charcoal_File $image_file) { if (!$image_file->exists()) { return NULL; } $data = getimagesize(us($image_file->getPath())); if ($data === FALSE) { _throw(new Charcoal_ImageGetSizeException($image_file)); } return $data['mime']; }
public function load($debug_mode, $profile_name) { $config_file = "{$profile_name}.profile.ini"; try { // get profile directory path $profile_dir = Charcoal_ResourceLocator::getApplicationFile('config/profile'); // make config file object $config_file = new Charcoal_File($config_file, $profile_dir); // check if profile directory exists if (!$profile_dir->exists()) { if ($debug_mode) { echo "profile directory not exists: [{$profile_dir}]" . eol(); } log_error("debug,config,profile", "profile directory not exists: [{$profile_dir}]"); _throw(new Charcoal_ProfileDirectoryNotFoundException($profile_dir)); } // throw exception when config file is not found if (!$config_file->isFile() || !$config_file->canRead()) { if ($debug_mode) { echo "profile config file not exists or not readable: [{$config_file}]" . eol(); } log_error("debug,config,profile", "profile config file not exists or not readable: [{$config_file}]"); _throw(new Charcoal_ProfileConfigFileNotFoundException($config_file)); } // parse config file // log_debug( "debug,config,profile", "profile", "parsing config file: [$config_file]" ); $config_file = $config_file->getAbsolutePath(); if ($debug_mode) { echo "executing parse_ini_file: [{$config_file}]" . eol(); } $config = @parse_ini_file($config_file, FALSE); if ($config === FALSE) { if ($debug_mode) { echo "profile config file format error: [{$config_file}]" . eol(); } log_error("debug,config,profile", "profile config file format error: [{$config_file}]"); _throw(new Charcoal_ProfileConfigFileFormatException($config_file)); } if ($debug_mode) { echo "executed parse_ini_file: " . ad($config) . eol(); } // log_debug( "profile", "profile", "parse_ini_file: " . print_r($config,TRUE) ); // 設定を保存 parent::mergeArray($config); // log_debug( "debug,config,profile", "profile", "marged profile:" . print_r($config,TRUE) ); } catch (Exception $ex) { // log_debug( "system,error,debug", "catch $e" ); _catch($ex); _throw(new Charcoal_ProfileLoadingException($config_file, $profile_name, $ex)); } }
/** * Output file * * @param Charcoal_File $file path to output * @param array $lines each line of the file to output */ public static function outputFile(Charcoal_File $file, array $lines) { $file_name = $file->getPath(); $fp = fopen($file_name, "w"); if ($fp === FALSE) { _throw(new Charcoal_FileOpenException($file)); } foreach ($lines as $line) { $res = fwrite($fp, $line . PHP_EOL); if ($res === FALSE) { _throw(new Charcoal_FileOutputException($file)); } } fclose($fp); }
/** * Check if the filter select the specified file. * * @param Charcoal_File $file Target fileto be tested. */ public function accept(Charcoal_File $file) { if ($this->extension) { $ext = $file->getExtension(); if ($ext != $this->extension) { return FALSE; } } $suffix = $this->extension ? s('.' . $this->extension) : NULL; $name = $suffix ? $file->getName($suffix) : $file->getName(); if (preg_match($this->pattern, $name)) { return TRUE; } return FALSE; }
public function open() { // すでに開いているならなにもしない if ($this->open) { return; } $file_name = rtrim($this->logs_dir, ' /') . '/' . ltrim($this->file_name, ' /'); $dir = new Charcoal_File(dirname($file_name)); // ディレクトリを作成 try { $dir->makeDirectory(); } catch (Exception $e) { print "FATAL error occured while output log file:{$this->file_name} error={$e}" . PHP_EOL; } $this->fp = fopen($file_name, "a"); $this->open = $this->fp != FALSE; }
/** * render buffer * * @param Charcoal_String|string $buffer rendering data */ public function render($buffer) { Charcoal_ParamTrait::validateString(1, $buffer); $buffer = us($buffer); if (is_string($buffer)) { $dir = new Charcoal_File(dirname($this->file_path)); if (!$dir->exists()) { $dir->makeDirectory($this->dir_mode); } if (!$dir->isWriteable()) { _throw(new Charcoal_RenderTargetException("directory not writable: {$dir}")); } $result = @file_put_contents($this->file_path, $buffer, LOCK_EX); if ($result === FALSE) { $last_error = print_r(error_get_last(), true); _throw(new Charcoal_RenderTargetException("file_put_contents failed: {$this->file_path} last error: {$last_error}")); } } }
public function __construct($userfile) { $file = $_FILES[us($userfile)]; $this->name = $file['name']; $this->type = $file['type']; $this->size = $file['size']; $this->tmp_name = $file['tmp_name']; $this->error = $file['error']; parent::__construct($this->tmp_name); }
/** * get cache * * @param Charcoal_String $cache_key identify cache * @param Charcoal_File $source config file * * @return mixed configure data */ public function getCache(Charcoal_String $cache_key, Charcoal_File $source) { $cache_dir = $this->getSandbox()->getProfile()->getString('CACHE_DIR'); $cache_dir = new Charcoal_File(s($cache_dir)); $cache_file = new Charcoal_File(s($cache_key), $cache_dir); if (!$cache_file->isFile()) { return FALSE; } if ($source->isFile()) { $lm_cache = $cache_file->getLastModified(); $lm_source = $source->getLastModified(); if ($lm_cache === FALSE || $lm_source === FALSE) { return FALSE; } if ($lm_cache >= $lm_source) { return FALSE; } } return unserialize($cache_file->getContents()); }
public function __construct($path, $parent = NULL) { Charcoal_ParamTrait::validateString(1, $path); Charcoal_ParamTrait::validateIsA(2, 'Charcoal_File', $parent, TRUE); parent::__construct($path, $parent); if (!is_readable(parent::getPath())) { _throw(new Charcoal_FileNotFoundException($this)); } $this->_data = getimagesize(parent::getPath()); if ($this->_data === FALSE) { _throw(new Charcoal_ImageGetSizeException($this)); } }
public function createFile($file_path, $contents, $overwrite = TRUE, $mode = 0777) { Charcoal_ParamTrait::validateString(1, $file_path); Charcoal_ParamTrait::validateString(2, $contents); Charcoal_ParamTrait::validateBoolean(3, $overwrite); Charcoal_ParamTrait::validateInteger(4, $mode); $obj = new Charcoal_File($file_path, $this->_base_dir_obj); if ($overwrite) { if ($obj->exists() && !$obj->canWrite()) { _throw(new Charcoal_FileSystemComponentException('specified file is not writeable.')); } } elseif ($obj->exists()) { _throw(new Charcoal_FileSystemComponentException('specified file is already exists.')); } try { // create file with parent directory $obj->makeFile($mode, $contents, TRUE); return $obj; } catch (Exception $e) { _catch($e); _throw(new Charcoal_FileSystemComponentException(s('creating file failed.'), $e)); } }
/** * prepare output directory * * @param string $out_dir * * @return Charcoal_File */ private function prepareOutputDirectory($out_dir) { $out_dir = new Charcoal_File($out_dir); $out_dir->makeDirectory(self::DIR_MODE); }
/** * イベントを処理する */ public function processEvent($context) { $event = $context->getEvent(); // パラメータを取得 $project_name = $event->getProjectName(); $out_dir = $event->getTargetDir(); //======================================= // Confirm input parameters //======================================= if (empty($project_name) || !preg_match('/^[0-9a-zA-Z_\\-]*$/', $project_name)) { _throw(new Charcoal_InvalidArgumentException($project_name)); } //======================================= // Make output directory //======================================= $out_dir = new Charcoal_File($out_dir); $out_dir->makeDirectory(self::DIR_MODE); //======================================= // Make public directory //======================================= $public_dir = new Charcoal_File('public_html', $out_dir); $public_dir->makeDirectory(self::DIR_MODE); //======================================= // Make ext_lib directory //======================================= $ext_lib_dir = new Charcoal_File('ext_lib', $out_dir); $ext_lib_dir->makeDirectory(self::DIR_MODE); //======================================= // Make logs directory //======================================= $logs_dir = new Charcoal_File('logs', $out_dir); $logs_dir->makeDirectory(self::DIR_MODE); //======================================= // Make sessions directory //======================================= $sessions_dir = new Charcoal_File('sessions', $out_dir); $sessions_dir->makeDirectory(self::DIR_MODE); //======================================= // Make cache directory //======================================= $cache_dir = new Charcoal_File('cache', $out_dir); $cache_dir->makeDirectory(self::DIR_MODE); //======================================= // Make tmp directory //======================================= $tmp_dir = new Charcoal_File('tmp', $out_dir); $tmp_dir->makeDirectory(self::DIR_MODE); //======================================= // Make web_app directory //======================================= $webapp_dir = new Charcoal_File('web_app', $out_dir); $webapp_dir->makeDirectory(self::DIR_MODE); //======================================= // Make project directory //======================================= $project_dir = new Charcoal_File($project_name, $webapp_dir); $project_dir->makeDirectory(self::DIR_MODE); //======================================= // Make project/app directory //======================================= $project_app_dir = new Charcoal_File('app', $project_dir); $project_app_dir->makeDirectory(self::DIR_MODE); //======================================= // Make project/config directory //======================================= $project_config_dir = new Charcoal_File('config', $project_dir); $project_config_dir->makeDirectory(self::DIR_MODE); echo "Project[{$project_name}] created at: " . $project_dir->getAbsolutePath() . PHP_EOL; return b(true); }
/** * イベントを処理する */ public function processEvent($context) { $event = $context->getEvent(); // パラメータを取得 $module_path = $event->getModulePath(); $app_name = $event->getAppName(); $project_name = $event->getProjectName(); $out_dir = $event->getTargetDir(); //======================================= // Confirm input parameters //======================================= if (!preg_match('/^[@:0-9a-zA-Z_\\-]*$/', $module_path)) { _throw(new Charcoal_InvalidArgumentException($module_path)); } if (!preg_match('/^[0-9a-zA-Z_\\-]*$/', $app_name)) { _throw(new Charcoal_InvalidArgumentException($app_name)); } if (!preg_match('/^[0-9a-zA-Z_\\-]*$/', $project_name)) { _throw(new Charcoal_InvalidArgumentException($project_name)); } //======================================= // Make output directory //======================================= $out_dir = new Charcoal_File($out_dir); $out_dir->makeDirectory(self::DIR_MODE); //======================================= // Make web_app directory //======================================= $webapp_dir = new Charcoal_File('web_app', $out_dir); $webapp_dir->makeDirectory(self::DIR_MODE); //======================================= // Make project directory //======================================= $project_dir = new Charcoal_File($project_name, $webapp_dir); $project_dir->makeDirectory(self::DIR_MODE); //======================================= // Make project/app directory //======================================= $project_app_dir = new Charcoal_File('app', $project_dir); $project_app_dir->makeDirectory(self::DIR_MODE); //======================================= // Make application directory //======================================= $application_dir = new Charcoal_File($app_name, $project_app_dir); $application_dir->makeDirectory(self::DIR_MODE); //======================================= // Make application/module directory //======================================= $module_dir = new Charcoal_File('module', $application_dir); $module_dir->makeDirectory(self::DIR_MODE); //======================================= // Make target module directory //======================================= $module_path = new Charcoal_ObjectPath($module_path); $target_module_dir = new Charcoal_File($module_path->getRealPath(), $module_dir); $target_module_dir->makeDirectory(self::DIR_MODE); //======================================= // Make procedure.ini file //======================================= $lines = NULL; $lines[] = "debug_mode = false"; $lines[] = "class_name = Charcoal_SimpleProcedure"; $lines[] = "task_manager = default_task_manager"; $lines[] = "tasks = "; $lines[] = "modules = "; $lines[] = "events = "; $lines[] = "log_enabled = yes"; $lines[] = "log_level = E"; $lines[] = "log_loggers = error"; $outfile = new Charcoal_File('procedure.ini', $target_module_dir); Charcoal_FileSystemUtil::outputFile($outfile, $lines); echo "Module[{$module_path}] created at: " . $target_module_dir->getAbsolutePath() . PHP_EOL; return b(true); }
/** * create directory * * @return Charcoal_File file object of created directory */ public function create() { try { $obj = new Charcoal_File(s($this->_dir_path)); $obj->makeDirectory($this->_mode, b(TRUE)); return $obj; } catch (Exception $e) { _catch($e); _throw(new Charcoal_TempDirComponentException(s('creating directory failed.'), $e)); } }
/** * クリーンアップ */ public function cleanUp($action, $context) { $action = us($action); switch ($action) { case "get_empty_data": case "get_integer_data": case "get_string_data": case "get_array_data": case "get_boolean_data": case "get_float_data": case "get_object_data": case "set_duration": case "delete": case "delete_wildcard": case "delete_regex": case "touch": case "touch_wildcard": case "touch_regex": $cache_files = glob($this->cache_root . '/*'); if ($cache_files && is_array($cache_files)) { foreach ($cache_files as $cache) { $file = new Charcoal_File(s($cache)); $name = $file->getName(); $ext = $file->getExtension(); if ($file->exists() && $file->isFile() && in_array($ext, array("meta", "data"))) { $file->delete(); echo "removed cache file: [{$cache}]" . eol(); } } } break; default: break; } }
/** * create file * * @param string|Charcoal_String $contents * @param Charcoal_File $dir * @param string|Charcoal_String $file_name * * @return Charcoal_File */ public function create($contents, $dir = null, $file_name = null) { if ($file_name === null) { $tmp_filename = Charcoal_System::hash() . '.tmp'; } if ($dir === null) { $dir = Charcoal_ResourceLocator::getFile($this->getSandbox()->getEnvironment(), "%TMP_DIR%"); } $tmp_file = new Charcoal_File($file_name, $dir); if ($tmp_file->isDirectory()) { _throw(new Charcoal_FileSystemComponentException('specified path is directory.')); } if ($tmp_file->exists()) { _throw(new Charcoal_FileSystemComponentException('specified file is already exists.')); } if ($this->overwrite) { if ($tmp_file->exists() && !$tmp_file->canWrite()) { _throw(new Charcoal_FileSystemComponentException('specified file is not writeable.')); } } try { // create file $tmp_file->makeFile($this->mode, $contents, TRUE); $this->file = $tmp_file; return $tmp_file; } catch (Exception $e) { _catch($e); _throw(new Charcoal_TempFileComponentException(s('creating file failed.'), $e)); } return null; }
/** * Rename the file or directory * * @param Charcoal_File $new_file */ public function rename($new_file) { $res = rename($this->path, $new_file->getPath()); if ($res === FALSE) { _throw(new Charcoal_FileRenameException($this->getPath(), $new_file->getPath())); } }
/** * イベントを処理する */ public function processEvent($context) { $event = $context->getEvent(); // パラメータを取得 $task_name = $event->getTaskName(); $module_path = $event->getModulePath(); $app_name = $event->getAppName(); $project_name = $event->getProjectName(); $out_dir = $event->getTargetDir(); //======================================= // Confirm input parameters //======================================= if (!preg_match('/^[0-9a-zA-Z_\\-]*$/', $task_name)) { _throw(new Charcoal_InvalidArgumentException($task_name)); } if (!preg_match('/^[@:0-9a-zA-Z_\\-]*$/', $module_path)) { _throw(new Charcoal_InvalidArgumentException($module_path)); } if (!preg_match('/^[0-9a-zA-Z_\\-]*$/', $app_name)) { _throw(new Charcoal_InvalidArgumentException($app_name)); } if (!preg_match('/^[0-9a-zA-Z_\\-]*$/', $project_name)) { _throw(new Charcoal_InvalidArgumentException($project_name)); } //======================================= // Make output directory //======================================= $out_dir = new Charcoal_File($out_dir); $out_dir->makeDirectory(self::DIR_MODE); //======================================= // Make web_app directory //======================================= $webapp_dir = new Charcoal_File('web_app', $out_dir); $webapp_dir->makeDirectory(self::DIR_MODE); //======================================= // Make project directory //======================================= $project_dir = new Charcoal_File($project_name, $webapp_dir); $project_dir->makeDirectory(self::DIR_MODE); //======================================= // Make project/app directory //======================================= $project_app_dir = new Charcoal_File('app', $project_dir); $project_app_dir->makeDirectory(self::DIR_MODE); //======================================= // Make application directory //======================================= $application_dir = new Charcoal_File($app_name, $project_app_dir); $application_dir->makeDirectory(self::DIR_MODE); //======================================= // Make application/module directory //======================================= $module_dir = new Charcoal_File('module', $application_dir); $module_dir->makeDirectory(self::DIR_MODE); //======================================= // Make target module directory //======================================= $module_path = new Charcoal_ObjectPath($module_path); $target_module_dir = new Charcoal_File($module_path->getRealPath(), $module_dir); $target_module_dir->makeDirectory(self::DIR_MODE); //======================================= // Make task.ini file //======================================= $task_class_name = str_replace(' ', '', ucwords(str_replace('_', ' ', $task_name))) . 'Task'; $lines = NULL; $lines[] = "class_name = {$task_class_name}"; $lines[] = "event_filters = "; $outfile = new Charcoal_File($task_name . '_task.task.ini', $target_module_dir); Charcoal_FileSystemUtil::outputFile($outfile, $lines); //======================================= // Make task class file //======================================= $lines = NULL; $lines[] = "<?php"; $lines[] = "/**"; $lines[] = " * (Auto Generated Class)"; $lines[] = " * {$task_class_name} class"; $lines[] = " * "; $lines[] = " * generated by CharcoalPHP ver." . Charcoal_Framework::getVersion(); $lines[] = " * "; $lines[] = " * @author your name"; $lines[] = " * @copyright "; $lines[] = " */"; $lines[] = "class {$task_class_name} extends Charcoal_Task"; $lines[] = "{"; $lines[] = " /**"; $lines[] = " * Process user/system event"; $lines[] = " *"; $lines[] = " * @param Charcoal_EventContext \$context Event context"; $lines[] = " * "; $lines[] = " * @return bool|Charcoal_Boolean Return true if the event is processed. Or you can "; $lines[] = " * return false if another task should process the event."; $lines[] = " */"; $lines[] = " public function processEvent( \$context )"; $lines[] = " {"; $lines[] = " \$request = \$context->getRequest();"; $lines[] = " \$response = \$context->getResponse();"; $lines[] = " \$sequence = \$context->getSequence();"; $lines[] = " \$procedure = \$context->getProcedure();"; $lines[] = " "; $lines[] = " // TODO: write processing event code here"; $lines[] = " \$name = \$request->getString('name', 'john');"; $lines[] = " "; $lines[] = " return b(true);"; $lines[] = " }"; $lines[] = "}"; $outfile = new Charcoal_File($task_class_name . '.class.php', $target_module_dir); Charcoal_FileSystemUtil::outputFile($outfile, $lines); echo "Task[{$task_name}] created at: " . $target_module_dir->getAbsolutePath() . PHP_EOL; return b(true); }