/** * Find out if APC is enabled * * @return bool */ public static function isAPCEnabled() { if (self::$isAPCEnabled == null) { $keySuffix = PHP_SAPI == 'cli' ? '_cli' : ''; self::$isAPCEnabled = ini_get('apc.enabled' . $keySuffix); } return self::$isAPCEnabled; }
/** * Protected constructor * * @return void */ protected function __construct() { $isAPCEnabled = Resqee::isAPCEnabled(); if ($isAPCEnabled) { $config = apc_fetch(self::APC_KEY_CONFIG . $this->getConfigFile()); if ($config !== false) { $this->config = $config; } } if (empty($this->config)) { $this->config = $this->parseConfig($this->getConfigFile()); if ($isAPCEnabled) { apc_add(self::APC_KEY_CONFIG . $this->getConfigFile(), $this->config, self::APC_TTL_CONFIG); } } }
/** * Get an array of plugins * * If $instanciate is true then the returned array will have instanciated * plugin objects that are ready for use. * * @param string $event What event you want the plugin to be enabled for * @param bool $instanciate Whether or not to instanciate the plugins * * @return array */ public static function getPlugins($event, $instanciate = true) { $config = Resqee_Config::getInstance()->getConfigSection(self::KEY_PLUGINS); if (empty($config)) { return array(); } $rtn = array(); foreach ($config as $pluginClass => $eventName) { if ($event == $eventName) { if ($instanciate) { Resqee::loadClass($pluginClass); $rtn[$pluginClass] = new $pluginClass(); } else { $rtn[] = $pluginClass; } } } return $rtn; }
/** * Handle requests for POST /job * * This is where we're handed a job and need to start running it * */ public function post() { try { Resqee::loadClass($_POST[Resqee::KEY_POST_JOB_CLASS_PARAM]); } catch (Exception $e) { throw new Resqee_Exception("Could not load job class"); } $plugins = Resqee_Plugins::getPlugins(Resqee_Plugin_Events::SERVER_JOB_POST); $responses = array(); $serialized = stripslashes(urldecode($_POST[Resqee::KEY_POST_JOB_PARAM])); $jobs = unserialize($serialized); foreach ($jobs as $jobId => $jobData) { $job = unserialize($jobData[Resqee::KEY_POST_JOB_PARAM]); if (!is_object($job) || !$job instanceof Resqee_Job) { throw new Resqee_Exception("Invalid job. Job must be an instance of Resqee_Job"); } // don't bother unserializing nulls $args = 'N;' != $jobData[Resqee::KEY_POST_JOB_ARGS_PARAM] ? unserialize($jobData[Resqee::KEY_POST_JOB_ARGS_PARAM]) : null; $item = new Resqee_Persistence_Item(); $item->jobId = $jobId; $item->job = $jobData[Resqee::KEY_POST_JOB_PARAM]; $item->args = $jobData[Resqee::KEY_POST_JOB_ARGS_PARAM]; $item->class = get_class($job); $item->parentClass = get_parent_class($job); $item->requestTime = $this->serverGlobal['REQUEST_TIME']; // run before() on each regsitered plugin foreach ($plugins as $plugin) { $plugin->before($item); } $runner = new Resqee_JobRunner($job, $args, $this->serverGlobal); $responses[$jobData[Resqee::KEY_POST_JOB_ID_PARAM]] = $runner->getResponse(); } // run after() for each registered plugin foreach ($responses as $jobId => $response) { foreach ($plugins as $plugin) { $plugin->after($jobId, $response); } } echo serialize($responses); }
/** * Factory method to get an instance of a controller * * @param string $uri The URI we want a controller for * * @return Resqee_Controller */ public static function factory($serverGlobal) { $uri = $serverGlobal['REDIRECT_URL']; $controller = null; $trimmed = trim($uri, '/'); if ($trimmed == '') { $controller = 'Index'; } else { $pathParts = explode('/', $trimmed); $controller = ucfirst($pathParts[0]); } $className = "Resqee_Controller_{$controller}"; try { Resqee::loadClass($className); return new $className($serverGlobal); } catch (Resqee_Exception $e) { throw new Resqee_Exception("404"); } }
/** * Get the result * * This method will unserialize the response from the server * * @return mixed */ public function getResult() { if ($this->resultDataType !== null) { Resqee::loadClass($this->resultDataType); } return unserialize($this->serializedResult); }
/** * Check whether a server is disabled * * We check local cache and APC * * @param string|array $hostAndPort localhost:80 * or array('host' => localhost, 'port' => 80) * * @return bool */ public function isServerDisabled($hostAndPort) { if (is_array($hostAndPort)) { $hostAndPort = "{$hostAndPort['host']}:{$hostAndPort['port']}"; } return isset(self::$disabledServers[$hostAndPort]) || Resqee::isAPCEnabled() && apc_fetch(self::APC_KEY_SERVER_DISABLED . $hostAndPort); }