예제 #1
0
 /**
  *    Create and configure an object
  *
  *    @param string $obj_path         object path to create
  *    @param string $type_name        type name of the object
  *    @param array $args             constructor arguments
  *    @param array $config           object configuration parameters
  *
  * @return Charcoal_CharcoalComponent        object instance
  */
 public function createObject($obj_path, $type_name, $args = array(), $config = NULL)
 {
     //        Charcoal_ParamTrait::validateStringOrObjectPath( 1, $obj_path );
     //        Charcoal_ParamTrait::validateString( 2, $type_name );
     //        Charcoal_ParamTrait::validateConfig( 3, $config, TRUE );
     try {
         $object = $this->sandbox->createObject($obj_path, $type_name, $args);
         if ($config) {
             $object->configure($config);
         }
         return $object;
     } catch (Exception $ex) {
         _catch($ex);
         _throw(new Charcoal_EventContextException(__METHOD__ . '() failed.', $ex));
     }
 }
예제 #2
0
 /**
  *    execute framework main process
  *
  * @param Charcoal_Sandbox $sandbox
  */
 private static function _run($sandbox)
 {
     //        Charcoal_ParamTrait::validateSandbox( 1, $sandbox );
     //==================================================================
     // create exception handler list
     self::$exception_handlers = new Charcoal_ExceptionHandlerList($sandbox);
     //==================================================================
     // create debugtrace renderder list
     self::$debugtrace_renderers = new Charcoal_DebugTraceRendererList($sandbox);
     //==================================================================
     // create logger list
     self::$loggers = new Charcoal_LoggerList($sandbox);
     //==================================================================
     // create core hook list
     self::$corehooks = new Charcoal_CoreHookList($sandbox);
     //==================================================================
     // create cache driver list
     self::$cache_drivers = new Charcoal_CacheDriverList($sandbox);
     //==================================================================
     // load sandbox
     $profile = NULL;
     try {
         $profile = $sandbox->load();
     } catch (Exception $ex) {
         _catch($ex);
         _throw($ex);
     }
     //=======================================
     // Start bootstrap
     self::setHookStage(Charcoal_EnumCoreHookStage::START_OF_BOOTSTRAP);
     //=======================================
     // フレームワーク初期化処理
     //
     self::setHookStage(Charcoal_EnumCoreHookStage::BEFORE_INIT_FRAMEWORK);
     // タイムアウトを指定
     if (!ini_get('safe_mode')) {
         $timeout = $profile->getInteger('SCRIPT_TIMEOUT', ini_get("max_execution_time"));
         set_time_limit(ui($timeout));
     }
     self::setHookStage(Charcoal_EnumCoreHookStage::AFTER_INIT_FRAMEWORK);
     //=======================================
     // クラスローダの登録
     //
     self::setHookStage(Charcoal_EnumCoreHookStage::BEFORE_REG_CLASS_LOADERS);
     $framework_class_loader = $sandbox->createClassLoader('framework');
     self::setHookStage(Charcoal_EnumCoreHookStage::CREATE_FRAMEWORK_CLASS_LOADER, $framework_class_loader);
     Charcoal_ClassLoader::addClassLoader($framework_class_loader);
     self::setHookStage(Charcoal_EnumCoreHookStage::REG_FRAMEWORK_CLASS_LOADER, $framework_class_loader);
     try {
         $class_loaders = $profile->getArray('CLASS_LOADERS');
         if ($class_loaders) {
             foreach ($class_loaders as $loader_name) {
                 if (strlen($loader_name) === 0) {
                     continue;
                 }
                 $loader = $sandbox->createClassLoader($loader_name);
                 self::setHookStage(Charcoal_EnumCoreHookStage::CREATE_CLASS_LOADER, $loader_name);
                 Charcoal_ClassLoader::addClassLoader($loader);
                 self::setHookStage(Charcoal_EnumCoreHookStage::REG_CLASS_LOADER, $loader_name);
             }
         }
     } catch (Charcoal_CreateClassLoaderException $ex) {
         _catch($ex);
         _throw(new Charcoal_FrameworkBootstrapException('failed to load class loader:' . $ex->getClassLoaderPath(), $ex));
     }
     // register framework class loader
     if (!spl_autoload_register('Charcoal_ClassLoader::loadClass', false)) {
         log_fatal("debug,system,error", 'framework', "registering master class loader failed.");
         _throw(new Charcoal_ClassLoaderRegistrationException('framework'));
     }
     self::setHookStage(Charcoal_EnumCoreHookStage::AFTER_REG_CLASS_LOADERS);
     //=======================================
     // Requestパラメータの取得
     //
     /** @var Charcoal_IRequest $request */
     $request = $sandbox->createObject(CHARCOAL_RUNMODE, 'request', array(), 'Charcoal_IRequest');
     self::$request = $request;
     //        log_debug( "debug,system","request object created: " . print_r($request,true), 'framework' );
     self::$proc_path = us($request->getProcedurePath());
     //        log_debug( "debug,system","proc_path=" . $proc_path, 'framework' );
     // if procedure path is not specified in url, forward the procedure to DEFAULT_PROCPATH in profile.ini
     if (strlen(self::$proc_path) === 0) {
         self::$proc_path = us($profile->getString('DEFAULT_PROCPATH'));
     }
     $sandbox->getEnvironment()->set('%REQUEST_PATH%', self::$proc_path);
     self::$loggers->init();
     //=======================================
     // 外部ライブラリの使用
     //
     self::setHookStage(Charcoal_EnumCoreHookStage::BEFORE_REG_EXTLIB_DIR);
     $use_extlib = b($profile->getBoolean('USE_EXTLIB', FALSE));
     if ($use_extlib->isTrue()) {
         $lib_dirs = $profile->getArray('EXTLIB_DIR', array(), TRUE);
         if ($lib_dirs) {
             foreach ($lib_dirs as $dir) {
                 if (strlen($dir) === 0) {
                     continue;
                 }
                 if (!file_exists($dir)) {
                     _throw(new Charcoal_ProfileConfigException('EXTLIB_DIR', "directory [{$dir}] does not exists"));
                 }
                 if (!is_dir($dir)) {
                     _throw(new Charcoal_ProfileConfigException('EXTLIB_DIR', "[{$dir}] is not directory"));
                 }
                 ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . $dir);
                 self::setHookStage(Charcoal_EnumCoreHookStage::ADD_EXTLIB_DIR, $dir);
             }
         }
     }
     self::setHookStage(Charcoal_EnumCoreHookStage::AFTER_REG_EXTLIB_DIR);
     //=======================================
     // セッションハンドラの作成
     //
     $use_session = b($profile->getBoolean('USE_SESSION', FALSE));
     if ($use_session->isTrue()) {
         self::setHookStage(Charcoal_EnumCoreHookStage::BEFORE_SET_SESSION_HANDLER);
         // セッションハンドラ名の取得
         $session_handler_name = s($profile->getString('SESSION_HANDLER_NAME'));
         if ($session_handler_name && !$session_handler_name->isEmpty()) {
             // セッションハンドラの作成
             $session_handler = $sandbox->createObject($session_handler_name, 'session_handler', array(), 'Charcoal_ISessionHandler');
             session_set_save_handler(array($session_handler, 'open'), array($session_handler, 'close'), array($session_handler, 'read'), array($session_handler, 'write'), array($session_handler, 'destroy'), array($session_handler, 'gc'));
         }
         self::setHookStage(Charcoal_EnumCoreHookStage::AFTER_SET_SESSION_HANDLER);
     }
     //=======================================
     // Create Session
     //
     $session = NULL;
     if ($use_session->isTrue()) {
         // create session
         $session = new Charcoal_Session();
         self::setHookStage(Charcoal_EnumCoreHookStage::BEFORE_START_SESSION);
         // start session
         $session->start();
         //            log_info( "system",'Session started', 'framework' );
         // restore session
         $session->restore();
         //            log_info( "system",'Session is restored.', 'framework' );
         self::setHookStage(Charcoal_EnumCoreHookStage::AFTER_START_SESSION);
     }
     //=======================================
     // Routing Rule
     //
     self::setHookStage(Charcoal_EnumCoreHookStage::BEFORE_ROUTING_RULE);
     // get routers list from profile
     $routing_rule_name = $profile->getString('ROUTING_RULE');
     $routing_rule_name = us($routing_rule_name);
     // register routers
     $routing_rule = NULL;
     if (!empty($routing_rule_name)) {
         /** @var Charcoal_IRoutingRule $routing_rule */
         $routing_rule = $sandbox->createObject($routing_rule_name, 'routing_rule', array(), 'Charcoal_IRoutingRule');
     }
     self::setHookStage(Charcoal_EnumCoreHookStage::AFTER_ROUTING_RULE);
     //=======================================
     // Router
     //
     if ($routing_rule) {
         self::setHookStage(Charcoal_EnumCoreHookStage::BEFORE_ROUTER);
         // get routers list from profile
         $router_names = $profile->getArray('ROUTERS');
         // register routers
         if ($router_names) {
             foreach ($router_names as $router_name) {
                 if (strlen($router_name) === 0) {
                     continue;
                 }
                 /** @var Charcoal_IRouter $router */
                 $router = $sandbox->createObject($router_name, 'router', array(), 'Charcoal_IRouter');
                 $res = $router->route($request, $routing_rule);
                 if (is_array($res)) {
                     self::$proc_path = $request->getProcedurePath();
                     $sandbox->getEnvironment()->set('%REQUEST_PATH%', self::$proc_path);
                     log_debug("debug,system", "routed: proc_path=[" . self::$proc_path . "]", 'framework');
                     break;
                 }
             }
         }
         self::setHookStage(Charcoal_EnumCoreHookStage::AFTER_ROUTER);
     }
     //=======================================
     // Procedureの作成
     //
     // プロシージャを作成
     self::setHookStage(Charcoal_EnumCoreHookStage::BEFORE_CREATE_PROCEDURE, self::$proc_path);
     $procedure = NULL;
     try {
         /** @var Charcoal_IProcedure $procedure */
         $procedure = $sandbox->createObject(self::$proc_path, 'procedure', array(), 'Charcoal_IProcedure');
     } catch (Exception $e) {
         _catch($e);
         _throw(new Charcoal_ProcedureNotFoundException(self::$proc_path, $e));
     }
     self::setHookStage(Charcoal_EnumCoreHookStage::AFTER_CREATE_PROCEDURE, self::$proc_path);
     // procedure forwarding
     self::setHookStage(Charcoal_EnumCoreHookStage::BEFORE_PROCEDURE_FORWARD);
     if ($procedure->hasForwardTarget()) {
         // get forward target path
         $object_path = $procedure->getForwardTarget();
         //            log_debug( "debug,system","procedure forward target:" . $object_path, 'framework' );
         self::setHookStage(Charcoal_EnumCoreHookStage::PRE_PROCEDURE_FORWARD, $object_path);
         // create target procedure
         $procedure = $sandbox->createObject($object_path, 'procedure', 'Charcoal_IProcedure');
         //            log_debug( "debug,system","forward procedure created:" . $procedure, 'framework' );
         self::setHookStage(Charcoal_EnumCoreHookStage::POST_PROCEDURE_FORWARD, $object_path);
     }
     self::setHookStage(Charcoal_EnumCoreHookStage::AFTER_PROCEDURE_FORWARD);
     // override logger settings by the procedure's settings
     self::$loggers->overrideByProcedure($procedure);
     //=======================================
     // create response object
     //
     $response = $sandbox->createObject(CHARCOAL_RUNMODE, 'response', array(), 'Charcoal_IResponse');
     //=======================================
     // ブートストラップ完了
     //
     self::setHookStage(Charcoal_EnumCoreHookStage::END_OF_BOOTSTRAP);
     //=======================================
     // Procedureの実行
     //
     self::setHookStage(Charcoal_EnumCoreHookStage::BEFORE_EXECUTE_PROCEDURES);
     // プロシージャの実行
     while ($procedure) {
         $path = $procedure->getObjectPath()->getVirtualPath();
         self::setHookStage(Charcoal_EnumCoreHookStage::PRE_EXECUTE_PROCEDURE, $path);
         $procedure->execute($request, $response, $session);
         self::setHookStage(Charcoal_EnumCoreHookStage::POST_EXECUTE_PROCEDURE, $path);
         // プロシージャをスタックから取得
         $procedure = self::popProcedure();
     }
     self::setHookStage(Charcoal_EnumCoreHookStage::AFTER_EXECUTE_PROCEDURES);
     //=======================================
     // 終了処理
     //
     self::setHookStage(Charcoal_EnumCoreHookStage::START_OF_SHUTDOWN);
     self::setHookStage(Charcoal_EnumCoreHookStage::BEFORE_SAVE_SESSION);
     // セッション情報の保存
     if ($use_session->isTrue() && $session) {
         // セッションを保存
         $session->save();
         $session->close();
     }
     self::setHookStage(Charcoal_EnumCoreHookStage::AFTER_SAVE_SESSION);
     //=======================================
     // コンテナの破棄
     //
     self::setHookStage(Charcoal_EnumCoreHookStage::BEFORE_DESTROY_CONTAINER);
     $sandbox->getContainer()->terminate();
     self::setHookStage(Charcoal_EnumCoreHookStage::AFTER_DESTROY_CONTAINER);
     self::setHookStage(Charcoal_EnumCoreHookStage::END_OF_SHUTDOWN);
 }