/** * Gets instance of application * @param string $appName Application name * @param string $instance Instance name * @param boolean $spawn If true, we spawn an instance if absent * @param boolean $preload If true, worker is at the preload stage * @return object $instance AppInstance */ public function getInstance($appName, $instance = '', $spawn = true, $preload = false) { $class = ClassFinder::find($appName, 'Applications'); if (isset(Daemon::$appInstances[$class][$instance])) { return Daemon::$appInstances[$class][$instance]; } if (!$spawn) { return false; } $fullname = $this->getAppFullname($appName, $instance); if (!class_exists($class)) { Daemon::$process->log(__METHOD__ . ': unable to find application class ' . $class . '\''); return false; } if (!is_subclass_of($class, '\\PHPDaemon\\Core\\AppInstance')) { Daemon::$process->log(__METHOD__ . ': class ' . $class . '\' found, but skipped as long as it is not subclass of \\PHPDaemon\\Core\\AppInstance'); return false; } $fullnameClass = $this->getAppFullname($class, $instance); if ($fullname !== $fullnameClass && isset(Daemon::$config->{$fullname})) { Daemon::$config->renameSection($fullname, $fullnameClass); } if (!$preload) { /** @noinspection PhpUndefinedVariableInspection */ if (!$class::$runOnDemand) { return false; } if (isset(Daemon::$config->{$fullnameClass}->limitinstances)) { return false; } } return new $class($instance); }
public function getObject($type, $cond = null, $objOrCb = null) { $class = ClassFinder::find($type, $this->name, $this->ns); if (!class_exists($class)) { Daemon::log(get_class($this) . ': undefined class: ' . $class); return false; } return new $class($cond, $objOrCb, $this); }
public function getCallbackURL() { $params = ['agent' => ClassFinder::getClassBasename($this)]; if (isset($_GET['external_token'])) { $params['external_token'] = Request::getString($_GET['external_token']); } if (isset($this->backUrl)) { $params['backurl'] = $this->backUrl; } return $this->req->getBaseUrl() . '/component/Account/ExternalAuthRedirect/json?' . http_build_query($params); }
public function defaultControllerHandler($name) { $class = '\\WakePHP\\Actions\\' . ClassFinder::getClassBasename($this) . '\\' . $name; if (class_exists($class)) { $action = new $class(); $action->setAppInstance($this->appInstance); $action->setRequest($this->req); $action->setComponent($this); $action->perform(); } else { $this->req->setResult(array('errmsg' => 'Unknown controller.')); } }
/** * Constructor. * @return void */ protected function init() { if ($this->isEnabled()) { list($class, $name) = explode(':', $this->name . ':'); $realclass = ClassFinder::find($class); $e = explode('\\', $realclass); if ($e[sizeof($e) - 1] !== 'Pool' && class_exists($realclass . '\\Pool')) { $realclass .= '\\Pool'; } if ($realclass !== $class) { $base = '\\PHPDaemon\\Core\\Pool:'; Daemon::$config->renameSection($base . $class . ($name !== '' ? ':' . $name : ''), $base . $realclass . ($name !== '' ? ':' . $name : '')); } if (!class_exists($realclass)) { Daemon::log($realclass . ' class not exists.'); return; } $this->pool = call_user_func([$realclass, 'getInstance'], $name); $this->pool->appInstance = $this; } }
/** * @param Request $req */ public function __construct($req) { $this->req = $req; $this->appInstance = $req->appInstance; $this->dbname =& $this->appInstance->dbname; Daemon::log(__CLASS__ . ' up.'); $this->db = $this->appInstance->db; $this->ORM = new MUChat($this); $this->tags = array(); $this->minMsgInterval = 1; $this->cache = \PHPDaemon\Clients\Memcache\Pool::getInstance(); $this->ipcId = sprintf('%x', crc32(Daemon::$process->pid . '-' . microtime(true))); $my_class = ClassFinder::getClassBasename($this); $this->config = isset($this->appInstance->config->{$my_class}) ? $this->appInstance->config->{$my_class} : null; $defaults = $this->getConfigDefaults(); if ($defaults) { $this->processDefaultConfig($defaults); } $this->dbname = $this->config->dbname->value; $this->init(); $this->onReady(); }
public function handleException($e) { if ($this->cmpName !== null) { if ($this->triggerAndCount('exception', $e)) { Daemon::log('handle exception trigger true'); return true; } $this->setResult(['exception' => is_callable([$e, 'toArray']) ? $e->toArray() : ['type' => ClassFinder::getClassBasename($e), 'code' => $e->getCode(), 'msg' => $e->getMessage()]]); $this->finish(); if (!$e instanceof \WakePHP\Exceptions\Generic || !$e->isSilent()) { Daemon::log('Debug: ' . $e . ': ' . $e->getTraceAsString()); } return true; } }
public function toArray() { return ['type' => ClassFinder::getClassBasename($this), 'code' => $this->getCode(), 'msg' => $this->getMessage(), 'critical' => $this->critical]; }
public function __construct($cond, $objOrCb, $orm) { $this->orm = $orm; $this->appInstance = $orm->appInstance; $this->cond($cond instanceof \MongoId ? ['_id' => $cond] : $cond); $this->type = ClassFinder::getClassBasename($this); $this->construct(); if (is_array($objOrCb) && !isset($objOrCb[0])) { $this->create($objOrCb); } elseif (is_callable($objOrCb)) { $this->fetch($objOrCb); } elseif (is_array($objOrCb)) { $this->create($objOrCb); } }