Esempio n. 1
0
 public function injectResponse(ViewEvent $e)
 {
     parent::injectResponse($e);
     $renderer = $e->getRenderer();
     if ($renderer !== $this->renderer) {
         return;
     }
     $response = $e->getResponse();
     $headers = $response->getHeaders();
     $headers->addHeaderLine('Content-Disposition: inline; filename=' . $renderer->getFilename());
     $headers->addHeaderLine('content-type', 'text/calendar; charset=UTF-8');
     $headers->addHeaderLine('x-content-type-options', 'nosniff');
     $headers->addHeaderLine('cache-control', 'no-cache, no-store, max-age=0, must-revalidate');
 }
Esempio n. 2
0
    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);
    }
Esempio n. 3
0
    public function testRender()
    {
        $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());
        /////////////////////////////////////////////////
        $viewConfigExample = ['some-view' => ['template' => 'some-template', 'viewModel' => 'some-view-model|\\Some\\ViewModel::class', 'extend' => 'parent-view', 'capture' => 'some-capture', 'children' => ['child-view'], 'childrenDynamicLists' => ['child-view' => 'listVar'], 'data' => ['fromGlobal' => 'varName', 'fromParent' => 'varName', 'static' => ['key' => 'value']], 'dataProvider' => 'some-view-model|\\Some\\DataProvider::class']];
        //////////////
        $viewConfig = ['page' => ['template' => 'page', 'children' => ['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']]]]]]], 'comments-list' => ['template' => 'comments-list', 'children' => ['comment' => ['viewModel' => \Sebaks\ViewTest\CommentViewModel::class, 'template' => 'comment', 'children' => ['user' => ['viewModel' => \Sebaks\ViewTest\UserViewModel::class, 'template' => 'user', 'data' => ['fromParent' => ['comment:userId' => 'userId'], 'static' => ['class' => 'user']]]], 'data' => ['fromParent' => ['comment' => 'comment']]]], 'childrenDynamicLists' => ['comment' => 'comments'], 'data' => ['fromGlobal' => 'comments']]];
        $data = ['comments' => [['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\MyselfViewModel::class, \Sebaks\ViewTest\MyselfViewModel::class, false);
        /////////////////////
        $config = new Config($viewConfig);
        $viewBuilder = new ViewBuilder($config, $serviceLocator);
        $pageViewModel = $viewBuilder->build($viewConfig['page'], $data);
        /////////////////////
        $view->render($pageViewModel);
        $result = $response->getBody();
        $expected = '<ul><li>text of c1
<div class="user">John</div></li><li>text of c2
<div class="user">Helen</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>';
        $this->assertEquals($expected, $result);
    }