示例#1
0
 /**
  * Create and configure a component
  *
  * @param string $obj_path         object path to create
  * @param array $args             constructor arguments
  * @param array $config           object configuration parameters
  *
  * @return Charcoal_IComponent        component instance
  */
 public function getComponent($obj_path, $args = array(), $config = NULL)
 {
     //        Charcoal_ParamTrait::validateStringOrObjectPath( 1, $obj_path );
     //        Charcoal_ParamTrait::validateConfig( 2, $config, TRUE );
     try {
         $component = $this->sandbox->getContainer()->getComponent($obj_path, $args);
         if ($config) {
             $component->configure($config);
         }
         return $component;
     } catch (Exception $ex) {
         _catch($ex);
         _throw(new Charcoal_EventContextException(__METHOD__ . '() failed.', $ex));
     }
 }
 /**
  *    List models
  *
  * @param Charcoal_Sandbox $sandbox
  * @param int $find_path
  *
  * @return Charcoal_ITableModel[]
  */
 public function listModels($sandbox, $find_path)
 {
     // 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';
     $config_target_list = array();
     if (Charcoal_System::isAnyBitSet($find_path, Charcoal_EnumFindPath::FIND_PATH_FRAMEWORK)) {
         $config_target_list[] = $dir_framework . '/config/table_model';
         $config_target_list[] = $dir_framework_module . '/config/table_model';
     }
     if (Charcoal_System::isAnyBitSet($find_path, Charcoal_EnumFindPath::FIND_PATH_PROJECT)) {
         $config_target_list[] = $dir_project . '/config/table_model';
         $config_target_list[] = $dir_project_module . '/config/table_model';
     }
     if (Charcoal_System::isAnyBitSet($find_path, Charcoal_EnumFindPath::FIND_PATH_APPLICATION)) {
         $config_target_list[] = $dir_application . '/config/table_model';
         $config_target_list[] = $dir_application_module . '/config/table_model';
     }
     // find model names in target directory
     $table_model_names = array();
     foreach ($config_target_list as $path) {
         $found_models = $sandbox->getRegistry()->listObjects($path, 'table_model');
         $table_model_names = array_merge($table_model_names, $found_models);
     }
     // convert model name into table model instance
     $table_models = array();
     foreach ($table_model_names as $model_name) {
         $model = $this->getModel($model_name);
         if ($model && $model instanceof Charcoal_ITableModel) {
             $table_models[$model_name] = $model;
         }
     }
     return $table_models;
 }
 /**
  *  Constructor
  *
  * @param Charcoal_Sandbox $sandbox
  */
 public function __construct($sandbox)
 {
     //        Charcoal_ParamTrait::validateSandbox( 1, $sandbox );
     $this->sandbox = $sandbox;
     parent::__construct($sandbox->getEnvironment());
 }
示例#4
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);
 }