Skip to content

Commit 27db55b

Browse files
committed
Merge pull request #186 from ezsystems/ezp24869-viewController_refactoring
Applied the view controller refactoring
2 parents e05664d + 88f6fcf commit 27db55b

4 files changed

Lines changed: 78 additions & 125 deletions

File tree

Controller/DemoController.php

Lines changed: 32 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010

1111
use eZ\Bundle\EzPublishCoreBundle\Controller;
1212
use eZ\Publish\API\Repository\Values\Content\Location;
13+
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
14+
use eZ\Publish\Core\MVC\Symfony\View\View;
1315
use eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchAdapter;
1416
use Pagerfanta\Pagerfanta;
1517
use Symfony\Component\HttpFoundation\Request;
1618
use Symfony\Component\HttpFoundation\Response;
17-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1819
use eZ\Publish\API\Repository\Values\Content\Query;
1920
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
2021

@@ -41,23 +42,20 @@ public function userLinksAction()
4142
/**
4243
* Renders article with extra parameters that controls page elements visibility such as image and summary.
4344
*
44-
* @param $locationId
45-
* @param $viewType
46-
* @param bool $layout
47-
* @param array $params
45+
* @param \eZ\Publish\Core\MVC\Symfony\View\View $view
46+
*
4847
* @return \Symfony\Component\HttpFoundation\Response
4948
*/
50-
public function showArticleAction($locationId, $viewType, $layout = false, array $params = array())
49+
public function showArticleAction(View $view)
5150
{
52-
return $this->get('ez_content')->viewLocation(
53-
$locationId,
54-
$viewType,
55-
$layout,
56-
array(
51+
$view->addParameters(
52+
[
5753
'showSummary' => $this->container->getParameter('ezdemo.article.full_view.show_summary'),
5854
'showImage' => $this->container->getParameter('ezdemo.article.full_view.show_image'),
59-
) + $params
55+
]
6056
);
57+
58+
return $view;
6159
}
6260

6361
/**
@@ -66,39 +64,24 @@ public function showArticleAction($locationId, $viewType, $layout = false, array
6664
* the view. Since it is not calling the ViewControler we don't need to match a specific
6765
* method signature.
6866
*
69-
* @param \eZ\Publish\API\Repository\Values\Content\Location $location containing blog posts
67+
* @param \eZ\Publish\Core\MVC\Symfony\View\ContentView $view
68+
* @param \Symfony\Component\HttpFoundation\Request $request
69+
*
7070
* @return \Symfony\Component\HttpFoundation\Response
7171
*/
72-
public function listBlogPostsAction(Location $location, Request $request)
72+
public function listBlogPostsAction(ContentView $view, Request $request)
7373
{
74-
$response = new Response();
75-
76-
// Setting default cache configuration (you can override it in you siteaccess config)
77-
$response->setSharedMaxAge($this->getConfigResolver()->getParameter('content.default_ttl'));
78-
79-
// Make the response location cache aware for the reverse proxy
80-
$response->headers->set('X-Location-Id', $location->id);
81-
$response->setVary('X-User-Hash');
82-
8374
$viewParameters = $request->attributes->get('viewParameters');
8475

85-
// Getting location and content from ezpublish dedicated services
86-
$repository = $this->getRepository();
87-
if ($location->invisible) {
88-
throw new NotFoundHttpException("Location #$location->id cannot be displayed as it is flagged as invisible.");
89-
}
90-
91-
$content = $repository
92-
->getContentService()
93-
->loadContentByContentInfo($location->getContentInfo());
94-
95-
// Getting language for the current siteaccess
76+
// This could be changed to use dynamic parameters injection
9677
$languages = $this->getConfigResolver()->getParameter('languages');
9778

9879
// Using the criteria helper (a demobundle custom service) to generate our query's criteria.
9980
// This is a good practice in order to have less code in your controller.
10081
$criteria = $this->get('ezdemo.criteria_helper')->generateListBlogPostCriterion(
101-
$location, $viewParameters, $languages
82+
$view->getLocation(),
83+
$viewParameters,
84+
$languages
10285
);
10386

10487
// Generating query
@@ -115,15 +98,12 @@ public function listBlogPostsAction(Location $location, Request $request)
11598
$pager->setMaxPerPage($this->container->getParameter('ezdemo.blog.blog_post_list.limit'));
11699
$pager->setCurrentPage($request->get('page', 1));
117100

118-
return $this->render(
119-
'eZDemoBundle:full:blog.html.twig',
120-
array(
121-
'location' => $location,
122-
'content' => $content,
123-
'pagerBlog' => $pager,
124-
),
125-
$response
126-
);
101+
$view->addParameters(['pagerBlog' => $pager]);
102+
103+
// The template identifier can be set from the controller action
104+
$view->setTemplateIdentifier('eZDemoBundle:full:blog.html.twig');
105+
106+
return $view;
127107
}
128108

129109
/**
@@ -133,29 +113,16 @@ public function listBlogPostsAction(Location $location, Request $request)
133113
* Viewcontroller's viewLocation method. To be able to do that, we need to implement it's
134114
* full signature.
135115
*
136-
* @param \eZ\Publish\API\Repository\Values\Content\Location $location of the blog post
137-
* @param $viewType
138-
* @param bool $layout
139-
* @param array $params
140-
* @return \Symfony\Component\HttpFoundation\Response
116+
* @param ContentView $view
117+
*
118+
* @return View
141119
*/
142-
public function showBlogPostAction(Location $location, $viewType, $layout = false, array $params = array())
120+
public function showBlogPostAction(ContentView $view)
143121
{
144-
// We need the author, whatever the view type is.
145-
$repository = $this->getRepository();
146-
$author = $repository->getUserService()->loadUser($location->getContentInfo()->ownerId);
147-
148-
// TODO once the keyword service is available, load the number of keyword for each keyword
149-
150-
// Delegate view rendering to the original ViewController
151-
// (makes it possible to continue using defined template rules)
152-
// We just add "author" to the list of variables exposed to the final template
153-
return $this->get('ez_content')->viewLocation(
154-
$location->id,
155-
$viewType,
156-
$layout,
157-
array('author' => $author) + $params
158-
);
122+
$author = $this->getRepository()->getUserService()->loadUser($view->getContent()->contentInfo->ownerId);
123+
$view->addParameters(['author' => $author]);
124+
125+
return $view;
159126
}
160127

161128
/**

Controller/FeedbackFormController.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,24 @@
99
namespace EzSystems\DemoBundle\Controller;
1010

1111
use eZ\Bundle\EzPublishCoreBundle\Controller;
12-
use eZ\Publish\API\Repository\Values\Content\Location;
12+
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
1313
use EzSystems\DemoBundle\Entity\Feedback;
1414
use EzSystems\DemoBundle\Helper\EmailHelper;
1515
use Symfony\Component\HttpFoundation\Request;
1616

1717
class FeedbackFormController extends Controller
1818
{
1919
/**
20-
* Displays and manages the feedback form.
20+
* Displays the feedback form, and processes posted data.
21+
* The signature of this method follows the one from the default view controller, and adds the Request, since
22+
* we use to handle form data.
2123
*
22-
* The signature of this method follows the one from the default view controller.
23-
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
24-
* @param $viewType
25-
* @param bool $layout
26-
* @param array $params
24+
* @param \Symfony\Component\HttpFoundation\Request $request
25+
* @param \eZ\Publish\Core\MVC\Symfony\View\ContentView $view
2726
*
28-
* @return mixed
27+
* @return View
2928
*/
30-
public function showFeedbackFormAction(Request $request, Location $location, $viewType, $layout = false, array $params = array())
29+
public function showFeedbackFormAction(Request $request, ContentView $view)
3130
{
3231
// Creating a form using Symfony's form component
3332
$feedback = new Feedback();
@@ -51,15 +50,12 @@ public function showFeedbackFormAction(Request $request, Location $location, $vi
5150
$this->get('translator')->trans('Thank you for your message, we will get back to you as soon as possible.')
5251
);
5352

54-
return $this->redirect($this->generateUrl($location));
53+
return $this->redirect($this->generateUrl($view->getLocation()));
5554
}
5655
}
5756

58-
return $this->get('ez_content')->viewLocation(
59-
$location->id,
60-
$viewType,
61-
$layout,
62-
array('form' => $form->createView()) + $params
63-
);
57+
$view->addParameters(['form' => $form->createView()]);
58+
59+
return $view;
6460
}
6561
}

Controller/FolderController.php

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,27 @@
1111
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
1212
use eZ\Publish\API\Repository\Values\Content\Query;
1313
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
14-
use eZ\Publish\API\Repository\Values\Content\Location;
14+
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
1515
use eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchAdapter;
1616
use eZ\Bundle\EzPublishCoreBundle\Controller;
1717
use Pagerfanta\Pagerfanta;
18-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1918
use Symfony\Component\HttpFoundation\Request;
2019

2120
class FolderController extends Controller
2221
{
2322
/**
2423
* Displays the sub folder if it exists.
2524
*
26-
* @param \eZ\Publish\API\Repository\Values\Content\Location $location of a folder
27-
* @throws NotFoundHttpException $location is flagged as invisible
28-
* @return \Symfony\Component\HttpFoundation\Response
25+
* @param \eZ\Publish\Core\MVC\Symfony\View\ContentView $view
26+
*
27+
* @return \Symfony\Component\HttpFoundation\Response $location is flagged as invisible
2928
*/
30-
public function showFolderListAsideViewAction(Location $location)
29+
public function showFolderListAsideViewAction(ContentView $view)
3130
{
32-
if ($location->invisible) {
33-
throw new NotFoundHttpException("Location #$location->id cannot be displayed as it is flagged as invisible.");
34-
}
35-
36-
$languages = $this->getConfigResolver()->getParameter('languages');
37-
38-
$includedContentTypeIdentifiers = $this->container->getParameter('ezdemo.folder.folder_tree.included_content_types');
39-
4031
$subContentCriteria = $this->get('ezdemo.criteria_helper')->generateSubContentCriterion(
41-
$location, $includedContentTypeIdentifiers, $languages
32+
$view->getLocation(),
33+
$this->container->getParameter('ezdemo.folder.folder_tree.included_content_types'),
34+
$this->getConfigResolver()->getParameter('languages')
4235
);
4336

4437
$subContentQuery = new LocationQuery();
@@ -56,37 +49,29 @@ public function showFolderListAsideViewAction(Location $location)
5649
$treeChildItems[] = $hit->valueObject;
5750
}
5851

59-
return $this->get('ez_content')->viewLocation(
60-
$location->id,
61-
'aside_sub',
62-
true,
63-
['treeChildItems' => $treeChildItems]
64-
);
52+
$view->addParameters(['treeChildItems' => $treeChildItems]);
53+
54+
return $view;
6555
}
6656

6757
/**
6858
* Displays the list of article.
6959
*
70-
* @param \eZ\Publish\API\Repository\Values\Content\Location $location of a folder
7160
* @param \Symfony\Component\HttpFoundation\Request $request request object
72-
* @throws NotFoundHttpException $location is flagged as invisible
73-
* @return \Symfony\Component\HttpFoundation\Response
61+
* @param \eZ\Publish\Core\MVC\Symfony\View\ContentView $view
62+
*
63+
* @return \Symfony\Component\HttpFoundation\Response $location is flagged as invisible
7464
*/
75-
public function showFolderListAction(Request $request, Location $location)
65+
public function showFolderListAction(Request $request, ContentView $view)
7666
{
77-
if ($location->invisible) {
78-
throw new NotFoundHttpException("Location #$location->id cannot be displayed as it is flagged as invisible.");
79-
}
80-
81-
// Getting language for the current siteaccess
8267
$languages = $this->getConfigResolver()->getParameter('languages');
8368

84-
$excludedContentTypes = $this->container->getParameter('ezdemo.folder.folder_view.excluded_content_types');
85-
8669
// Using the criteria helper (a demobundle custom service) to generate our query's criteria.
8770
// This is a good practice in order to have less code in your controller.
8871
$criteria = $this->get('ezdemo.criteria_helper')->generateListFolderCriterion(
89-
$location, $excludedContentTypes, $languages
72+
$view->getLocation(),
73+
$this->container->getParameter('ezdemo.folder.folder_view.excluded_content_types'),
74+
$languages
9075
);
9176

9277
// Generating query
@@ -108,7 +93,7 @@ public function showFolderListAction(Request $request, Location $location)
10893

10994
// Get sub folder structure
11095
$subContentCriteria = $this->get('ezdemo.criteria_helper')->generateSubContentCriterion(
111-
$location, $includedContentTypeIdentifiers, $languages
96+
$view->getLocation(), $includedContentTypeIdentifiers, $languages
11297
);
11398

11499
$subContentQuery = new LocationQuery();
@@ -125,11 +110,8 @@ public function showFolderListAction(Request $request, Location $location)
125110
$treeItems[] = $hit->valueObject;
126111
}
127112

128-
return $this->get('ez_content')->viewLocation(
129-
$location->id,
130-
'full',
131-
true,
132-
['pagerFolder' => $pager, 'treeItems' => $treeItems]
133-
);
113+
$view->addParameters(['pagerFolder' => $pager, 'treeItems' => $treeItems]);
114+
115+
return $view;
134116
}
135117
}

PremiumContent/PremiumLocationViewProvider.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99

1010
use eZ\Publish\API\Repository\Repository;
1111
use eZ\Publish\API\Repository\Values\Content\Location;
12+
use eZ\Publish\Core\MVC\Symfony\View\ContentValueView;
1213
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
13-
use eZ\Publish\Core\MVC\Symfony\View\Provider\Location as LocationViewProvider;
14+
use eZ\Publish\Core\MVC\Symfony\View\View;
15+
use eZ\Publish\Core\MVC\Symfony\View\ViewProvider;
1416

1517
/**
1618
* Returns the premium_content view if it applies to a location and if the user isn't a Premium subscriber.
1719
*/
18-
class PremiumLocationViewProvider implements LocationViewProvider
20+
class PremiumLocationViewProvider implements ViewProvider
1921
{
2022
/**
2123
* ID of the section used to mark content as Premium.
@@ -38,13 +40,19 @@ public function __construct(Repository $repository, PremiumSubscriptionChecker $
3840
$this->subscriptionChecker = $subscriptionChecker;
3941
}
4042

41-
public function getView(Location $location, $viewType)
43+
public function getView(View $view)
4244
{
45+
$viewType = $view->getViewType();
46+
4347
if ($viewType !== 'full') {
4448
return null;
4549
}
4650

47-
if ($location->getContentInfo()->sectionId !== $this->premiumSectionId) {
51+
if (!$view instanceof ContentValueView) {
52+
return null;
53+
}
54+
55+
if ($view->getContent()->contentInfo->sectionId !== $this->premiumSectionId) {
4856
return null;
4957
}
5058

0 commit comments

Comments
 (0)