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; }
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; }