/** * Set cookie * * The second argument may be a single scalar value, in which case * it will be merged with the default settings and considered the `value` * of the merged result. * * The second argument may also be an array containing any or all of * the keys shown in the default settings above. This array will be * merged with the defaults shown above. * * @param string $key Cookie name * @param mixed $value Cookie settings */ public function set($key, $value) { if (is_array($value)) { $cookieSettings = array_replace($this->defaults, $value); } else { $cookieSettings = array_replace($this->defaults, array('value' => $value)); } parent::set($key, $cookieSettings); }
/** * Fetch COOKIE data * * This method returns a key-value array of Cookie data sent in the HTTP request, or * the value of a array key if requested; if the array key does not exist, NULL is returned. * * @param string $key * @return array|string|null */ public function cookies($key = null) { if ($key) { return $this->cookies->get($key); } return $this->cookies; // if (!isset($this->env['pulse.request.cookie_hash'])) { // $cookieHeader = isset($this->env['COOKIE']) ? $this->env['COOKIE'] : ''; // $this->env['pulse.request.cookie_hash'] = Util::parseCookieHeader($cookieHeader); // } // if ($key) { // if (isset($this->env['pulse.request.cookie_hash'][$key])) { // return $this->env['pulse.request.cookie_hash'][$key]; // } else { // return null; // } // } else { // return $this->env['pulse.request.cookie_hash']; // } }
/** * Render a template file * * NOTE: This method should be overridden by custom view subclasses * * @param string $template The template pathname, relative to the template base directory * @param array $data Any additonal data to be passed to the template. * @return string The rendered template * @throws \RuntimeException If resolved template pathname is not a valid file */ protected function render($template, $data = null) { $templateEX = explode(':', $template); $template = explode(':', $template); if ($templateEX[0] == 'Default') { $template[0] = 'index.php'; } else { if (!file_exists('Views/' . $templateEX[0] . '.php')) { $template[0] = 'index.php'; } else { $template[0] = $template[0] . '.php'; } } $templatePathname = $this->getTemplatePathname($template[0]); if (!is_file($templatePathname)) { throw new \RuntimeException("View cannot render `{$template['0']}` because the template does not exist"); } $data = array_merge($this->data->all(), (array) $data); extract($data); ob_start(); $_SESSION['path.now'] = $templateEX[0] . '/' . $template[1]; require $templatePathname; return ob_get_clean(); }
/** * Constructor * @param array $userSettings Associative array of application settings */ public function __construct(array $userSettings = array()) { // Setup IoC container $this->container = new \Pulse\Security\Set(); $this->container['settings'] = array_merge(static::getDefaultSettings(), $userSettings); // Default environment $this->container->singleton('environment', function ($c) { return \Pulse\Environment::getInstance(); }); // Default request $this->container->singleton('request', function ($c) { return new \Pulse\Http\Request($c['environment']); }); // Default response $this->container->singleton('response', function ($c) { return new \Pulse\Http\Response(); }); // Default router $this->container->singleton('router', function ($c) { return new \Pulse\Router(); }); // Default view $this->container->singleton('view', function ($c) { $viewClass = $c['settings']['view']; $templatesPath = $c['settings']['templates.path']; $view = $viewClass instanceof \Pulse\View ? $viewClass : new $viewClass(); $view->setTemplatesDirectory($templatesPath); return $view; }); // Default log writer $this->container->singleton('logWriter', function ($c) { $logWriter = $c['settings']['log.writer']; return is_object($logWriter) ? $logWriter : new \Pulse\LogWriter($c['environment']['pulse.errors']); }); // Default log $this->container->singleton('log', function ($c) { $log = new \Pulse\Log($c['logWriter']); $log->setEnabled($c['settings']['log.enabled']); $log->setLevel($c['settings']['log.level']); $env = $c['environment']; $env['pulse.log'] = $log; return $log; }); // Default mode $this->container['mode'] = function ($c) { $mode = $c['settings']['mode']; if (isset($_ENV['SLIM_MODE'])) { $mode = $_ENV['SLIM_MODE']; } else { $envMode = getenv('SLIM_MODE'); if ($envMode !== false) { $mode = $envMode; } } return $mode; }; // Define default kernel stack $this->kernel = array($this); $this->add(new \Pulse\Kernel\Flash()); $this->add(new \Pulse\Kernel\MethodOverride()); $this->add(new \Pulse\Procedures()); $this->add(new \Pulse\RestAuth()); // Make default if first instance if (is_null(static::getInstance())) { $this->setName('default'); } }