Skip to content

Commit

Permalink
Add test app for backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Dec 8, 2023
1 parent 4d209b3 commit 355616d
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Template/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ public function addGlobal($name, $value)
*
* @param string $template
* @param array<string, mixed> $data
* @param string $extension
* @return string
*/
public function render($template, array $data = array())
public function render($template, array $data = [], $extension = 'html')
{
return $this->twig->render($template, $data);
return $this->twig->render("$template.$extension", $data);
}
}
27 changes: 27 additions & 0 deletions tests/Component/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Rougin\Slytherin\Application;
use Rougin\Slytherin\Component\Collector;
use Rougin\Slytherin\Container\Container;

$root = dirname(dirname(__DIR__));

require $root . '/vendor/autoload.php';

$items = array();

$items[] = 'Rougin\Slytherin\Fixture\Components\CollectionComponent';
$items[] = 'Rougin\Slytherin\Fixture\Components\DebuggerComponent';
$items[] = 'Rougin\Slytherin\Fixture\Components\DispatcherComponent';
$items[] = 'Rougin\Slytherin\Fixture\Components\HttpComponent';
$items[] = 'Rougin\Slytherin\Fixture\Components\SingleComponent';

$container = new Container;

$components = Collector::get($container, $items, $globals);

var_dump($components);

$app = new Application($components);

$app->run();
15 changes: 15 additions & 0 deletions tests/Previous/Handlers/Hello.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rougin\Slytherin\Previous\Handlers;

class Hello
{
public function __invoke($request, $response, $next = null)
{
$response = $next($request, $response);

$response->getBody()->write('Hello from middleware');

return $response;
}
}
1 change: 1 addition & 0 deletions tests/Previous/Plates/Greet.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world!
1 change: 1 addition & 0 deletions tests/Previous/Plates/Hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello {{ name }}!
19 changes: 19 additions & 0 deletions tests/Previous/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Rougin\Slytherin\Dispatching\Router;

$name = 'Rougin\Slytherin\Previous\Routes';

$router = new Router;

$router->addRoute('GET', '/', [ "$name\Hello", 'index' ]);

$router->addRoute('GET', '/hi/:name', [ "$name\Hello", 'hi' ]);

// Add the middlewares to a specified route ---------------
$items = array('Rougin\Slytherin\Previous\Handlers\Hello');

$router->addRoute('GET', '/hello', function () {}, $items);
// --------------------------------------------------------

return $router;
25 changes: 25 additions & 0 deletions tests/Previous/Routes/Hello.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rougin\Slytherin\Previous\Routes;

use Rougin\Slytherin\Template\RendererInterface;

class Hello
{
protected $renderer;

public function __construct(RendererInterface $renderer)
{
$this->renderer = $renderer;
}

public function index()
{
return $this->renderer->render('Greet');
}

public function hi($name)
{
return $this->renderer->render('Hello', compact('name'));
}
}
64 changes: 64 additions & 0 deletions tests/Previous/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

use Rougin\Slytherin\Application;
use Rougin\Slytherin\ComponentCollection;
use Rougin\Slytherin\Dispatching\Dispatcher;
use Rougin\Slytherin\ErrorHandler\Whoops;
use Rougin\Slytherin\IoC\Auryn;
use Rougin\Slytherin\Middleware\StratigilityMiddleware;
use Rougin\Slytherin\Template\RendererInterface;
use Rougin\Slytherin\Template\Twig;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Stratigility\MiddlewarePipe;

$root = dirname(dirname(__DIR__));

require $root . '/vendor/autoload.php';

$component = new ComponentCollection;

// Initialize the RendererInterface ------------
$views = (string) realpath(__DIR__ . '/Plates');
$loader = new Twig_Loader_Filesystem($views);
$twig = new Twig(new Twig_Environment($loader));
$renderer = RendererInterface::class;
// ---------------------------------------------

// Initialize the DependencyInjectorInterface ---
$auryn = new Auryn(new \Auryn\Injector);
// Create an alias for the RendererInterface ---
$auryn->share($twig);
$auryn->alias($renderer, get_class($twig));
// ---------------------------------------------
$component->setDependencyInjector($auryn);
// ----------------------------------------------

// Initialize the ErrorHandlerInterface ---
$whoops = new Whoops(new \Whoops\Run);
$component->setErrorHandler($whoops);
// ----------------------------------------

// Initialize the ServerRequestInterface and ResponseInterface ---
$request = ServerRequestFactory::fromGlobals();
$response = new Response;
$component->setHttp($request, $response);
// ---------------------------------------------------------------

// Initialize the routing dispatcher interface -----
$router = require realpath(__DIR__ . '/Router.php');
$dispatcher = new Dispatcher($router, $response);
$component->setDispatcher($dispatcher);
// -------------------------------------------------

// Initialize the middleware -------------------
$pipe = new MiddlewarePipe;
$middleware = new StratigilityMiddleware($pipe);
$component->setMiddleware($middleware);
// ---------------------------------------------

// Initialize then run the Application ---
$app = new Application($component);

$app->run();
// ---------------------------------------

0 comments on commit 355616d

Please sign in to comment.