示例#1
0
 public function injectLayout(MvcEvent $e)
 {
     $request = $e->getRequest();
     if (!$request instanceof HttpRequest) {
         return;
     }
     $result = $e->getResult();
     if (!$result instanceof ZendViewModel) {
         return;
     }
     $response = $e->getResponse();
     if ($response->getStatusCode() != 200) {
         return;
     }
     $matchedRoute = $e->getRouteMatch();
     if (!$matchedRoute) {
         return;
     }
     $matchedRouteName = $matchedRoute->getMatchedRouteName();
     $serviceLocator = $e->getApplication()->getServiceManager();
     $config = $serviceLocator->get('config');
     $options = $config['sebaks-view'];
     if (!isset($options['contents'][$matchedRouteName])) {
         return;
     }
     $viewConfig = $options['contents'][$matchedRouteName];
     $config = new Config(array_merge($options['layouts'], $options['contents'], $options['blocks']));
     $viewConfig = $config->applyInheritance($viewConfig);
     $viewBuilder = new ViewBuilder($config, $serviceLocator);
     $data = $result->getVariables()->getArrayCopy();
     if (isset($viewConfig['layout'])) {
         $viewComponentLayout = $viewConfig['layout'];
         if (!isset($options['layouts'][$viewComponentLayout])) {
             throw new \Exception("Layout '{$viewComponentLayout}' not found for view component '{$matchedRouteName}'");
         }
         $options['layouts'][$viewComponentLayout]['children']['content'] = $viewConfig;
         $viewComponent = $viewBuilder->build($options['layouts'][$viewComponentLayout], $data);
     } else {
         $viewComponent = $viewBuilder->build($viewConfig, $data);
     }
     $viewComponent->setTerminal(true);
     $e->setViewModel($viewComponent);
     $e->setResult($viewComponent);
 }
示例#2
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);
    }