Esempio n. 1
0
 function __construct(\cherry\Mvc\Request $request)
 {
     $this->app = \cherry\Application::getInstance();
     $this->request = $request;
     if (is_callable(array($this, 'initialize'))) {
         $this->initialize();
     }
     \Cherry\Base\Event::invoke('cherry:mvc.controller.create', $this);
 }
Esempio n. 2
0
 public function __construct($context = null)
 {
     $this->context = $context;
     Event::invoke(\Cherry\Mvc\EventsEnum::REQUEST_CREATE, $this);
     $this->sapi = php_sapi_name();
     if (!empty($_SERVER['SERVER_PROTOCOL'])) {
         $this->protocol = $_SERVER['SERVER_PROTOCOL'];
     }
     switch ($this->sapi) {
         case 'cli-server':
             $this->server = $_SERVER['HTTP_HOST'];
             $this->uri = $_SERVER['REQUEST_URI'];
             $this->method = $_SERVER['REQUEST_METHOD'];
             $this->remoteip = $_SERVER['REMOTE_ADDR'];
             $this->remoteport = $_SERVER['REMOTE_PORT'];
             $this->protocol = $_SERVER['SERVER_PROTOCOL'];
             if (!empty($_SERVER['HTTP_ACCEPT'])) {
                 $this->accept = new HttpAcceptRequestDirective($_SERVER['HTTP_ACCEPT']);
             } else {
                 $this->accept = new HttpAcceptRequestDirective();
             }
             if (!empty($_SERVER['HTTP_CACHE_CONTROL'])) {
                 $cache_control = $_SERVER['HTTP_CACHE_CONTROL'];
             }
             //$this->cache_control = new HttpCacheRequestDirective($cache_control?:'');
             break;
         case 'cli':
             $this->server = getenv('REQUEST_HOST') ?: 'localhost';
             $this->protocol = 'HTTP/1.1';
             $this->accept = new HttpAcceptRequestDirective('*/*');
             $this->method = getenv('REQUEST_METHOD') ?: 'GET';
             $this->uri = getenv('REQUEST_URI') ?: '/';
         default:
             if (!empty($_SERVER['REQUEST_URI'])) {
                 $this->uri = $_SERVER['REQUEST_URI'];
             }
             if (!$this->method) {
                 $this->method = empty($_SERVER['REQUEST_METHOD']) ? 'GET' : $_SERVER['REQUEST_METHOD'];
             }
     }
     $this->uri = $this->uri ?: '/';
     if (strpos($this->uri, "?")) {
         $uri = substr($this->uri, 0, strpos($this->uri, "?"));
     } else {
         $uri = $this->uri;
     }
     $this->segments = explode("/", trim($uri, "/"));
     $this->method = $this->method ?: 'GET';
 }
Esempio n. 3
0
Event::observe('cherryutil.application.post-setup', function (array $data, $log) {
    $app_root = $data['approot'];
    $cfgpath = \cherry\unipath($app_root . '/application.ini');
    if (!file_exists($cfgpath)) {
        if ($log) {
            $log->write("Application config not found. Creating...\n");
        }
        $cfg = array('application' => array());
    } else {
        $log->write("Updating application configuration...\n");
        $cfg = parse_ini_file($cfgpath, true);
    }
    $cfg['application']['namespace'] = $data['appns'];
    if (empty($cfg['application']['appname'])) {
        $cfg['application']['appname'] = 'MVC Application';
    }
    if (empty($cfg['application']['version'])) {
        $cfg['application']['version'] = '1.0.0';
    }
    if (empty($cfg['application']['uuid']) || $data['replace']) {
        printf("Generating new UUID...");
        $uuid = \cherry\crypto\Uuid::getInstance()->generate(\cherry\crypto\Uuid::UUID_V4);
        //$uuid = trim(exec('uuidgen'));
        printf("%s\n", $uuid);
        $cfg['application']['uuid'] = $uuid;
    } else {
        $log->write("UUID already set. If you wish to regenerate it, use +replace.\n");
    }
    $out = '';
    foreach ($cfg as $group => $cfgvals) {
        $out .= sprintf("[%s]\n", $group);
        foreach ($cfgvals as $k => $v) {
            if (is_string($v)) {
                $v = '"' . $v . '"';
            }
            $out .= sprintf("%s=%s\n", $k, $v);
            if ($cfgvals[$k] == end($cfgvals)) {
                $out .= "\n";
            }
        }
    }
    file_put_contents($cfgpath, $out);
    // Do the actual configuration
    printf("Applying templates...\n");
});
Esempio n. 4
0
 function parseViewTags($str)
 {
     $pos = strpos($str, '<@');
     if ($pos === false) {
         return $str;
     }
     $end = strpos($str, '>', $pos);
     while ($end > $pos) {
         $s_pre = substr($str, 0, $pos);
         $s_tag = substr($str, $pos + 1, $end - $pos - 1);
         $s_aft = substr($str, $end + 1);
         if (substr($s_tag, strlen($s_tag) - 1, 1) == '/') {
             $s_tag = trim(substr($s_tag, 0, strlen($s_tag) - 1));
         }
         $parts = explode(' ', $s_tag);
         $tag = $parts[0];
         $attr = array();
         for ($n = 1; $n < count($parts); $n++) {
             $attrsep = strpos($parts[$n], '=');
             if ($attrsep === false) {
                 $this->attr[$parts[$n]] = true;
             } else {
                 $aname = substr($parts[$n], 0, $attrsep);
                 $aval = trim(substr($parts[$n], $attrsep + 1), "\"'");
                 $attr[strtolower($aname)] = $aval;
             }
         }
         if ($tag == '@content') {
             if (empty($attr['id'])) {
                 $cont = $this->contentview->render();
             } else {
                 $id = $attr['id'];
                 if (array_key_exists($id, $this->subviews)) {
                     $cont = $this->subviews[$id]->render(true);
                 } else {
                     $cont = '<span style="color:red">Error: No such content id ' . $id . '</span>';
                 }
             }
         } elseif ($tag == '@embed') {
             // Switch based on the module
             if (!empty($attr['type'])) {
                 switch (strtolower($attr['type'])) {
                     case 'widget':
                         if (!empty($attr['class'])) {
                             $cn = $attr['class'];
                             $cn = '\\' . str_replace('.', '\\', $cn);
                             if (empty($attr['id'])) {
                                 $id = null;
                             } else {
                                 $id = $attr['id'];
                             }
                             try {
                                 $widget = new $cn($id, $attr);
                                 $cont = $widget->render(true);
                             } catch (\Exception $e) {
                                 $msg = $e->getMessage();
                                 $cont = "<div>Error: {$msg}</div>";
                             }
                         } else {
                             $cont = '<div>Error: Widget requires "class" attribute</div>';
                         }
                         break;
                     default:
                         $cont = "<div>Error: Unknown include type '{$attr['type']}'</div>";
                 }
             }
         } else {
             $cont = Event::invoke('cherry:mvc.render.specialtag', $s_tag, $attr);
         }
         $str = $s_pre . $cont . $s_aft;
         $pos = strpos($str, '<@');
         if ($pos === false) {
             return $str;
         }
         $end = strpos($str, '>', $pos);
     }
     return $str;
 }
Esempio n. 5
0
 function createapp($template = null, $appns = null)
 {
     $con = \Cherry\Cli\Console::getAdapter();
     if (!$appns) {
         printf("Use: create <template> <appns>\n");
         return 1;
     }
     $args = func_get_args();
     $opts = $this->parseOpts(array_slice($args, 2), array('replace' => '+replace'));
     if (empty($opts['replace'])) {
         $opts['replace'] = false;
     }
     $con->write("Creating new project %s...\n", $appns);
     $tpath = CHERRY_LIB . '/share/projects/' . $template . '/';
     $rdi = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($tpath, \FilesystemIterator::SKIP_DOTS));
     foreach ($rdi as $file) {
         if (strpos((string) $file, 'PKG-META') !== false) {
             $sub = explode(DIRECTORY_SEPARATOR, substr($file, strpos((string) $file, 'PKG-META') + 9));
             if ($sub[0] == 'triggers') {
                 require $file;
             }
         } else {
             $dest = str_replace($tpath, './', $file);
             // Check paths
             $path = dirname($dest);
             if (!file_exists($path)) {
                 mkdir($path, 0777, true);
             }
             // Copy file
             copy($file, $dest);
         }
     }
     \Cherry\Base\Event::invoke('cherryutil.application.post-setup', array('approot' => \getcwd(), 'appns' => $appns, 'replace' => $opts['replace']), $con);
     $con->write("Done\n");
 }
Esempio n. 6
0
 function __ob_callback($str)
 {
     $out = '';
     $offs = 0;
     do {
         $pos = strpos($str, '<@');
         if ($pos === false) {
             $out .= $str;
             $str = '';
         }
         $end = strpos($str, '>', $pos);
         $out .= substr($str, 0, $pos);
         if ($end > $pos) {
             $s_tag = trim(substr($str, $pos + 1, $end - $pos - 1));
             $str = substr($str, $end + 1);
             if (substr($s_tag, strlen($s_tag) - 1, 1) == '/') {
                 $s_tag = trim(substr($s_tag, 0, strlen($s_tag) - 1));
             }
             $parts = explode(' ', $s_tag);
             $tag = $parts[0];
             $attr = array();
             for ($n = 1; $n < count($parts); $n++) {
                 $attrsep = strpos($parts[$n], '=');
                 if ($attrsep === false) {
                     $this->attr[$parts[$n]] = true;
                 } else {
                     $aname = substr($parts[$n], 0, $attrsep);
                     $aval = substr($parts[$n], $attrsep + 1);
                     if ($aval[0] == '"') {
                         if ($aval[strlen($aval) - 1] == '"') {
                             // Just unquote it
                             $attr[$aname] = substr($aval, 1, strlen($aval) - 2);
                         } else {
                             $fvals = array(substr($aval, 1));
                             do {
                                 $n++;
                                 if (strpos($parts[$n], '"') !== false) {
                                     $fvals[] = $parts[$n];
                                 } else {
                                     $fvals[] = substr($parts[$n], strlen($parts[$n] - 1));
                                     break;
                                 }
                             } while (true);
                             $attr[$aname] = join(" ", $fvals);
                         }
                     }
                 }
             }
             switch ($tag) {
                 case '@header':
                     $cont = Event::invoke('cherry:mvc.render.head', $tag, (array) $attr);
                     break;
                 case '@embed':
                     $cont = '[EMBED]';
                     break;
                 case '@widget':
                     $cont = '[WIDGET]';
                     break;
                 case '@content':
                     $cont = $this->content;
                     break;
                 default:
                     $cont = Event::invoke('cherry:mvc.render.specialtag', $tag, (array) $attr);
                     break;
             }
             $out .= $cont;
         }
     } while ($str);
     return $out;
 }
Esempio n. 7
0
 function initialize()
 {
     Event::observe(\Cherry\Mvc\EventsEnum::RENDER_SPECIALTAG, array($this, 'onTag'));
 }
Esempio n. 8
0
 function initialize()
 {
     Event::observe('cherry:mvc.render.head', array($this, 'createBar'));
 }