protected static function getService() { //TODO: caching $config = Config::get('database'); $serviceName = $config['service']; return Virge::service($serviceName); }
/** * Commit our migrations, pass in a directory will read all files within * and commit them as migrations * @param string $dir */ public function commitMigrations($dir) { $fileArray = Virge::dirToArray($dir); //TODO: use the Virge ORM $sql = "SELECT DISTINCT(`filename`) AS `file` FROM `virge_migration`"; $result = Database::query($sql); $existing = array(); foreach ($result as $row) { $existing[] = $row['file']; } $files = $fileArray['file']; uasort($files, function ($a, $b) use($dir) { $aFilename = pathinfo($a, PATHINFO_FILENAME); $aDateTime = \DateTime::createFromFormat('Y-m-d-H-i-s', substr($aFilename, strlen($aFilename) - 19)); $bFilename = pathinfo($b, PATHINFO_FILENAME); $bDateTime = \DateTime::createFromFormat('Y-m-d-H-i-s', substr($bFilename, strlen($bFilename) - 19)); if ($aDateTime < $bDateTime) { return -1; } if ($aDateTime > $bDateTime) { return 1; } if (filemtime($dir . $a) < filemtime($dir . $b)) { return 1; } return -1; }); foreach ($files as $file) { if (!in_array($file, $existing)) { include $dir . $file; $this->logMigration($file); } } }
/** * Listen on the given event, and call our callable * @param Event $event * @return boolean * @throws \RuntimeException */ public function listen(Event $event) { if (is_callable($this->getCallable())) { return call_user_func_array($this->getCallable(), array($event)); } if ($this->getMethod() && ($service = Virge::service($this->getCallable()))) { return call_user_func_array(array($service, $this->getMethod()), array($event)); } throw new \RuntimeException(sprintf("Invalid listener: %s, cannot listen on event type: %s", get_class($this), get_class($event))); }
/** * Route a web request */ public function route($uri = null) { $request = $this->_buildRequest($uri); foreach (Routes::getResolvers() as $resolverConfig) { $resolver = $resolverConfig['resolver']; if (is_string($resolver) && Virge::service($resolver)) { $resolver = Virge::service($resolver); } $method = $resolverConfig['method']; if (false !== ($response = call_user_func_array(array($resolver, $method), array($request)))) { if ($response instanceof Response) { $response->send(); } else { $response = new Response($response); $response->send(); } return true; } } $route = $this->_getRoute($request->getURI()); if (!$route) { throw new NotFoundException(); } if (!$route->access()) { throw new UnauthorizedAccessException(); } $route->setActive(true); if (is_callable($route->getContoller())) { return call_user_func($route->getController(), $request); } $controllerClassname = $route->getController(); $controller = new $controllerClassname(); $method = $route->getMethod(); $response = call_user_func_array(array($controller, $method), array($request)); if ($response instanceof Response) { $response->send(); } else { $response = new Response($response); $response->send(); } }
<?php use Virge\Virge; /** * Registers all given handlers with Virge that this Capsule contains * @author Michael Kramer */ Virge::registerService("router", "\\Virge\\Router\\Service\\RouterService"); Virge::registerService("templating", "\\Virge\\Router\\Service\\TemplateService");
<?php use Virge\Cron\Service\ExpressionService; use Virge\Cron\Service\JobService; use Virge\Cron\Service\ScheduleService; use Virge\Virge; /** * * @author Michael Kramer */ Virge::registerService(ExpressionService::SERVICE_ID, new ExpressionService()); Virge::registerService(JobService::SERVICE_ID, new JobService()); Virge::registerService(ScheduleService::SERVICE_ID, new ScheduleService());
/** * @return ExpressionService */ protected function getExpressionService() { return Virge::service(ExpressionService::SERVICE_ID); }
<?php use Virge\Virge; /** * * @author Michael Kramer */ Virge::registerService('virge/database', '\\Virge\\Database\\Service\\DatabaseService'); Virge::registerService('virge/schema', '\\Virge\\Database\\Service\\SchemaService');
<?php use Virge\Event\Service\EventService; use Virge\Virge; /** * * @author Michael Kramer */ Virge::registerService(EventService::SERVICE_ID, new EventService());
<?php use Virge\Graphite\Service\QueueService; use Virge\Virge; /** * * @author Michael Kramer */ Virge::registerService(QueueService::SERVICE_ID, new QueueService());
/** * Compile the config */ public static function compile() { $configPath = self::get('app_path') . 'config/'; $configFiles = Virge::dirToArray($configPath); $distConfigs = array(); if ($configFiles) { foreach ($configFiles['file'] as $configFile) { if (strpos($configFile, '.dist') === false) { continue; } $configName = self::getConfigNameFromFile($configFile); $distConfigs[$configName] = (include_once $configPath . $configFile); } } $compiledConfig = array(); foreach (self::$_config as $name => $config) { if (isset($distConfigs[$name])) { $distConfig = $distConfigs[$name]; $compiledConfig[$name] = array_replace_recursive($distConfig, $config); } else { $compiledConfig[$name] = $config; } } file_put_contents($configPath . '_compiled.php', serialize($compiledConfig)); }
/** * Register a handler with a given short-name, which maps to the given * class * @param string $serviceName * @param string $serviceClass */ public static function registerService($serviceName, $serviceClass) { Virge::registerService($serviceName, $serviceClass); }
/** * @return QueueService */ protected function getQueueService() { return Virge::service(QueueService::SERVICE_ID); }
protected static function service($serviceId) { return Virge::service($serviceId); }
/** * @return SchemaService */ protected function getSchemaService() { return Virge::service('virge/schema'); }
/** * @return TemplateService */ public function getTemplatingService() { return Virge::service('templating'); }
/** * Enter into a service * @param string $service * @param array $arguments */ protected function entry($service, $method, $arguments) { return call_user_func_array(array(Virge::service($service), $method), $arguments); }
/** * @return ScheduleService */ protected function getScheduleService() { return Virge::service(ScheduleService::SERVICE_ID); }
<?php use Virge\Virge; /** * Registers all given handlers with Virge that this Capsule contains * @author Michael Kramer */ Virge::registerService("cli", "\\Virge\\Cli");