/** * @inheritdoc */ public function init(\Psr\Http\Message\ServerRequestInterface $request, array $codes = null) { if (isset($codes) && is_array($codes) && count($codes)) { // Scan for localization information within the URI. // If found, it will be used for localization purposes and stripped from the request. $uri = $request->getUri(); $path = $uri->getPath(); $matches = []; if (preg_match("#^(" . implode('|', $codes) . ")\\/#", $path, $matches)) { $this->regionCode = $matches[1]; $newPath = substr($path, strlen($matches[0])); $uri = $uri->withPath($newPath); $request = $request->withUri($uri, true); } else { $this->regionCode = $codes[0]; } // // Initialize the services using the acquired region code $modules = \Chubby\AppFactory::getApp()->getModules(); foreach ($this->services->get() as $service) { $service->init($this->regionCode, $modules); } } return $request; }
/** * @param array $settings */ public function __construct($settings) { $this->container = \Chubby\AppFactory::getApp()->getSlim()->getContainer(); if (!isset($settings['urlRoot'])) { throw new \Exception("Missing urlRoot."); } if (substr($settings['urlRoot'], -1) == '/') { $settings['urlRoot'] = substr($settings['urlRoot'], 0, -1); } $this->urlRoot = $settings['urlRoot']; // Setup Cartalyst's Sentinel if (!isset($settings['database'])) { throw new \Exception("Missing database connection parameters."); } $this->capsule = new Capsule(); $this->capsule->addConnection($settings['database']); $this->capsule->bootEloquent(); // We use a closure to reference the sentinel object so // the object itself isn't created until the first time it's needded. // This is IMPORTANT because because native installations of Sentinel, by using // Symfony's HttpFoundation's Request object, will consume the php://input stream. // If Symfony's object gets to consume the stream before Slim, we won't be able to // get any input from it. // So, we MUST make sure that Sentinel getter (getSentinel()) is executed AFTER // Slim has processed the request, hence read the php://input stream. $this->sentinel = function () { static $obj = null; if ($obj == null) { $obj = Sentinel::instance()->getSentinel(); } return $obj; }; }
/** * @inheritdoc */ public function getLabel() { static $locale = null; if ($locale == null) { $c = \Chubby\AppFactory::getApp()->getSlim()->getContainer(); $locale = $c['settings']['locale']; } return $locale()->say(parent::getLabel()); }
/** * Returns the crumb's label. * * @return string */ public function getName() { static $locale = null; if ($locale == null) { $c = \Chubby\AppFactory::getApp()->getSlim()->getContainer(); $locale = $c['settings']['locale']; } return $locale()->say($this->name); }
/** * */ public function __construct($domain) { $this->domain = $domain; $this->section = '*'; $this->variable = '*'; $container = \Chubby\AppFactory::getApp()->getSlim()->getContainer(); if (isset($container['appParamsBootstrapService'])) { $this->bootstrapper = $container['appParamsBootstrapService']->bootstrapper; } else { $this->bootstrapper = new Bootstrapper(); } $this->initData(); }
/** * Serve the asset. * * @param array $args */ public function get(ServerRequestInterface $request, ResponseInterface $response, $args) { $module = \Chubby\AppFactory::getApp()->getModules()[$args['module']]; $fullFile = $this->getFilePath($args['module'], $module['path'], $args['path'], $args['file']); if ($fullFile === false) { $response = $response->withStatus(404); } else { $mimeType = \Defr\MimeType::get($args['file']); $body = $response->getBody(); $body->write(file_get_contents($fullFile)); $response = $response->withStatus(200)->withHeader('Content-type', $mimeType)->withBody($body); } return $response; }
/** * Loads modules from all recognizable locations. * To find moudles Chubby uses composer's map stored at APP_PATH/vendor/composer/autoload_psr4.php * Chubby expects two locations for modules: the application's APP_PATH/Modules and as composer packages * installed somewhere under APP_PATH/vendor. * Installed modules are required to have the root namespace Chubby\Modues so they can be found by this loader. */ public static function loadModules(\Slim\Container $container) { $sources = array_merge([['path' => APP_PATH . DS . 'Modules']], self::findPackages('Modules')); $modules = []; foreach ($sources as $source) { $path = $source['path']; foreach (new \DirectoryIterator($path) as $fileInfo) { if ($fileInfo->isDot() || $fileInfo->isFile()) { continue; } $moduleName = $fileInfo->getBasename(); $className = "{$moduleName}Module"; if (isset($source['namespace'])) { $modulesNameSpace = "\\{$source['namespace']}"; } else { $modulesNameSpace = \Chubby\AppFactory::getApp()->appNamespace . "\\Modules"; } $moduleClassName = "{$modulesNameSpace}\\{$moduleName}\\{$className}"; // // Load the module class $moduleObject = new $moduleClassName(); $required = 'Chubby\\AbstractModule'; if (!$moduleObject instanceof \Chubby\AbstractModule) { throw new \Exception("Module class {$moduleClassName} MUST extend {$required}."); } $module = new $moduleClassName(); $priority = $module->getPriority(); if ($priority < 0) { $priority = 0; } // Highest allowed priority // Only MainModule is allowed to have prioarity 0 if ($priority == 0 && $className != 'MainModule') { $priority = 1; } if ($moduleName != 'Main') { if (isset($modules[$priority][$moduleName])) { // Resolve name collisions by adding an underscore separated namespace prefix. $nameSpace = str_replace('\\', '_', $moduleNameSpace); $moduleName = "{$nameSpace}_{$moduleName}"; } } $modules[$priority][$moduleName] = ['object' => $module, 'path' => $path]; } } // // Modules are sorted by priority to allow for manual initialization sequence. krsort($modules); return $modules; }
/** * This general method provide impelemenatation classes two oportunities to break the middleware * chain by returning a value from runBeforeNext() or runAfterNext(). * If any of these functions return a value other than null, that value will be returned by the __invoke() itself. */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) { $this->request = $request; $this->response = $response; $this->slim = \Chubby\AppFactory::getApp()->getSlim(); $this->container = $this->slim->getContainer(); $returned = $this->runBeforeNext(); if ($returned instanceof ResponseInterface) { return $returned; } $this->response = $next($this->request, $this->response); $returned = $this->runAfterNext(); if ($returned instanceof ResponseInterface) { return $returned; } return $this->response; }
/** * @param array $settings */ public function __construct($settings) { $this->container = \Chubby\AppFactory::getApp()->getSlim()->getContainer(); if (!isset($settings['urlRoot'])) { throw new \Exception("Missing urlRoot."); } if (substr($settings['urlRoot'], -1) == '/') { $settings['urlRoot'] = substr($settings['urlRoot'], 0, -1); } $this->urlRoot = $settings['urlRoot']; // Setup Cartalyst's Sentinel if (!isset($settings['database'])) { throw new \Exception("Missing database connection parameters."); } $this->capsule = new Capsule(); $this->capsule->addConnection($settings['database']); $this->capsule->bootEloquent(); $this->sentinel = Sentinel::instance()->getSentinel(); }
/** * @return \Slim\App The instance of Slim stored in the singleton Chubby\App object. */ public function getSlim() { return \Chubby\AppFactory::getApp()->getSlim(); }
defined('DS') || define('DS', DIRECTORY_SEPARATOR); /** * Search for local settings. */ if (is_readable(HOME_PATH . DS . 'debug.php')) { include HOME_PATH . DS . 'debug.php'; // Dev environment only } defined('DEBUG') || define('DEBUG', false); /** * Chubby is conceived to organize code in a way that every single file is located outside the public_html directory. * The PRIVATE_HTML constant should point to the root directory to your projects. */ defined('PRIVATE_HTML') || define('PRIVATE_HTML', DS . 'home' . DS . 'user' . DS . 'private_html'); /** * Location of this application's files. * Here we are assuming that the application is located under a directory named the same as \ * the directory containing the index.php file. */ defined('APP_PATH') || define('APP_PATH', PRIVATE_HTML . DS . basename(__DIR__)); /** * Location of the vendor directory. * This is most of the time right below APP_PATH. */ defined('VENDOR_PATH') || define('VENDOR_PATH', APP_PATH . DS . 'vendor'); /** * Create the application object and run... */ require VENDOR_PATH . DS . 'autoload.php'; \Chubby\AppFactory::getApp()->run('App');