Example #1
0
 public function testMuxGetMethod()
 {
     $mux = new Mux();
     $mux->get('/news', array('NewsController', 'listAction'));
     $mux->get('/news_item', array('NewsController', 'itemAction'), array());
     $routes = $mux->getRoutes();
     $this->assertCount(2, $routes);
     $this->assertEquals(2, $mux->length());
     $_SERVER['REQUEST_METHOD'] = "GET";
     $this->assertNotNull($mux->dispatch('/news_item'));
     $this->assertNotNull($mux->dispatch('/news'));
 }
 public function testAnnotations()
 {
     if (defined('HHVM_VERSION')) {
         echo "HHVM does not support Reflection to expand controller action methods";
         return;
     }
     $controller = new ExpandableProductController();
     $this->assertTrue(is_array($map = $controller->getActionMethods()));
     $routes = $controller->getActionRoutes();
     $this->assertNotEmpty($routes);
     $this->assertEquals('', $routes[0][0], 'the path');
     $this->assertEquals('indexAction', $routes[0][1], 'the mapping method');
     $mux = new Pux\Mux();
     // works fine
     // $submux = $controller->expand();
     // $mux->mount('/product', $submux );
     // gc scan bug
     $mux->mount('/product', $controller->expand());
     $paths = array('/product/delete' => 'DELETE', '/product/update' => 'PUT', '/product/add' => 'POST', '/product/foo/bar' => null, '/product/item' => 'GET', '/product' => null);
     foreach ($paths as $path => $method) {
         if ($method) {
             $_SERVER['REQUEST_METHOD'] = $method;
         } else {
             $_SERVER['REQUEST_METHOD'] = 'GET';
         }
         ok($mux->dispatch($path), $path);
     }
 }
Example #3
0
 public function testMuxCustomDispatch()
 {
     $mux = new Mux();
     $mux->mount('/bs/product', function ($x) {
         $x = new Mux();
         $x->any('/create', ['Product', 'createAction']);
         $x->any('/edit/:id', ['Product', 'editAction']);
         return $x;
     });
     $route = $mux->dispatch('/bs/product/create');
     $this->assertNotEmpty($route);
     $this->assertEquals('/create', $route[1]);
     $route = $mux->dispatch('/bs/product/edit/30');
     $this->assertNotEmpty($route);
     $this->assertEquals('/edit/:id', $route[3]['pattern']);
 }
 public function testIndexAction()
 {
     return $this->markTestSkipped('phifty bootstrap script is not fixed yet');
     $handler = new EmailTemplateCRUDHandler();
     $mux = new Mux();
     $mux->mount('/bs/email', $handler);
     $route = $mux->dispatch('/bs/email');
     $this->assertNotNull($route);
     $response = RouteExecutor::execute($route);
     $this->assertNotNull($response);
 }
Example #5
0
 public function testRESTfulDispatch()
 {
     $con = new ProductResourceController();
     $routes = $con->getActionRoutes();
     $this->assertNotEmpty($routes);
     $this->assertTrue(is_array($routes));
     $methods = $con->getActionMethods();
     $this->assertNotEmpty($methods);
     $productMux = $con->expand();
     // there is a sorting bug (fixed), this tests it.
     $this->assertNotEmpty($productMux);
     $root = new Mux();
     $root->mount('/product', $con->expand());
     $_SERVER['REQUEST_METHOD'] = 'GET';
     $this->assertNotNull($root->dispatch('/product/10'));
     $_SERVER['REQUEST_METHOD'] = 'DELETE';
     $this->assertNotNull($root->dispatch('/product/10'));
     $_SERVER['REQUEST_METHOD'] = 'POST';
     $this->assertNotNull($root->dispatch('/product'));
     // create
     $_SERVER['REQUEST_METHOD'] = 'POST';
     $this->assertNotNull($root->dispatch('/product/10'));
     // update
 }
Example #6
0
 public function testMuxMounting()
 {
     $mainMux = new Mux();
     $mainMux->expand = false;
     $pageMux = new Mux();
     $pageMux->add('/page1', array('PageController', 'page1'));
     $pageMux->add('/page2', array('PageController', 'page2'));
     $mainMux->mount('/sub', $pageMux);
     $pageMux2 = new Mux();
     $pageMux2->add('/bar', array('PageController', 'page1'));
     $pageMux2->add('/zoo', array('PageController', 'page2'));
     is(2, $pageMux2->length());
     $mainMux->mount('/foo', $pageMux2);
     is(2, $mainMux->length());
     foreach (array('/sub/page1', '/sub/page2', '/foo/bar', '/foo/zoo') as $p) {
         $r = $mainMux->dispatch($p);
         ok($r, "Matched route for {$p}");
     }
     return $mainMux;
 }
require 'SimpleBench/MatrixPrinter/EzcGraph.php';
require 'SimpleBench/MatrixPrinter/Console.php';
require 'SimpleBench/SystemInfo/Darwin.php';
require 'SimpleBench.php';
// requirement from symfon
require 'symfony/vendor/autoload.php';
require 'pux/PatternCompiler.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Pux\Mux;
$bench = new SimpleBench();
$bench->setN(5000);
$mux = new Mux();
$mux->add('/product/:id', ['ProductController', 'index']);
$bench->iterate('pux extension (dispatch)', function () use($mux) {
    $route = $mux->dispatch('/product/23');
});
$routes = new RouteCollection();
$routes->add('product', new Route('/product/{id}', array('controller' => 'foo', 'action' => 'bar')));
$bench->iterate('symfony/routing (dispatch)', function () use($routes) {
    $context = new RequestContext();
    // this is optional and can be done without a Request instance
    $context->fromRequest(Request::createFromGlobals());
    $matcher = new UrlMatcher($routes, $context);
    $route = $matcher->match('/product/23');
});
$result = $bench->compare();
echo $result->output('console');
Example #8
0
 * Created by PhpStorm.
 * User: Aztyu
 * Date: 26/12/2015
 * Time: 15:24
 */
require_once 'vendor/autoload.php';
require_once 'controller/api/ApiUserController.php';
require_once 'controller/api/ApiFuelController.php';
require_once 'controller/api/SiteController.php';
require_once 'entities/Coordinates.php';
use Pux\Mux;
use Pux\Executor;
$path = explode("?", $_SERVER['REQUEST_URI'])[0];
$api_mux = new Mux();
$api_user_mux = new Mux();
$api_fuel_mux = new Mux();
$main_mux = new Mux();
$api_user_mux->get('/check', ['ApiUserController', 'checkAction']);
$api_user_mux->get('/connect', ['ApiUserController', 'connectAction']);
$api_user_mux->get('/create', ['ApiUserController', 'createAction']);
$api_mux->mount('/user', $api_user_mux);
$api_fuel_mux->get('/find', ['ApiFuelController', 'findAction']);
$api_fuel_mux->post('/insert', ['ApiFuelController', 'insertAction']);
$api_mux->mount('/fuel', $api_fuel_mux);
$main_mux->mount('/api', $api_mux);
$main_mux->get('/', ['SiteController', 'indexAction']);
$route = $main_mux->dispatch($path);
if ($route == null) {
    $route = $main_mux->dispatch('/');
}
echo Executor::execute($route);
Example #9
0
 /**
  * @depends testControllerConstructor
  */
 public function testMountNoExpand($controller)
 {
     $mainMux = new Mux();
     $mainMux->expand = false;
     $mainMux->mount('/product', $controller);
     $mainMux->any('/', array('ProductController', 'indexAction'));
     ok($mainMux->getRoutes());
     count_ok(2, $mainMux->getRoutes(), 'route count should be 2');
     ok($r = $mainMux->dispatch('/product'), 'matched /product');
     // match indexAction
     $this->assertSame(array('CRUDProductController', 'indexAction'), $r[2]);
     ok($r = $mainMux->dispatch('/product/add'));
     $this->assertSame(array('CRUDProductController', 'addAction'), $r[2]);
     ok($r = $mainMux->dispatch('/product/del'));
     $this->assertSame(array('CRUDProductController', 'delAction'), $r[2]);
     ok(null == $mainMux->dispatch('/foo'));
     ok(null == $mainMux->dispatch('/bar'));
 }
Example #10
0
    {
        $path = "office/{$name}";
        if (file_exists($path)) {
            $handle = fopen($path, "r");
            $size = filesize($path);
            $contents = fread($handle, $size);
            $SHA256 = base64_encode(hash('sha256', $contents, true));
            $json = array('BaseFileName' => $name, 'OwnerId' => 'admin', 'Size' => $size, 'SHA256' => $SHA256, 'Version' => '222888822');
            echo json_encode($json);
        } else {
            echo json_encode(array());
        }
    }
    public function getFileAction($name)
    {
        $path = "office/{$name}";
        if (file_exists($path)) {
            $handle = fopen($path, "r");
            $contents = fread($handle, filesize($path));
            header("Content-type: application/octet-stream");
            echo $contents;
        }
    }
}
$mux = new Mux();
$mux->get('/files/:name', ['FilesController', 'getFileInfoAction']);
$mux->get('/files/:name/contents', ['FilesController', 'getFileAction']);
$path = $_SERVER['PATH_INFO'];
$args = explode("&", $path);
$route = $mux->dispatch($args[0]);
Executor::execute($route);
Example #11
0
 public function testSubmuxMergeOptions()
 {
     $mux = new Mux();
     $this->assertEquals(0, $mux->length());
     $submux = new Mux();
     $submux->any('/foo', array('FooController', 'indexAction'), array('default' => array('suffix' => 'csv')));
     $submux->any('/hello/:name', array('HelloController', 'indexAction'), array('default' => array('suffix' => 'json', 'prefix' => 'v1')));
     $this->assertEquals(2, $submux->length());
     ok($routes = $submux->getRoutes());
     $this->assertEquals(2, $submux->length());
     $this->assertEquals(0, $mux->length());
     // XXX: we don't expand routes now, so we won't have the default
     //      options for the subroutes...
     /*
     $mux->mount('/sub', $submux, array(
         'default' => array('suffix' => 'xml', 'prefix' => 'v1'),
         'require' => array('suffix' => '[a-z]{3,4}')
     ));
     */
     $mux->mount('/sub', $submux);
     $this->assertEquals(1, $mux->length());
     $r = $mux->dispatch('/sub/hello/John');
     $this->assertNotNull($r);
     $this->assertEquals('json', $r[3]['default']['suffix']);
     $this->assertEquals('v1', $r[3]['default']['prefix']);
     $this->assertPcreRoute($r, '/hello/:name');
     $r = $mux->dispatch('/sub/foo');
     $this->assertNotNull($r);
     // ok($r[3]['default']['suffix'] == 'csv');
     // ok($r[3]['require']['suffix'] == '[a-z]{3,4}');
     $this->assertNonPcreRoute($r, '/foo');
 }
Example #12
0
 public function testCallableSubMux()
 {
     $mux = new \Pux\Mux();
     $mux->mount('/test', function (Mux $submux) {
         $submux->any('/hello/static', array('HelloController2', 'indexAction'));
         $submux->any('/hello/:name', array('HelloController2', 'indexAction'));
     });
     ok($routes = $mux->getRoutes());
     ok(is_array($routes));
     $this->assertCount(1, $routes);
     $this->assertEquals('/test', $routes[0][1]);
     $r = $mux->dispatch('/test/hello/John');
     $this->assertNotEmpty($r);
     $this->assertPcreRoute($r, '/hello/:name');
     $r = $mux->dispatch('/test/hello/static');
     $this->assertNotEmpty($r);
     $this->assertNonPcreRoute($r, '/hello/static');
 }
Example #13
0
<?php

require '../vendor/autoload.php';
use Pux\Controller\ExpandableController;
use Pux\RouteExecutor;
use Pux\Mux;
use Pux\Router;
class MyController extends Controller
{
    public function indexAction()
    {
        return 'MyController::indexAction()!';
    }
    public function helloAction()
    {
        return 'MyController::helloAction()!';
    }
    /**
     * @uri /foo
     */
    public function overrideAction()
    {
        return 'MyController::overrideAction(), NOT MyController::fooAction()!';
    }
}
$controller = new MyController();
$mux = new Mux();
$mux->mount('/', $controller->expand());
$route = $mux->dispatch($_SERVER['REQUEST_URI']);
printf("Response: %s\n", RouteExecutor::execute($route));