require dirname(__DIR__) . '/vendor/autoload.php'; use Zend\Diactoros; use ExpressiveAsync\Server; use React\EventLoop\Factory; use React\Socket\Server as SocketServer; use ExpressiveAsync\Application; use React\Promise\Deferred; $serviceManager = new \Zend\ServiceManager\ServiceManager(); $eventLoop = Factory::create(); $socketServer = new SocketServer($eventLoop); $httpServer = new Server($socketServer); $serviceManager->setFactory('EventLoop', function () use($eventLoop) { return $eventLoop; }); $serviceManager->setInvokableClass('Zend\\Expressive\\Router\\RouterInterface', 'Zend\\Expressive\\Router\\FastRouteRouter'); $router = new \Zend\Expressive\Router\FastRouteRouter(); $router->addRoute(new \Zend\Expressive\Router\Route('/', function ($request, $response) use($eventLoop) { echo 'Home executed' . PHP_EOL; return new Diactoros\Response\HtmlResponse('Hello World.'); }, ['GET'], 'home')); $router->addRoute(new \Zend\Expressive\Router\Route('/deferred', function ($request, $response) use($eventLoop) { // create a request, wait 1-5 seconds and then return a response. $deferred = new Deferred(); $eventLoop->addTimer(rand(1, 5), function () use($deferred) { echo 'Timer executed' . PHP_EOL; $deferred->resolve(new Diactoros\Response\HtmlResponse('Deferred response.')); }); return new \ExpressiveAsync\DeferredResponse($deferred->promise()); }, ['GET'], 'deferred')); $router->addRoute(new \Zend\Expressive\Router\Route('/form', function ($request, $response) use($eventLoop) {
public function test() { /* $viewConfigExample = [ 'some-view' => [ // view name 'template' => 'some-template', // html template //--- not required options 'viewModel' => 'some-view-model|\Some\ViewModel::class', // Instance of ViewModel 'extend' => 'parent-view', // extended view 'capture' => 'some-capture', // for grouping views 'children' => [ 'child-view'// array of views ], 'childrenDynamicLists' => [ // will be generated by list from one of child 'child-view' => 'listVar' // every entry in listVar will be setted to genereted child ], 'data' => [ // required data 'fromGlobal' => 'varName', // // will be set as variables from global data 'fromParent' => 'varName', // will be set as variables by calling getVariable('varName') from parent 'static' => [ // will be set as variables 'key' => 'value' ], ], ], ]; */ $renderer = new PhpRenderer(); $resolver = new Resolver\AggregateResolver(); $map = new Resolver\TemplateMapResolver(array('page' => __DIR__ . '/view/page.phtml', 'comments-list' => __DIR__ . '/view/comments-list.phtml', 'comment' => __DIR__ . '/view/comment.phtml', 'user' => __DIR__ . '/view/user.phtml')); $stack = new Resolver\TemplatePathStack(array('script_paths' => array(__DIR__ . '/view'))); $resolver->attach($map)->attach($stack)->attach(new Resolver\RelativeFallbackResolver($map))->attach(new Resolver\RelativeFallbackResolver($stack)); $renderer->setResolver($resolver); $view = new View(); $response = new Response(); $view->setResponse($response); $strategy = new PhpRendererStrategy($renderer); $strategy->attach($view->getEventManager()); $viewConfig = ['layouts' => ['layout' => ['template' => 'layout']], 'contents' => ['page' => ['layout' => 'layout', 'template' => 'page', 'children' => ['comments-list' => ['extend' => 'comments-list', 'template' => 'comments-list'], 'comment-create' => ['template' => 'comment-create', 'children' => ['myself-info' => ['viewModel' => \Sebaks\ViewTest\MyselfViewModel::class, 'template' => 'user'], 'comment-create-form' => ['template' => 'form', 'children' => ['form-element-textarea' => ['capture' => 'form-element', 'template' => 'form-element-textarea'], 'form-element-button' => ['capture' => 'form-element', 'template' => 'form-element-button']]]]], 'users-table' => ['template' => 'table', 'children' => ['table-head-rows' => ['template' => 'table-tr', 'data' => ['fromParent' => 'rows'], 'children' => ['table-th' => ['template' => 'table-th', 'capture' => 'table-td', 'data' => ['fromParent' => 'value']]], 'childrenDynamicLists' => ['table-th' => 'rows']], 'table-body-rows' => ['template' => 'table-tr', 'data' => ['fromParent' => 'rows'], 'children' => ['table-td' => ['template' => 'table-td', 'data' => ['fromParent' => 'value']]], 'childrenDynamicLists' => ['table-td' => 'rows']]], 'childrenDynamicLists' => ['table-body-rows' => 'bodyRows', 'table-head-rows' => 'headRows'], 'data' => ['static' => ['headRows' => [['Id', 'Name']], 'bodyRows' => [['1', 'John'], ['2', 'Helen']]]]]]]], 'blocks' => ['comments-list' => ['children' => ['comment' => ['viewModel' => \Sebaks\ViewTest\CommentViewModel::class, 'template' => 'comment', 'children' => ['user' => ['viewModel' => \Sebaks\ViewTest\UserViewModel::class, 'template' => 'user', 'data' => ['fromParent' => 'userId', 'static' => ['class' => 'user']], 'children' => ['location' => ['viewModel' => \Sebaks\ViewTest\LocationViewModel::class, 'template' => 'location', 'data' => ['fromParent' => 'countryId']]]]], 'data' => ['fromParent' => ['foo' => 'bar', 'comment' => 'comment']]]], 'childrenDynamicLists' => ['comment' => 'comments'], 'data' => ['fromGlobal' => ['foo' => 'bar', 'result' => 'comments']]]]]; $data = ['result' => [['id' => 'c1', 'userId' => 'u1', 'text' => 'text of c1'], ['id' => 'c2', 'userId' => 'u2', 'text' => 'text of c2']]]; $serviceLocator = new \Zend\ServiceManager\ServiceManager(); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\CommentViewModel::class, \Sebaks\ViewTest\CommentViewModel::class, false); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\UserViewModel::class, \Sebaks\ViewTest\UserViewModel::class, false); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\LocationViewModel::class, \Sebaks\ViewTest\LocationViewModel::class, false); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\MyselfViewModel::class, \Sebaks\ViewTest\MyselfViewModel::class, false); ///////////////////// $config = ['sebaks-view' => $viewConfig]; $serviceLocator->setService('config', $config); $serviceLocator->setService('EventManager', new EventManager()); $request = new Request(); $serviceLocator->setService('Request', $request); //$response = new Response(); $serviceLocator->setService('Response', $response); $e = new MvcEvent(); $e->setRequest($request); $e->setResponse($response); $dispatchResult = new ViewModel(); $dispatchResult->setVariables($data); $e->setResult($dispatchResult); $routeMatch = new RouteMatch([]); $routeMatch->setMatchedRouteName('page'); $e->setRouteMatch($routeMatch); $application = new Application([], $serviceLocator); $e->setApplication($application); ///////////////////// $viewBuilder = new BuildViewListener(); $viewBuilder->injectLayout($e); $pageViewModel = $e->getViewModel(); $view->render($pageViewModel); $result = $response->getBody(); $expected = '<body><ul><li>text of c1 <div class="user">John<span class="location">Ukraine</span></div></li><li>text of c2 <div class="user">Helen<span class="location">United States</span></div></li></ul> <div class="">Me</div><form><textarea></textarea><button type="submit">Submit</button></form><table> <thead> <tr><th>Id</th><th>Name</th></tr> </thead> <tbody> <tr><td>1</td><td>John</td></tr><tr><td>2</td><td>Helen</td></tr> </tbody> </table></body>'; $this->assertEquals($expected, $result); }