/** * 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; }
/** * create class loader * * @param string $obj_path class loader's object path to create * * @return Charcoal_IClassLoader class loader object */ public function createClassLoader($obj_path) { // Charcoal_ParamTrait::validateStringOrObjectPath( 1, $obj_path ); $class_loader = NULL; try { $obj_path = is_string($obj_path) ? new Charcoal_ObjectPath($obj_path) : $obj_path; // Configをロード $config = Charcoal_ConfigLoader::loadConfig($this, $obj_path, 'class_loader'); $config = new Charcoal_Config($this->environment, $config); // クラス名を取得 $class_name = $config->getString('class_name'); if ($class_name === NULL) { _throw(new Charcoal_ClassLoaderConfigException($obj_path, 'class_name', 'mandatory')); } $class_name = us($class_name); // project directory $project_dir = Charcoal_ResourceLocator::getProjectPath(); // ソースの取り込み $source_path = $project_dir . '/app/' . CHARCOAL_APPLICATION . '/class/class_loader/' . $class_name . '.class.php'; if (is_readable($source_path)) { /** @noinspection PhpIncludeInspection */ include $source_path; } else { $source_path = $project_dir . '/class/class_loader/' . $class_name . '.class.php'; if (is_readable($source_path)) { /** @noinspection PhpIncludeInspection */ include $source_path; } } // クラスローダのインスタンス生成 $klass = new Charcoal_Class($class_name); /** @var Charcoal_IClassLoader $class_loader */ $class_loader = $klass->newInstance(); $class_loader->setSandbox($this); // インタフェース確認 $interface = new Charcoal_Interface('Charcoal_IClassLoader'); $interface->validateImplements($class_loader); // log_info( 'system', "factory", "クラスローダ[" . us($object_path) . "]を作成しました。" ); } catch (Exception $ex) { _catch($ex); _throw(new Charcoal_CreateClassLoaderException($obj_path, $ex)); } return $class_loader; }
/** * load component * * @param string $component_name component path * @param Charcoal_Vector $args constructor arguments */ public function loadComponent($component_name, $args = array()) { try { $component_name = us($component_name); // log_info( "system,container", "container", "Loading component: [$component_name]"); // コンポーネント設定ファイルの読み込み $obj_path = new Charcoal_ObjectPath($component_name); $config = Charcoal_ConfigLoader::loadConfig($this->sandbox, $obj_path, 'component'); $config = new Charcoal_Config($this->sandbox->getEnvironment(), $config); // キャッシュに保存 $this->component_configs[$component_name] = $config; // クラス名を取得 $class_name = $config->getString('class_name'); if ($class_name === NULL) { _throw(new Charcoal_ComponentConfigException($component_name, "class_name", "mandatory")); } // create class object $klass = new Charcoal_Class($class_name); // コンポーネントスコープを取得 $scope = $config->getString('scope', self::SCOPE_REQUEST); $scope = us($scope); // コンポーネントスコープによって生成方法を変更 $component = NULL; switch ($scope) { case self::SCOPE_SESSION: // コンポーネントのインスタンスをセッションから復元 $component = unserialize($_SESSION[$component_name]); // セッションになければ、インスタンスを生成 if ($component == NULL) { $component = $klass->newInstance($args); } break; case self::SCOPE_TRANSIENT: case self::SCOPE_REQUEST: // コンポーネントのインスタンス生成 $component = $klass->newInstance($args); break; default: // scopeに指定されたワードが不正 _throw(new Charcoal_ComponentConfigException($component_name, 'scope', "invalid scope value:{$scope}")); break; } // initialize component $component->setComponentName($component_name); $component->setSandbox($this->sandbox); // 生成したインスタンスがIComponentインタフェースを実装しているか確認 if (!$component instanceof Charcoal_IComponent) { // 実装例外 _throw(new Charcoal_InterfaceImplementException($class_name, "Charcoal_IComponent")); } // コンポーネントを初期化 // log_info( "system,container", "container", "configuring component: [$component_name]"); $component->configure($config); // コンポーネントを配列に登録 if ($scope == self::SCOPE_SESSION || $scope == self::SCOPE_REQUEST) { $this->components[$component_name] = $component; } // log_info( "system,container", "container", "loaded component: [$component_name]"); // ロードしたコンポーネントを返却 return $component; } catch (Exception $ex) { _catch($ex); // rethrow exception _throw(new Charcoal_ComponentLoadingException($component_name, $ex)); } }