Example #1
0
<?php

namespace Epic;

session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);
define('SITE_URL', 'http://epic-blog/lesson%2010/src/public/index.php');
require '../vendor/autoload.php';
Lib\connection(['host' => 'localhost', 'dbname' => 'blog', 'user' => 'root', 'password' => 'vagrant', 'encoding' => 'utf8']);
$router = new Router();
$router->add('home', '\\Epic\\Controllers\\Home', ['\\Epic\\Lib\\is_logged']);
$router->add('profile', '\\Epic\\Controllers\\Profile', ['\\Epic\\Lib\\is_logged']);
$router->add('login', '\\Epic\\Controllers\\Login');
$router->handle();
Example #2
0
 public function testClosures()
 {
     $router = new Router($this->collection);
     $router->handle('closure/123/alpha');
     $closure = $router->controllerName();
     $expects = call_user_func_array($closure, $router->params());
     $this->assertTrue(is_callable($router->controllerName()));
     $this->assertEquals($expects, '123-alpha');
 }
Example #3
0
 public static function transfer(Router $router, $where, $data = false)
 {
     $route = new static(false, false, $where);
     $router->setController($route->getController());
     $router->setAction($route->getAction());
     if (is_array($data)) {
         $router->setData($data);
     }
     return $router->handle();
 }
Example #4
0
File: App.php Project: pagon/core
 /**
  * App will run
  */
 public function run()
 {
     // Check if run
     if ($this->_run) {
         throw new \RuntimeException("Application already running");
     }
     // Save current app
     self::$self = $this;
     // Emit run
     $this->emit('run');
     // Set run
     $this->_run = true;
     $_error = false;
     $_path = $this->input->path();
     if ($this->injectors['error']) {
         // If config error, register error handle and set flag
         $_error = true;
         $this->registerErrorHandler();
     }
     try {
         // Emit "bundle" event
         $this->emit('bundle');
         /**
          * Loop bundles and lookup the bundle info and start the bundle
          */
         foreach ($this->injectors['bundles'] as $id => $options) {
             // Set id
             $id = isset($options['id']) ? $options['id'] : $id;
             // Set bootstrap file
             $bootstrap = isset($options['bootstrap']) ? $options['bootstrap'] : 'bootstrap.php';
             // Set dir to load
             $dir = isset($options['dir']) ? $options['dir'] : 'bundles/' . $id;
             // Path check, if not match start of path, skip
             if (isset($options['path']) && strpos($_path, $options['path']) !== 0) {
                 continue;
             }
             // Check the file path
             if (!($file = $this->path($dir . '/' . $bootstrap))) {
                 throw new \InvalidArgumentException('Bundle "' . $id . '" can not bootstrap');
             }
             // Check if bootstrap file loaded
             if (isset(self::$loads[$file])) {
                 throw new \RuntimeException('Bundle "' . $id . '" can not bootstrap twice');
             }
             // Emit "bundle.[id]" event
             $this->emit('bundle.' . $id);
             // Set variable for bootstrap file
             $app = $this;
             extract($options);
             require $file;
             // Save to loads
             self::$loads[$file] = true;
         }
         // Start buffer
         if ($this->injectors['buffer']) {
             ob_start();
         }
         if (!in_array(array('', $this->router, array()), $this->injectors['stacks'])) {
             $this->injectors['stacks'][] = $this->router;
         }
         // Emit "middleware" event
         $this->emit('middleware');
         // Process the stacks
         if (!$this->router->handle($this->injectors['stacks'], function ($stack) use($_path) {
             // Try to match the path
             if (is_array($stack) && $stack[0] && strpos($_path, $stack[0]) === false) {
                 return false;
             }
             if (!is_array($stack)) {
                 return $stack;
             } else {
                 return Middleware::build($stack[1], $stack[2]);
             }
         })) {
             $this->handleError('404');
         }
         // Write direct output to the head of buffer
         if ($this->injectors['buffer']) {
             $this->output->write(ob_get_clean());
         }
     } catch (Exception\Stop $e) {
     } catch (\Exception $e) {
         if ($this->injectors['debug']) {
             throw $e;
         } else {
             try {
                 $this->handleError('exception', $e);
             } catch (Exception\Stop $e) {
             }
         }
         $this->emit('error');
     }
     $this->_run = false;
     // Send start
     $this->emit('flush');
     // Flush
     $this->flush();
     // Send end
     $this->emit('end');
     if ($_error) {
         $this->restoreErrorHandler();
     }
 }
Example #5
0
        $file = __DIR__ . '/' . $val . '/' . $filename;
        if (file_exists($file) == true) {
            include $file;
            break;
        }
    }
    return false;
}
$config = new Config();
require 'config.php';
DB::connect($config);
$registry = new Registry($config);
$router = new Router($registry->request);
$routes = array();
require 'routes.php';
foreach ($routes as $path => $param) {
    if ($router->isMatch($param['pattern'])) {
        $router->handle($param['pattern'], $param['data'], function () use(&$registry, $path, $param) {
            if ($registry->auth->canAccessPage()) {
                /** @var Controller $controller */
                $controller = new $param['className']($registry);
                $data = $controller->{$path . 'Action'}();
                $controller->customize($data);
                $controller->render('page', $path);
            } else {
                header('Location: ' . BASE_URL . '/login');
            }
        });
    }
}
Controller::handleNotFound();