Ejemplo n.º 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;
 }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 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);
         }
     }
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 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}]");
         }
     }
 }
Ejemplo n.º 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}");
         }
     }
 }
Ejemplo n.º 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.");
     }
 }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 14
0
 /**
  * @return AuthStorage
  */
 private static function getAuthStorage()
 {
     return DI::get(AuthStorage::class);
 }
Ejemplo n.º 15
0
 /**
  * @return Model
  */
 public static function model()
 {
     $table = get_called_class();
     /**
      * @var Model $model
      */
     $model = DI::get($table);
     return $model;
 }
Ejemplo n.º 16
0
 public function __construct($layout, $viewResult)
 {
     $this->layout = $layout;
     $this->viewResult = $viewResult;
     $this->req = DI::get(Request::class);
 }
Ejemplo n.º 17
0
 public function __construct()
 {
     $this->state = DI::get(AppModuleState::class);
     $this->className = get_called_class();
 }
Ejemplo n.º 18
0
 /**
  * @return Request
  */
 public function getCredential()
 {
     return DI::get(Request::class);
 }
Ejemplo n.º 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]);
     }
 }
Ejemplo n.º 20
0
 public function appStart()
 {
     DI::bindSingleton(AuthStorage::class, SessionAuthStorage::class);
 }
Ejemplo n.º 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);
Ejemplo n.º 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;
 }