public static function makeRelativeURL($sandbox, $obj_path, $params = NULL) { Charcoal_ParamTrait::validateSandbox(1, $sandbox); Charcoal_ParamTrait::validateStringOrObject(2, 'Charcoal_ObjectPath', $obj_path); Charcoal_ParamTrait::validateHashMap(3, $params, TRUE); if (is_string(us($obj_path))) { $obj_path = new Charcoal_ObjectPath($obj_path); } // プロシージャキーを取得 $proc_key = $sandbox->getProfile()->getString('PROC_KEY', 'proc'); // URLを生成 $url = '/?' . us($proc_key) . '=' . $obj_path->getObjectName() . "@" . $obj_path->getVirtualPath(); // パラメータ部分 if ($params) { foreach ($params as $key => $value) { $url .= '&' . $key . '=' . $value; } } return $url; }
/** * load a rask * * @param Charcoal_ObjectPath $obj_path * @param Charcoal_String|string $path * @param Charcoal_ITaskManager $task_manager * * @return Charcoal_ITask|NULL */ private function loadTask($obj_path, $path, $task_manager) { // Charcoal_ParamTrait::validateObjectPath( 1, $obj_path ); // Charcoal_ParamTrait::validateString( 2, $path ); // Charcoal_ParamTrait::validateImplements( 3, 'Charcoal_ITaskManager', $task_manager ); // file base name $base_name = basename($path); // retrieve class name from file name $pos = strpos($base_name, '.class.php'); $class_name = substr($base_name, 0, $pos); // include source file Charcoal_Framework::loadSourceFile($path); // create new instance $klass = new Charcoal_Class($class_name); $task = $klass->newInstance(); // check if th object implements Charcoal_Task interface if (!$task instanceof Charcoal_Task) { // Invoke Exception _throw(new Charcoal_InterfaceImplementException($task, 'Charcoal_Task')); } log_info('system, event, debug', "created task[{$task}] in module[{$obj_path}]"); // build object path for the task $obj_name = $task->getObjectName(); $task_path = s($obj_name . '@' . $obj_path->getVirtualPath()); // set task property $task->setObjectPath($task_path); $task->setTypeName('task'); $task->setSandbox($this->getSandbox()); // load object config $config = Charcoal_ConfigLoader::loadConfig($this->getSandbox(), $task_path, 'task'); $config = new Charcoal_Config($this->getSandbox()->getEnvironment(), $config); // configure task $task->configure($config); // log_info( 'system, event, debug', "task[$task] configured."); // regiser task $task_manager->registerTask($task_path, $task); log_info('system, event, debug', "task[{$class_name}] registered as: [{$task_path}]"); return $task; }
public static function loadConfig($sandbox, $obj_path, $type_name) { // Charcoal_ParamTrait::validateSandbox( 1, $sandbox ); // Charcoal_ParamTrait::validateStringOrObject( 2, 'Charcoal_ObjectPath', $obj_path ); // Charcoal_ParamTrait::validateString( 3, $type_name ); if (!$obj_path instanceof Charcoal_ObjectPath) { $obj_path = new Charcoal_ObjectPath($obj_path); } $object_name = $obj_path->getObjectName(); // log_info( "system", "config", "loading object config: path=[$obj_path] type=[$type_name]" ); // get root path of framework,project,web_app $dir_framework = Charcoal_ResourceLocator::getFrameworkPath(); $dir_project = Charcoal_ResourceLocator::getProjectPath(); $dir_application = Charcoal_ResourceLocator::getApplicationPath(); // get module root path of framework,project,web_app $dir_framework_module = $dir_framework . '/module'; $dir_project_module = $dir_project . '/module'; $dir_application_module = $dir_application . '/module'; // get real path(relative path) $real_path = $obj_path->getRealPath(); // config target set $config_target_list = NULL; // config base name $config_basename = $object_name ? $object_name . '.' . $type_name : $type_name; // read under global config folder $config_name = '/' . $type_name . '/' . $config_basename; $config_target_list[] = $dir_framework . '/config' . $config_name; $config_target_list[] = $dir_project . '/config' . $config_name; $config_target_list[] = $dir_application . '/config' . $config_name; // read under global config folder(relative path) if (strlen($real_path) > 0) { $config_name = '/' . $type_name . $real_path . '/' . $config_basename; $config_target_list[] = $dir_framework . '/config' . $config_name; $config_target_list[] = $dir_project . '/config' . $config_name; $config_target_list[] = $dir_application . '/config' . $config_name; } // read under global server folder $config_name = '/server/' . CHARCOAL_PROFILE . '/' . $type_name . '/' . $config_basename; $config_target_list[] = $dir_project . '/config' . $config_name; $config_target_list[] = $dir_application . '/config' . $config_name; // read under server config folder if (strlen($real_path) > 0) { $config_name = '/server/' . CHARCOAL_PROFILE . '/' . $type_name . $real_path . '/' . $config_basename; $config_target_list[] = $dir_project . '/config' . $config_name; $config_target_list[] = $dir_application . '/config' . $config_name; } // read under modules directory(current object path) $config_name = strlen($real_path) > 0 ? $real_path . '/' . $config_basename : '/' . $config_basename; $config_target_list[] = $dir_framework_module . $config_name; $config_target_list[] = $dir_project_module . $config_name; $config_target_list[] = $dir_application_module . $config_name; // read under modules directory(current procedure path) $request = Charcoal_Framework::getRequest(); if ($request) { $request_path = us($request->getProcedurePath()); $pos = strpos($request_path, '@'); if ($pos !== FALSE) { $virt_dir = substr($request_path, $pos + 1); if (strlen($virt_dir) > 0) { $proc_dir = str_replace(':', '/', $virt_dir); $config_target_list[] = $dir_application_module . $proc_dir . '/' . $config_basename; } } } // get registry from sandbox $registry = $sandbox->getRegistry(); // load all config files $config = $registry->get($config_target_list, $obj_path, $type_name); // import if (isset($config['import'])) { $import = $config['import']; $data = self::loadConfig($sandbox, $import, $type_name); if ($data) { $config = array_merge($config, $data); } } return $config; }
/** * get configuration data by key * * @param string[] $keys key list * @param Charcoal_ObjectPath $obj_path object path * @param string $type_name type name of the object * * @return mixed configuration data */ public function get(array $keys, $obj_path, $type_name) { // Charcoal_ParamTrait::validateString( 1, $key ); // get config povier $provider = $this->sandbox->getConfigProvider(); // make cache file path $base_dir = 'config/' . CHARCOAL_PROJECT . '/' . CHARCOAL_APPLICATION . '/' . $type_name; $real_path = $obj_path->getRealPath(); $base_dir = empty($real_path) ? $base_dir : $base_dir . '/' . $obj_path->getRealPath(); $cache_file = $obj_path->getObjectName() . '.config.php'; $cache_file_path = CHARCOAL_CACHE_DIR . '/' . $base_dir . '/' . $cache_file; // if cache file is not found, read config file. if (!is_readable($cache_file_path)) { goto LOAD_CONFIG_FROM_FILE; } // read cache file $fp = fopen($cache_file_path, 'r'); if (!$fp) { goto LOAD_CONFIG_FROM_FILE; } flock($fp, LOCK_EX); $contents = ''; while (!feof($fp)) { $contents .= fread($fp, 1024); } flock($fp, LOCK_UN); fclose($fp); // eval contents $cached_config = null; $retval = null; try { $retval = @eval('$cached_config=' . $contents . '; return true;'); } catch (ParseError $e) { goto LOAD_CONFIG_FROM_FILE; } if ($retval && is_array($cached_config)) { // read cache created date $cache_date = isset($cached_config['cache_date']) ? $cached_config['cache_date'] : false; // get each config $config_all = array(); foreach ($keys as $key) { // cache entry $cache_exists = isset($cached_config['config'][$key]); // if cache data does not exists, then load from file if (!$cache_exists) { goto LOAD_CONFIG_FROM_FILE; } // read config file date $config_date = $provider->getConfigDate($key); // if config file does not exist, then load from file if (!$config_date) { goto LOAD_CONFIG_FROM_FILE; } // check config file's date if ($config_date > $cache_date) { goto LOAD_CONFIG_FROM_FILE; } // correct cached data $cache_data = $cached_config['config'][$key]; $config_all = is_array($cache_data) ? array_merge($config_all, $cache_data) : $config_all; } // if cache is not modified, return cache data return $config_all; } LOAD_CONFIG_FROM_FILE: // get all config data from file $config_all = array(); $config_by_key = array(); foreach ($keys as $key) { $config = $provider->loadConfig($key); if (is_array($config)) { $config_all = array_merge($config_all, $config); $config_by_key[$key] = $config; } else { $config_by_key[$key] = array(); } } // create cache root directory if not exists if (!file_exists(CHARCOAL_CACHE_DIR)) { $res = @mkdir(CHARCOAL_CACHE_DIR); if (!$res) { _throw(new Charcoal_RegistryException('file_system', 'mkdir failed:' . CHARCOAL_CACHE_DIR)); } } // create cache directory recursively $dirs = explode('/', $base_dir); $dir_walk = ''; foreach ($dirs as $d) { if (empty($d)) { continue; } $dir_walk .= '/' . $d; $checkdir = CHARCOAL_CACHE_DIR . $dir_walk; if (!file_exists($checkdir)) { $res = @mkdir($checkdir); if (!$res) { _throw(new Charcoal_RegistryException('file_system', 'mkdir failed:' . $checkdir)); } } } // make new cache data $new_cache = array('cache_date' => time(), 'config' => $config_by_key); $lines = var_export($new_cache, true); // create cache file $fp = @fopen($cache_file_path, 'c'); if (!$fp) { _throw(new Charcoal_RegistryException('file_system', 'failed to open cache file:' . $cache_file_path)); } flock($fp, LOCK_EX); ftruncate($fp, 0); fputs($fp, $lines); flock($fp, LOCK_UN); fclose($fp); return $config_all; }
public function createObject($obj_path, $type_name, $args = NULL, $interface = NULL, $default_class = NULL) { // Charcoal_ParamTrait::validateStringOrObjectPath( 1, $obj_path ); // Charcoal_ParamTrait::validateString( 2, $type_name ); // Charcoal_ParamTrait::validateVector( 3, $args, TRUE ); // Charcoal_ParamTrait::validateStringOrObject( 4, 'Charcoal_Interface', $interface, TRUE ); // Charcoal_ParamTrait::validateStringOrObject( 5, 'Charcoal_Class', $default_class, TRUE ); $object = NULL; if (is_string($obj_path) || $obj_path instanceof Charcoal_String) { $obj_path = new Charcoal_ObjectPath($obj_path); } if (is_string($interface) || $interface instanceof Charcoal_String) { $interface = new Charcoal_Interface($interface); } try { $object_path_string = $obj_path->getObjectPathString(); // load configure file $config = Charcoal_ConfigLoader::loadConfig($this, $object_path_string, $type_name); $config = new Charcoal_Config($this->getEnvironment(), $config); // get class name from configure file $class_name = $config->getString('class_name'); $klass = NULL; if ($class_name && !empty($class_name)) { $klass = new Charcoal_Class($class_name); } else { if ($default_class) { if (is_string($default_class) || $default_class instanceof Charcoal_Class) { $default_class = new Charcoal_Class($default_class); } $klass = $default_class; } else { _throw(new Charcoal_ClassNameEmptyException("{$object_path_string}/{$type_name}")); } } // constructor args $obj_name = $obj_path->getObjectName(); /** @var Charcoal_CharcoalComponent $object */ $object = $klass->newInstance($args); // confirm implementation of the instance if ($interface) { $interface->validateImplements($object); } // set properties $object->setObjectName($obj_name); $object->setObjectPath($obj_path); $object->setTypeName($type_name); $object->setSandbox($this); // configure object $object->configure($config); } catch (Exception $e) { _catch($e); // 上位にthrow _throw(new Charcoal_CreateObjectException($obj_path, $type_name, $e)); } catch (Throwable $e) { _catch($e); // 上位にthrow _throw(new Charcoal_CreateObjectException($obj_path, $type_name, $e)); } // return created instance return $object; }
/** * イベントを処理する */ 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); }
/** * process event * * @param Charcoal_IEventContext $context event context * * @return Charcoal_Boolean|bool */ public function processEvent($context) { $request = $context->getRequest(); // $response = $context->getResponse(); // $sequence = $context->getSequence(); // $procedure = $context->getProcedure(); echo PHP_EOL; echo "==========================================" . PHP_EOL; echo "CharcoalPHP Test Runner" . PHP_EOL; echo " Framework Version:" . Charcoal_Framework::getVersion() . PHP_EOL; echo "==========================================" . PHP_EOL; // get paramter from command line $scenario = $request->getString('scenario'); $scenario = trim($scenario); log_debug("debug,scenario", "scenario: {$scenario}"); if ($scenario === NULL) { echo "actions or scenario parameter must be specified." . PHP_EOL; log_error("debug,error,scenario", "actions or scenario parameter must be specified."); return TRUE; } $scenario_file = $this->scenario_dir . '/' . $scenario . '.scenario.ini'; if (!is_file($scenario_file)) { echo "scenario file not found: {$scenario_file}" . PHP_EOL; log_error("debug,error,scenario", "scenario file not found: {$scenario_file}"); return TRUE; } $scenario_data = parse_ini_file($scenario_file, TRUE); log_debug("debug,scenario", "scenario_data: " . print_r($scenario_data, true)); if (empty($scenario_data)) { echo "couldn't read scenario file: {$scenario_file}" . PHP_EOL; log_error("debug,error,scenario", "couldn't read scenario file: {$scenario_file}"); return TRUE; } foreach ($scenario_data as $section => $data) { $target = isset($data['target']) ? $data['target'] : NULL; $actions = isset($data['actions']) ? $data['actions'] : NULL; $enabled = isset($data['enabled']) ? $data['enabled'] : TRUE; log_debug("debug,scenario", "target: {$target}"); log_debug("debug,scenario", "actions: {$actions}"); log_debug("debug,scenario", "enabled: {$enabled}"); if (in_array(strtolower($enabled), array('0', 'false', 'no'))) { echo "section[{$section}] is DISABLED. will skip." . PHP_EOL; log_warning("debug, scenario", "section[{$section}] is DISABLED."); continue; } if (empty($target)) { echo "[WARNING] 'target' is not found at section[{$section}]" . PHP_EOL; log_warning("debug, scenario", "'target' is not found at section[{$section}]"); continue; } if (empty($actions)) { echo "[WARNING] 'actions' is not found at section[{$section}]" . PHP_EOL; log_warning("debug, scenario", "'actions' is not found at section[{$section}]"); continue; } $target_path = new Charcoal_ObjectPath($target); $module_path = '@' . $target_path->getVirtualPath(); $context->loadModule($module_path); log_info("debug,scenario", "loaded module: {$module_path}"); $event_args = array($section, $target, $actions); /** @var Charcoal_IEvent $event */ $event = $context->createEvent('test', $event_args); $context->pushEvent($event); log_debug("debug,scenario", "event_args: " . print_r($event_args, true)); log_debug("debug,scenario", "pushed event: " . print_r($event, true)); } // request fo test summary /** @var Charcoal_IEvent $event */ $event = $context->createEvent('test_summary'); $context->pushEvent($event); return TRUE; }
/** * イベントを処理する */ 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); }