/** * Constructor method to instantiate the default controller object * * @param Request $request * @param Response $response * @param Project $project * @param string $viewPath * @return self */ public function __construct(Request $request = null, Response $response = null, Project $project = null, $viewPath = null) { if (null === $viewPath) { $cfg = $project->module('Phire')->asArray(); $viewPath = __DIR__ . '/../../../../../view/phire/install'; if (isset($cfg['view'])) { $class = get_class($this); if (is_array($cfg['view']) && isset($cfg['view'][$class])) { $viewPath = $cfg['view'][$class]; } else { if (is_array($cfg['view']) && isset($cfg['view']['*'])) { $viewPath = $cfg['view']['*'] . '/install'; } else { if (is_string($cfg['view'])) { $viewPath = $cfg['view'] . '/install'; } } } } } $lang = isset($_GET['lang']) ? $_GET['lang'] : 'en_US'; if (!defined('POP_LANG')) { define('POP_LANG', $lang); } $this->i18n = I18n::factory(); $this->i18n->loadFile($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . '/vendor/Phire/data/assets/i18n/' . $this->i18n->getLanguage() . '.xml'); parent::__construct($request, $response, $project, $viewPath); $this->sess = Session::getInstance(); }
/** * Finalize the request and send the response. * * @param int $code * @param array $headers * @throws \Pop\Mvc\Exception * @return void */ public function send($code = 200, array $headers = null) { if (null === $this->view) { throw new Exception('The view object is not defined.'); } if (!$this->view instanceof View) { throw new Exception('The view object is not an instance of Pop\\Mvc\\View.'); } if (null !== $this->project->logger()) { $this->project->log("Response [" . $code . "]", time()); } $this->response->setCode($code); if (null !== $headers) { foreach ($headers as $name => $value) { $this->response->setHeader($name, $value); } } // Trigger any dispatch events, then send the response if (null !== $this->project->getEventManager()->get('dispatch')) { $this->project->log('[Event] Dispatch', time(), \Pop\Log\Logger::NOTICE); } $this->project->getEventManager()->trigger('dispatch', array('controller' => $this)); $this->response->setBody($this->view->render(true)); if (null !== $this->project->getEventManager()->get('dispatch.send')) { $this->project->log('[Event] Dispatch Send', time(), \Pop\Log\Logger::NOTICE); } $this->project->getEventManager()->trigger('dispatch.send', array('controller' => $this)); $this->response->send(); }
/** * Constructor method to instantiate the user controller object * * @param Request $request * @param Response $response * @param Project $project * @param string $viewPath * @return self */ public function __construct(Request $request = null, Response $response = null, Project $project = null, $viewPath = null) { // Create the session object and get the user type $this->sess = Session::getInstance(); $this->type = $project->getService('acl')->getType(); if (null === $viewPath) { $cfg = $project->module('Phire')->asArray(); $viewPath = __DIR__ . '/../../../../view/phire'; if (isset($cfg['view'])) { $class = get_class($this); if (is_array($cfg['view']) && isset($cfg['view'][$class])) { $viewPath = $cfg['view'][$class]; } else { if (is_array($cfg['view']) && isset($cfg['view']['*'])) { $viewPath = $cfg['view']['*']; } else { if (is_string($cfg['view'])) { $viewPath = $cfg['view']; } } } } // If it is not a user, or a user globally logged into another area if (strtolower($this->type->type) != 'user' && !$this->type->global_access || substr($_SERVER['REQUEST_URI'], 0, strlen(BASE_PATH . APP_URI)) != BASE_PATH . APP_URI) { $site = Table\Sites::getSite(); $theme = Table\Extensions::findBy(array('type' => 0, 'active' => 1), null, 1); $themePath = $site->document_root . $site->base_path . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->type->type; $activeThemePath = null; if (isset($theme->rows[0])) { $activeThemePath = $site->document_root . $site->base_path . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $theme->rows[0]->name . DIRECTORY_SEPARATOR . $this->type->type; } if (null !== $activeThemePath && file_exists($activeThemePath)) { $viewPath = $activeThemePath; } else { if (file_exists($themePath)) { $viewPath = $themePath; } } } } // Set the correct base path and user URI based on user type if (get_called_class() == 'Phire\\Controller\\Phire\\IndexController') { $basePath = strtolower($this->type->type) != 'user' ? BASE_PATH . '/' . strtolower($this->type->type) : BASE_PATH . APP_URI; $request = new Request(null, $basePath); } parent::__construct($request, $response, $project, $viewPath); }
/** * Constructor method to instantiate the groups controller object * * @param Request $request * @param Response $response * @param Project $project * @param string $viewPath * @return self */ public function __construct(Request $request = null, Response $response = null, Project $project = null, $viewPath = null) { if (null === $viewPath) { $cfg = $project->module('Phire')->asArray(); $viewPath = __DIR__ . '/../../../../../view/phire/structure'; if (isset($cfg['view'])) { $class = get_class($this); if (is_array($cfg['view']) && isset($cfg['view'][$class])) { $viewPath = $cfg['view'][$class]; } else { if (is_array($cfg['view']) && isset($cfg['view']['*'])) { $viewPath = $cfg['view']['*']; } else { if (is_string($cfg['view'])) { $viewPath = $cfg['view']; } } } } } parent::__construct($request, $response, $project, $viewPath); }
/** * Route to the correct controller * * @param \Pop\Project\Project $project * @return void */ public function route(\Pop\Project\Project $project = null) { if (null !== $project) { $this->project = $project; } // If the request isn't root '/', traverse the URI path if ($this->request->getPath(0) != '') { $this->controllerClass = $this->traverseControllers($this->controllers); // Else, use root '/' } else { $this->controllerClass = isset($this->controllers['/']) ? $this->controllers['/'] : null; } // If found, create the controller object if (null !== $this->controllerClass && class_exists($this->controllerClass)) { // Push the real base path and URI into the request object $realBasePath = $this->request->getBasePath() . $this->basePath; $realUri = substr($this->request->getFullUri(), strlen($this->request->getBasePath() . $this->basePath)); // Create the controller object $this->controller = new $this->controllerClass($this->request->setRequestUri($realUri, $realBasePath), null, $this->project); // Trigger any route events if (null !== $this->project) { if (null !== $this->project->getEventManager()->get('route')) { $this->project->log('[Event] Route', time(), \Pop\Log\Logger::NOTICE); } $this->project->getEventManager()->trigger('route', array('router' => $this)); } // Else, trigger any route error events } else { if (null !== $this->project) { if (null !== $this->project->getEventManager()->get('route.error')) { $this->project->log('[Event] Route Error', time(), \Pop\Log\Logger::NOTICE); } $this->project->getEventManager()->trigger('route.error', array('router' => $this)); } } }
<?php require_once '../../bootstrap.php'; use Pop\Project\Project; try { $project = new Project(); $project->setService('config', 'Pop\\Config', array(array('test' => 123)))->setService('color', 'Pop\\Color\\Color', function () { return array(new \Pop\Color\Rgb(255, 0, 0)); }); print_r($project); print_r($project->getService('config')); print_r($project->getService('color')); print_r($project); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL . PHP_EOL; }
/** * Add any project specific code to this method for run-time use here. * * @return void */ public function run() { // Set the services $this->setService('acl', 'Phire\\Auth\\Acl'); $this->setService('auth', 'Phire\\Auth\\Auth'); $this->setService('phireNav', 'Pop\\Nav\\Nav'); // Get loaded modules and add their routes and nav $modules = $this->modules(); $nav = $this->getService('phireNav'); // Load module routes and nav foreach ($modules as $name => $config) { $cfg = $config->asArray(); // Add nav if (isset($cfg['nav'])) { $nav->addBranch($cfg['nav'], true); } // Add routes if (isset($cfg['routes'])) { $routes = APP_URI == '' && isset($cfg['routes'][APP_URI]) ? $cfg['routes'][APP_URI] : $cfg['routes']; $this->router->addControllers($routes); } } // If the path is the install path if (substr($_SERVER['REQUEST_URI'], 0, strlen(BASE_PATH . APP_URI . '/install')) == BASE_PATH . APP_URI . '/install') { parent::run(); // Else, load any user routes and initialize the ACL object } else { $this->loadUserRoutes(); $this->initAcl(); // Set the auth method to trigger on 'dispatch.pre' $this->attachEvent('dispatch.pre', 'Phire\\Project::auth'); // Set up routing error check on 'route.error' $this->attachEvent('route.error', 'Phire\\Project::error'); // If SSL is required for this user type, and not SSL, // redirect to SSL, else, just run if ($this->getService('acl')->getType()->force_ssl && !($_SERVER['SERVER_PORT'] == '443')) { \Pop\Http\Response::redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); } else { parent::run(); } } }
public function testServiceLocator() { $p = new Project(); $p->setService('config', 'Pop\\Config', array(array('test' => 123)))->setService('color', 'Pop\\Color\\Color', function () { return array(new \Pop\Color\Space\Rgb(255, 0, 0)); }); $this->assertInstanceOf('Pop\\Config', $p->getService('config')); $this->assertInstanceOf('Pop\\Color\\Color', $p->getService('color')); $this->assertInstanceOf('Pop\\Service\\Locator', $p->getServiceLocator()); }
$this->view = View::factory($this->viewPath . '/index.phtml', array('title' => 'Test Event', 'subtitle' => 'Home Page', 'content' => 'This is the home page')); $this->send(); } public function users() { $this->view = View::factory($this->viewPath . '/index.phtml', array('title' => 'Test Event', 'subtitle' => 'Users Page', 'content' => 'This is the users page')); $this->send(); } public function error() { $this->view = View::factory($this->viewPath . '/index.phtml', array('title' => 'Test Event', 'subtitle' => 'Error Page', 'content' => 'Page not found.')); $this->send(404); } } try { $project = new Project(null, null, new Router(array('/' => 'IndexController'))); $project->attachEvent('dispatch', function ($controller) { if ($controller->getRequest()->getRequestUri() == '/') { $controller->getView()->set('subtitle', 'This is the REVISED Home Page Subtitle.'); return 'Hello World! This is the home page!'; } }); $project->attachEvent('dispatch', function ($controller, $result) { if ($controller->getRequest()->getRequestUri() == '/') { $controller->getView()->set('content', $result); } }); $project->run(); } catch (\Exception $e) { echo $e->getMessage(); }