Ejemplo n.º 1
0
 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;
 }
Ejemplo n.º 2
0
 /**
  * イベントを処理する
  */
 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);
 }
 /**
  * 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;
 }
Ejemplo n.º 4
0
 /**
  * イベントを処理する
  */
 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);
 }