예제 #1
0
 private static function getActionResult($controller, $action)
 {
     $controller = $controller . '_controller';
     if (!class_exists($controller)) {
         throw new WebException($controller, $action, "Can't find controller - [{$controller}]");
     }
     if (!method_exists($controller, $action)) {
         throw new WebException($controller, $action, "Can't find action [{$action}] in [{$controller}]");
     }
     /**
      * @var Controller $controller
      */
     $controller = DI::get($controller);
     $ref = new \ReflectionClass($controller);
     $controller->attributes = AttributeCollection::create($ref->getDocComment());
     $controller->req = DI::get(Request::class);
     $pageLoadResult = $controller->onPageLoad();
     if (isset($pageLoadResult)) {
         return $pageLoadResult;
     }
     if (!$controller->isEnded) {
         $actionInfo = new ActionInfo($controller, $action);
         try {
             $actionResult = $actionInfo->invoke($controller->req->params(), $controller->req->posts());
             return $actionResult;
         } catch (\Exception $ex) {
             if ($controller instanceof OutputStatusException || $actionInfo->getIfOutputStatusException() || $controller->attributes->exists(OutputStatusExceptionAttribute::class)) {
                 $status = $controller->status($ex->getMessage(), $ex->getCode());
                 return $status;
             }
             throw new WebException($controller, $action, $ex->getMessage(), $ex);
         }
     }
     return null;
 }
예제 #2
0
 public function __construct($viewName)
 {
     $request = DI::get(Request::class);
     $this->name = $viewName;
     // 若为空,则默认调用 Controller 的 Action 为名字的 View 文件。
     if (!isset($this->name) || empty($this->name)) {
         $namespace = $request->param('namespace');
         $controller = $request->param('controller');
         $action = $request->param('action');
         $name = array();
         if (isset($namespace)) {
             $name[] = $namespace;
         }
         if (isset($controller)) {
             $name[] = $controller;
         }
         if (isset($action)) {
             $name[] = $action;
         }
         if (!isset($name) || empty($name)) {
             $name[] = self::DEFAULT_VIEW;
         }
         $this->name = implode(DIRECTORY_SEPARATOR, $name);
     }
 }
예제 #3
0
 public static function handle(\Exception $ex)
 {
     if ($ex->getPrevious() instanceof ConsoleException) {
         Console::error($ex->getMessage());
         return;
     }
     $handles = Settings::load('exception');
     if (isset($handles) && !empty($handles)) {
         foreach ($handles as $key => $value) {
             if (self::recursive($ex, $key)) {
                 if (is_callable($value)) {
                     call_user_func($value, $ex);
                     return;
                 } else {
                     /** @var ExceptionHandle $handle */
                     $handle = DI::get($value);
                     if (isset($handle)) {
                         $handle->handle();
                         return;
                     }
                 }
             }
         }
     }
     throw $ex;
 }
예제 #4
0
 private static function instance()
 {
     if (!isset(self::$instance)) {
         $provider = DI::get(SessionProvider::class);
         if (!isset($provider)) {
             DI::bindSingleton(SessionProvider::class, new FileSessionProvider());
             $provider = DI::get(SessionProvider::class);
         }
         self::$instance = new Session($provider);
     }
     return self::$instance;
 }
예제 #5
0
 /**
  * @throws HttpAuthenticationException
  */
 public function invoke()
 {
     $authorize = DI::get($this->authorize, $this->arguments);
     if (isset($authorize) && $authorize instanceof Authorize) {
         try {
             if (!$authorize->authorize(DI::get(HttpAuthCredential::class))) {
                 throw new HttpAuthenticationException("Http authorize failure [{$this->authorize}].");
             }
         } catch (\Exception $ex) {
             throw new HttpAuthenticationException($ex->getMessage(), $ex);
         }
     }
 }
예제 #6
0
 public function appStart()
 {
     Command::bind('model', ModelCommand::class);
     Command::bind('migrate', MigrateCommand::class);
     Command::bind('migration', MigrationCommand::class);
     Command::bind('daemon', DaemonCommand::class);
     Command::bind('service', ServiceCommand::class);
     Daemon::bind('idle', IdleDaemon::class);
     Daemon::bind('schedule', ScheduleDaemon::class);
     Settings::load('commands');
     Settings::load('daemons');
     DI::bindSingleton(AuthStorage::class, CacheAuthStorage::class);
 }
예제 #7
0
 public static function make($viewName = null)
 {
     $request = DI::get(Request::class);
     if (!$viewName) {
         throw new WebException($request->param('controller'), $request->param('action'), "Invalid view name.");
     } else {
         $viewFilePath = self::getFilePath($viewName);
         if (is_file($viewFilePath) && is_readable($viewFilePath)) {
             return new View($viewFilePath);
         } else {
             throw new WebException($request->param('controller'), $request->param('action'), "Can't find view file [{$viewFilePath}]");
         }
     }
 }
예제 #8
0
 /**
  * @param $widgetsName
  * @return Widgets
  */
 public static function make($widgetsName)
 {
     $request = DI::get(Request::class);
     if (!$widgetsName) {
         throw new WebException($request->param('controller'), $request->param('action'), "Invalid widgets name");
     } else {
         $filePath = self::getFilePath($widgetsName);
         if (is_file($filePath) && is_readable($filePath)) {
             return new Widgets($filePath);
         } else {
             throw new WebException($request->param('controller'), $request->param('action'), "Can't find widgets file {$filePath}");
         }
     }
 }
예제 #9
0
 private function migrate($name)
 {
     if (!isset($name) || "" === $name || array_search($name, $this->history)) {
         return;
     }
     $migration = DI::get($name);
     if (!isset($migration) || !$migration instanceof Migration) {
         return;
     }
     if (DB::transaction(function () use($migration, $name) {
         return false !== $migration->up();
     })) {
         $this->recordHistory($name);
         $this->clearTableSchema($name);
         Console::line("migrate {$name} finished.");
     } else {
         Console::line("migrate {$name} failure.");
     }
 }
예제 #10
0
 private static function getParent($id, array &$traces, TableQuery $query = null)
 {
     /** @var Model $model */
     $model = DI::get(__CLASS__);
     if (!isset($query)) {
         $realQuery = DB::table($model->getSchema()->getName());
     } else {
         $realQuery = clone $query;
     }
     $current = $realQuery->where('id', $id)->select()->detail()->execute();
     if (!isset($traces)) {
         $traces = array();
     }
     $traces[] = $current['name'];
     $parentID = $current[$model->getParentIDField()];
     if ($parentID > 0) {
         self::getParent($parentID, $traces, $query);
     }
 }
예제 #11
0
 public static function make($argc, array $argv)
 {
     if ($argc > 1) {
         $cmd = $name = $argv[1];
         if (array_key_exists($name, self::$commands)) {
             $name = self::$commands[$name];
         } else {
             $name = $name . 'Command';
         }
         unset($argv[0]);
         unset($argv[1]);
         try {
             $command = DI::get($name, array('args' => self::parseArgs($argv)));
             if ($command instanceof Command) {
                 return $command;
             }
         } catch (\Exception $ex) {
             throw new ConsoleException("Command [{$cmd}] undefined.");
         }
         throw new ConsoleException("Class [{$name}] is not a Command.");
     }
     return null;
 }
예제 #12
0
 public static function toObject($json, $class)
 {
     if (is_string($class)) {
         $class = new \ReflectionClass($class);
     }
     $params = self::toArray($json);
     if (!empty($params)) {
         $object = DI::get($class->name);
         if ($object instanceof Model) {
             $object = $object->load($params);
         } else {
             $properties = $class->getProperties();
             if (!empty($properties)) {
                 foreach ($properties as $property) {
                     if (array_key_exists($property->name, $params)) {
                         $property->setValue($object, $params[$property->name]);
                     }
                 }
             }
         }
         return $object;
     }
     return null;
 }
예제 #13
0
 public static function make($argc, array $argv)
 {
     if ($argc > 1) {
         $cmd = $name = $argv[2];
         if (array_key_exists($name, self::$daemons)) {
             $name = self::$daemons[$name];
         } else {
             $name = $name . 'Daemon';
         }
         unset($argv[0]);
         unset($argv[1]);
         unset($argv[2]);
         try {
             $daemon = DI::get($name, array('args' => self::parseArgs($argv)));
             if ($daemon instanceof Daemon) {
                 return $daemon;
             }
         } catch (\Exception $ex) {
             throw new ConsoleException("Daemon [{$cmd}] undefined.");
         }
         throw new ConsoleException("Class [{$name}] is not a Daemon.");
     }
     return null;
 }
예제 #14
0
 /**
  * @return AuthStorage
  */
 private static function getAuthStorage()
 {
     return DI::get(AuthStorage::class);
 }
예제 #15
0
 /**
  * @return Model
  */
 public static function model()
 {
     $table = get_called_class();
     /**
      * @var Model $model
      */
     $model = DI::get($table);
     return $model;
 }
예제 #16
0
 public function __construct($layout, $viewResult)
 {
     $this->layout = $layout;
     $this->viewResult = $viewResult;
     $this->req = DI::get(Request::class);
 }
예제 #17
0
 public function __construct()
 {
     $this->state = DI::get(AppModuleState::class);
     $this->className = get_called_class();
 }
예제 #18
0
 /**
  * @return Request
  */
 public function getCredential()
 {
     return DI::get(Request::class);
 }
예제 #19
0
 public static function module($module)
 {
     DI::bindSingleton($module, $module);
     if (false === array_search($module, self::instance()->appModules)) {
         self::instance()->appModules[] = $module;
     }
     $killedIndex = array_search($module, self::instance()->killedAppModules);
     if (false !== $killedIndex) {
         unset(self::instance()->killedAppModules[$killedIndex]);
     }
 }
예제 #20
0
 public function appStart()
 {
     DI::bindSingleton(AuthStorage::class, SessionAuthStorage::class);
 }
예제 #21
0
<?php

/**
 * Created by PhpStorm.
 * User: lbob
 * Date: 2015/11/13
 * Time: 17:46
 */
use Xaircraft\DI;
use Xaircraft\Web\Session\FileSessionProvider;
use Xaircraft\Web\Session\SessionProvider;
DI::bindSingleton(SessionProvider::class, new FileSessionProvider());
DI::bindSingleton(EmailSender::class, EmailSenderImpl::class);
예제 #22
0
 public function getQueryString(QueryContext $context = null)
 {
     $context = isset($context) ? $context : DI::get(QueryContext::class);
     $tableQueryExecutor = $this->parseTableQuery($context);
     $this->contextLock = true;
     if (isset($tableQueryExecutor)) {
         return $tableQueryExecutor->toQueryString($context);
     }
     return null;
 }