Exemple #1
0
 private function packageForDevelopment($packages)
 {
     $assets = Current::$config->get('css');
     $files = Current::$request->getInfo('css');
     // Merge all files into an array
     foreach ($packages as $package) {
         $files = array_merge($files, $assets[$package]);
     }
     list($app_css_path, $css_path) = Current::$config->gets('paths.app_css', 'paths.urls.css');
     // Transform into correct paths
     foreach ($files as $key => $file) {
         $files[$key] = Cowl::url(str_replace($app_css_path, $css_path, $file));
     }
     Current::$request->setInfo('css', $files);
 }
Exemple #2
0
 public function postCommandRun(Command $command, $method, $request)
 {
     $mode = Current::$config->get('mode');
     $js_packages = $command->getJS();
     if ($mode == 'production') {
         $this->packageForProduction($js_packages);
     } else {
         $this->packageForDevelopment($js_packages, $command, $request);
     }
     // This cannot change with the new packaging
     // Unless we package all pages into one file... which would be nice. But cost in performance?
     // --
     // See if this command has page.commandname.js
     $page_name = 'app/' . $request->app_directory . 'page.' . end($request->pieces) . '.js';
     $page_name = $this->parsePath($page_name);
     if (file_exists($page_name)) {
         $this->files[] = $page_name;
         Current::$request->setInfo('js_fire', substr(get_class($command), 0, -strlen('command')));
         Current::$request->setInfo('js_page_path', Cowl::url($page_name));
     }
     // --
     $this->setFiles();
 }
Exemple #3
0
 public final function run(RequestData $request)
 {
     $this->request = $request;
     $view = explode('.', $request->command);
     $view = $view[1];
     $args = array_slice($request->argv, 1);
     $method = count($args) ? $args[count($args) - 1] : false;
     try {
         $this->template->setType("html");
         // Call initialize method, if one exists
         if (method_exists($this, 'initialize')) {
             $redirect = call_user_func_array(array($this, 'initialize'), $args);
             if (is_array($redirect) || is_string($redirect)) {
                 $url = is_array($redirect) ? Cowl::url($redirect) : $redirect;
                 return $url;
             }
         }
         // If aliases exists, "reroute" the method
         if (isset($this->aliases[$method]) && method_exists($this, $this->aliases[$method])) {
             $method = $this->aliases[$method];
         } elseif (!$method || $method == 'run' || !method_exists($this, $method)) {
             $method = 'index';
         }
         // Ensure that method is public
         $reflection = new ReflectionMethod(get_class($this), $method);
         if (!$reflection->isPublic()) {
             $method = 'index';
         }
         $request->method = $method;
         // Set view to either the base-name of the class, which is default or the name of the method
         if (is_null($this->view) && $this->template->exists('view.' . $method . '.php')) {
             $this->setView($method);
         } else {
             if (is_null($this->view)) {
                 $this->setView($view);
             }
         }
         // If a global layout type has defined use that
         // Set the appropriate layout for the response type
         try {
             $type = isset($this->layouts['*']) ? $this->layouts['*'] : $request->response_type;
             $type = isset($this->layouts[$type]) ? $this->layouts[$type] : $type;
             // Check if the user has restricted which layouts can automatically be set
             $allowed_types = Current::$config->getOr('allowed_layouts', true);
             if (is_array($allowed_types) && !in_array($type, $allowed_types)) {
                 throw new TPLNotAllowedHereException($type);
             }
             $this->template->setType($type);
         } catch (Exception $e) {
             $html_type = isset($this->layouts['html']) ? $this->layouts['html'] : 'html';
             $this->template->setType($html_type);
             $request->response_type = 'html';
         }
         if ($this->add_request_to_template) {
             $this->template->add('request', $request);
         }
         Current::$request->setInfo('request', $request);
         Current::$plugins->hook('commandRun', $this, $method, $request);
         // Prepare some stuff before firing the command
         $this->requestBegan();
         $this->error_occured = false;
         // _This_ is where all the magic happens
         $ret = call_user_func_array(array($this, $method), $args);
         // If an array is returned it is used as pieces for a <Cowl::url> redirect
         if (is_array($ret) || is_string($ret) && strlen($ret)) {
             $this->requestEnded();
             return is_array($ret) ? Cowl::url($ret) : $ret;
         }
     } catch (RegistryMemberNotFoundException $e) {
         $this->error_occured = 400;
         header("HTTP/1.1", true, 400);
     } catch (AbortCommand $e) {
         $code = $e->getMessage();
         $this->error_occured = $code;
         header("HTTP/1.1", true, $code);
     } catch (CowlRedirectCommand $e) {
         $url = $e->getURL();
         if (is_array($url) || is_string($url) && strlen($url)) {
             $this->requestEnded();
             return is_array($url) ? Cowl::url($url) : $url;
         }
     }
     if ($this->error_occured) {
         if (method_exists($this, 'onError')) {
             $this->onError();
         }
     }
     Current::$plugins->hook('postCommandRun', $this, $method, $request);
     // Render the template if not in CLI environment
     if (!COWL_CLI) {
         if (!$this->error_occured) {
             $this->template->render($this->view);
         } else {
             $this->template->renderError($this->error_occured);
         }
     }
     $this->requestEnded();
 }
Exemple #4
0
 public function execute()
 {
     $this->controller = new Controller($this->path);
     $this->static_server = new StaticServer($this->path);
     Current::$plugins->hook('prePathParse', $this->controller, $this->static_server);
     if ($this->static_server->isFile()) {
         Current::$plugins->hook('preStaticServe', $this->static_server);
         // Output the file
         $this->static_server->render();
         Current::$plugins->hook('postStaticServe', $this->static_server);
         exit;
     }
     // Parse arguments from path and call appropriate command
     Cowl::timer('cowl parse');
     $request = $this->controller->parse();
     Cowl::timerEnd('cowl parse');
     if (COWL_CLI) {
         $this->fixRequestForCLI($request);
     }
     $command = new $request->argv[0]();
     // Set template directory, which is the command directory mirrored
     $command->setTemplateDir(Current::$config->get('paths.view') . $request->app_directory);
     Current::$plugins->hook('postPathParse', $request);
     Cowl::timer('cowl command run');
     $ret = $command->run($request);
     Cowl::timerEnd('cowl command run');
     Current::$plugins->hook('postRun');
     if (is_string($ret)) {
         Cowl::redirect($ret);
     }
 }
Exemple #5
0
 public function testURLQueryString()
 {
     $this->assertEquals(Cowl::url('?foo=bar'), '/?foo=bar');
     $this->assertEquals(Cowl::url('sub', 'command?foo=bar&moo'), '/sub/command?foo=bar&moo');
     $this->assertEquals(Cowl::url('a', 'b.html?c=d&e=f'), '/a/b.html?c=d&e=f');
 }
Exemple #6
0
 public function html($url)
 {
     if ($this->pages <= 1) {
         return '';
     }
     $html = '';
     // This is so we can replace __PAGE__ with the page
     $page_index = array_search('__PAGE__', $url);
     if ($page_index === false) {
         $page_index = count($url);
     }
     if ($this->page > 1) {
         $url[$page_index] = $this->page - 1;
         $html .= sprintf('<a href="%s">&laquo;</a>', Cowl::url($url));
     } else {
         $html .= '&laquo;';
     }
     for ($i = 1; $i <= $this->pages; $i++) {
         if ($this->page == $i) {
             $html .= ' ' . $i;
             continue;
         }
         $url[$page_index] = $i;
         $html .= sprintf(' <a href="%s">%s</a>', Cowl::url($url), $i);
     }
     if ($this->page < $this->pages) {
         $url[$page_index] = $this->page + 1;
         $html .= sprintf(' <a href="%s">&raquo;</a>', Cowl::url($url));
     } else {
         $html .= ' &raquo;';
     }
     return $html;
 }
Exemple #7
0
function js()
{
    $request = Current::$request->getInfo('request');
    printf('<script type="text/javascript">');
    printf('	var COWL_BASE = "%s";', COWL_BASE);
    printf('	var COWL_CURRENT = [%s];', fimplode('"%__val__;"', array_map('addslashes', $request->original_request), ', '));
    printf('</script>');
    $scripts = Current::$request->getInfo('js');
    if (is_array($scripts)) {
        $tag = Current::$config->get('release_tag');
        if ($tag === 'dev') {
            $tag .= time();
        }
        foreach ($scripts as $script) {
            printf('<script type="text/javascript" src="%s?%s"></script>', Cowl::url($script), $tag);
        }
    }
    if ($fire = Current::$request->getInfo('js_fire')) {
        $request = Current::$request->getInfo('request');
        printf('<script type="text/javascript">Cowl.fire("%s", "%s")</script>', strtolower(implode('.', $request->pieces)), $request->method);
    }
}