Skip to content

Commit c46ec7a

Browse files
committed
validate we have the right type of object manager in the fixtures
1 parent ac7587d commit c46ec7a

File tree

5 files changed

+95
-69
lines changed

5 files changed

+95
-69
lines changed

src/Sandbox/MainBundle/DataFixtures/PHPCR/LoadMenuData.php

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,62 +23,67 @@ public function getOrder()
2323
}
2424

2525
/**
26-
* @param \Doctrine\ODM\PHPCR\DocumentManager $dm
26+
* @param \Doctrine\ODM\PHPCR\DocumentManager $manager
2727
*/
28-
public function load(ObjectManager $dm)
28+
public function load(ObjectManager $manager)
2929
{
30-
$session = $dm->getPhpcrSession();
30+
if (!$manager instanceof DocumentManager) {
31+
$class = get_class($manager);
32+
throw new \RuntimeException("Fixture requires a PHPCR ODM DocumentManager instance, instance of '$class' given.");
33+
}
34+
35+
$session = $manager->getPhpcrSession();
3136

3237
$basepath = $this->container->getParameter('cmf_menu.persistence.phpcr.menu_basepath');
3338
$content_path = $this->container->getParameter('cmf_content.persistence.phpcr.content_basepath');
3439

3540
NodeHelper::createPath($session, $basepath);
36-
$root = $dm->find(null, $basepath);
41+
$root = $manager->find(null, $basepath);
3742

3843
$labels = array('en' => 'Home', 'de' => 'Start', 'fr' => 'Accueil');
3944
/** @var $main Menu */
40-
$main = $this->createMenuNode($dm, $root, 'main', $labels, $dm->find(null, "$content_path/home"));
45+
$main = $this->createMenuNode($manager, $root, 'main', $labels, $manager->find(null, "$content_path/home"));
4146
$main->setChildrenAttributes(array("class" => "menu_main"));
4247

4348
if ($session->getRepository()->getDescriptor(RepositoryInterface::QUERY_FULL_TEXT_SEARCH_SUPPORTED)) {
44-
$this->createMenuNode($dm, $main, 'search-item', 'Search', null, null, 'liip_search');
49+
$this->createMenuNode($manager, $main, 'search-item', 'Search', null, null, 'liip_search');
4550
}
4651

47-
$this->createMenuNode($dm, $main, 'admin-item', 'Admin', null, null, 'sonata_admin_dashboard');
52+
$this->createMenuNode($manager, $main, 'admin-item', 'Admin', null, null, 'sonata_admin_dashboard');
4853

49-
$projects = $this->createMenuNode($dm, $main, 'projects-item', array('en' => 'Projects', 'de' => 'Projekte', 'fr' => 'Projets'), $dm->find(null, "$content_path/projects"));
50-
$this->createMenuNode($dm, $projects, 'cmf-item', 'Symfony CMF', $dm->find(null, "$content_path/cmf"));
54+
$projects = $this->createMenuNode($manager, $main, 'projects-item', array('en' => 'Projects', 'de' => 'Projekte', 'fr' => 'Projets'), $manager->find(null, "$content_path/projects"));
55+
$this->createMenuNode($manager, $projects, 'cmf-item', 'Symfony CMF', $manager->find(null, "$content_path/cmf"));
5156

52-
$company = $this->createMenuNode($dm, $main, 'company-item', array('en' => 'Company', 'de' => 'Firma', 'fr' => 'Entreprise'), $dm->find(null, "$content_path/company"));
53-
$this->createMenuNode($dm, $company, 'team-item', array('en' => 'Team', 'de' => 'Team', 'fr' => 'Equipe'), $dm->find(null, "$content_path/team"));
54-
$this->createMenuNode($dm, $company, 'more-item', array('en' => 'More', 'de' => 'Mehr', 'fr' => 'Plus'), $dm->find(null, "$content_path/more"));
57+
$company = $this->createMenuNode($manager, $main, 'company-item', array('en' => 'Company', 'de' => 'Firma', 'fr' => 'Entreprise'), $manager->find(null, "$content_path/company"));
58+
$this->createMenuNode($manager, $company, 'team-item', array('en' => 'Team', 'de' => 'Team', 'fr' => 'Equipe'), $manager->find(null, "$content_path/team"));
59+
$this->createMenuNode($manager, $company, 'more-item', array('en' => 'More', 'de' => 'Mehr', 'fr' => 'Plus'), $manager->find(null, "$content_path/more"));
5560

56-
$demo = $this->createMenuNode($dm, $main, 'demo-item', 'Demo', $dm->find(null, "$content_path/demo"));
61+
$demo = $this->createMenuNode($manager, $main, 'demo-item', 'Demo', $manager->find(null, "$content_path/demo"));
5762
//TODO: this should be possible without a content as the controller might not need a content. support directly having the route document as "content" in the menu document?
58-
$this->createMenuNode($dm, $demo, 'controller-item', 'Explicit controller', $dm->find(null, "$content_path/demo_controller"));
59-
$this->createMenuNode($dm, $demo, 'template-item', 'Explicit template', $dm->find(null, "$content_path/demo_template"));
60-
$this->createMenuNode($dm, $demo, 'type-item', 'Route type to controller', $dm->find(null, "$content_path/demo_type"));
61-
$this->createMenuNode($dm, $demo, 'class-item', 'Class to controller', $dm->find(null, "$content_path/demo_class"));
62-
$this->createMenuNode($dm, $demo, 'test-item', 'Normal Symfony Route', null, null, 'test');
63-
$this->createMenuNode($dm, $demo, 'external-item', 'External Link', null, 'http://cmf.symfony.com/');
63+
$this->createMenuNode($manager, $demo, 'controller-item', 'Explicit controller', $manager->find(null, "$content_path/demo_controller"));
64+
$this->createMenuNode($manager, $demo, 'template-item', 'Explicit template', $manager->find(null, "$content_path/demo_template"));
65+
$this->createMenuNode($manager, $demo, 'type-item', 'Route type to controller', $manager->find(null, "$content_path/demo_type"));
66+
$this->createMenuNode($manager, $demo, 'class-item', 'Class to controller', $manager->find(null, "$content_path/demo_class"));
67+
$this->createMenuNode($manager, $demo, 'test-item', 'Normal Symfony Route', null, null, 'test');
68+
$this->createMenuNode($manager, $demo, 'external-item', 'External Link', null, 'http://cmf.symfony.com/');
6469

65-
$singlelocale = $this->createMenuNode($dm, $main, 'singlelocale-item', array('en' => 'singlelocale'), $dm->find(null, "$content_path/singlelocale"));
66-
$this->createMenuNode($dm, $singlelocale, 'singlelocale-sub-item', array('en' => 'singlelocale child'), $dm->find(null, "$content_path/singlelocale"));
70+
$singlelocale = $this->createMenuNode($manager, $main, 'singlelocale-item', array('en' => 'singlelocale'), $manager->find(null, "$content_path/singlelocale"));
71+
$this->createMenuNode($manager, $singlelocale, 'singlelocale-sub-item', array('en' => 'singlelocale child'), $manager->find(null, "$content_path/singlelocale"));
6772

68-
$seo = $this->createMenuNode($dm, $main, 'seo', 'SEO', $dm->find(null, "$content_path/simple-seo-example"));
69-
$this->createMenuNode($dm, $seo, 'simple-seo-example', array('en' => 'Seo-Simple-Content'), $dm->find(null, "$content_path/simple-seo-example"));
70-
$this->createMenuNode($dm, $seo, 'demo-seo-extractor', array('en' => 'Seo-Extractor'), $dm->find(null, "$content_path/demo-seo-extractor"));
71-
$this->createMenuNode($dm, $seo, 'simple-seo-property', array('en' => 'Seo-Extra-Properties'), $dm->find(null, "$content_path/simple-seo-property"));
73+
$seo = $this->createMenuNode($manager, $main, 'seo', 'SEO', $manager->find(null, "$content_path/simple-seo-example"));
74+
$this->createMenuNode($manager, $seo, 'simple-seo-example', array('en' => 'Seo-Simple-Content'), $manager->find(null, "$content_path/simple-seo-example"));
75+
$this->createMenuNode($manager, $seo, 'demo-seo-extractor', array('en' => 'Seo-Extractor'), $manager->find(null, "$content_path/demo-seo-extractor"));
76+
$this->createMenuNode($manager, $seo, 'simple-seo-property', array('en' => 'Seo-Extra-Properties'), $manager->find(null, "$content_path/simple-seo-property"));
7277

73-
$this->createMenuNode($dm, $main, 'routing-auto-item', array('en' => 'Auto routing example', 'de' => 'Auto routing beispiel', 'fr' => 'Auto routing exemple'), $dm->find(null, "$content_path/news/RoutingAutoBundle generates routes!"));
78+
$this->createMenuNode($manager, $main, 'routing-auto-item', array('en' => 'Auto routing example', 'de' => 'Auto routing beispiel', 'fr' => 'Auto routing exemple'), $manager->find(null, "$content_path/news/RoutingAutoBundle generates routes!"));
7479

75-
$dm->flush();
80+
$manager->flush();
7681
}
7782

7883
/**
7984
* @return MenuNode a Navigation instance with the specified information
8085
*/
81-
protected function createMenuNode(DocumentManager $dm, $parent, $name, $label, $content, $uri = null, $route = null)
86+
protected function createMenuNode(DocumentManager $manager, $parent, $name, $label, $content, $uri = null, $route = null)
8287
{
8388
if (!$parent instanceof MenuNode && !$parent instanceof Menu) {
8489
$menuNode = new Menu();
@@ -89,7 +94,7 @@ protected function createMenuNode(DocumentManager $dm, $parent, $name, $label, $
8994
$menuNode->setParentDocument($parent);
9095
$menuNode->setName($name);
9196

92-
$dm->persist($menuNode); // do persist before binding translation
97+
$manager->persist($menuNode); // do persist before binding translation
9398

9499
if (null !== $content) {
95100
$menuNode->setContent($content);
@@ -102,7 +107,7 @@ protected function createMenuNode(DocumentManager $dm, $parent, $name, $label, $
102107
if (is_array($label)) {
103108
foreach ($label as $locale => $l) {
104109
$menuNode->setLabel($l);
105-
$dm->bindTranslation($menuNode, $locale);
110+
$manager->bindTranslation($menuNode, $locale);
106111
}
107112
} else {
108113
$menuNode->setLabel($label);

src/Sandbox/MainBundle/DataFixtures/PHPCR/LoadNewsData.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public function getOrder()
2222

2323
public function load(ObjectManager $manager)
2424
{
25+
if (!$manager instanceof DocumentManager) {
26+
$class = get_class($manager);
27+
throw new \RuntimeException("Fixture requires a PHPCR ODM DocumentManager instance, instance of '$class' given.");
28+
}
29+
2530
$basePath = $this->container->getParameter('cmf_content.persistence.phpcr.content_basepath');
2631
$newsPath = $basePath . '/news';
2732
NodeHelper::createPath($manager->getPhpcrSession(), $newsPath);

src/Sandbox/MainBundle/DataFixtures/PHPCR/LoadRoutingData.php

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
77
use Doctrine\Common\Persistence\ObjectManager;
88

9+
use Doctrine\ODM\PHPCR\DocumentManager;
910
use PHPCR\Util\NodeHelper;
1011

1112
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RedirectRoute;
@@ -28,68 +29,73 @@ public function getOrder()
2829
* consistent in what you use and only use different things for special
2930
* cases.
3031
*
31-
* @param $dm \Doctrine\ODM\PHPCR\DocumentManager
32+
* @param $manager \Doctrine\ODM\PHPCR\DocumentManager
3233
*/
33-
public function load(ObjectManager $dm)
34+
public function load(ObjectManager $manager)
3435
{
35-
$session = $dm->getPhpcrSession();
36+
if (!$manager instanceof DocumentManager) {
37+
$class = get_class($manager);
38+
throw new \RuntimeException("Fixture requires a PHPCR ODM DocumentManager instance, instance of '$class' given.");
39+
}
40+
41+
$session = $manager->getPhpcrSession();
3642

3743
$basepath = $this->container->getParameter('cmf_routing.dynamic.persistence.phpcr.admin_basepath');
3844
if ($session->itemExists($basepath)) {
3945
$session->removeItem($basepath);
4046
}
4147

4248
NodeHelper::createPath($session, $basepath);
43-
$parent = $dm->find(null, $basepath);
49+
$parent = $manager->find(null, $basepath);
4450

4551
$content_path = $this->container->getParameter('cmf_content.persistence.phpcr.content_basepath');
4652
$locales = $this->container->getParameter('locales');
4753
foreach ($locales as $locale) {
4854
$home = new Route();
4955
$home->setPosition($parent, $locale);
5056
$home->setDefault(RouteObjectInterface::TEMPLATE_NAME, 'SandboxMainBundle:Homepage:index.html.twig');
51-
$home->setContent($dm->find(null, "$content_path/home"));
52-
$dm->persist($home);
57+
$home->setContent($manager->find(null, "$content_path/home"));
58+
$manager->persist($home);
5359

5460
$company = new Route;
5561
$company->setPosition($home, 'company');
56-
$company->setContent($dm->find(null, "$content_path/company"));
57-
$dm->persist($company);
62+
$company->setContent($manager->find(null, "$content_path/company"));
63+
$manager->persist($company);
5864

5965
$team = new Route;
6066
$team->setPosition($company, 'team');
61-
$team->setContent($dm->find(null, "$content_path/team"));
62-
$dm->persist($team);
67+
$team->setContent($manager->find(null, "$content_path/team"));
68+
$manager->persist($team);
6369

6470
$more = new Route;
6571
$more->setPosition($company, 'more');
66-
$more->setContent($dm->find(null, "$content_path/more"));
67-
$dm->persist($more);
72+
$more->setContent($manager->find(null, "$content_path/more"));
73+
$manager->persist($more);
6874

6975
$projects = new Route;
7076
$projects->setPosition($home, 'projects');
71-
$projects->setContent($dm->find(null, "$content_path/projects"));
72-
$dm->persist($projects);
77+
$projects->setContent($manager->find(null, "$content_path/projects"));
78+
$manager->persist($projects);
7379

7480
$cmf = new Route;
7581
$cmf->setPosition($projects, 'cmf');
76-
$cmf->setContent($dm->find(null, "$content_path/cmf"));
77-
$dm->persist($cmf);
82+
$cmf->setContent($manager->find(null, "$content_path/cmf"));
83+
$manager->persist($cmf);
7884

7985
$seo = new Route();
8086
$seo->setPosition($home, 'simple-seo-example');
81-
$seo->setContent($dm->find(null, "$content_path/simple-seo-example"));
82-
$dm->persist($seo);
87+
$seo->setContent($manager->find(null, "$content_path/simple-seo-example"));
88+
$manager->persist($seo);
8389

8490
$seo = new Route();
8591
$seo->setPosition($home, 'simple-seo-property');
86-
$seo->setContent($dm->find(null, "$content_path/simple-seo-property"));
87-
$dm->persist($seo);
92+
$seo->setContent($manager->find(null, "$content_path/simple-seo-property"));
93+
$manager->persist($seo);
8894

8995
$seo = new Route();
9096
$seo->setPosition($home, 'demo-seo-extractor');
91-
$seo->setContent($dm->find(null, "$content_path/demo-seo-extractor"));
92-
$dm->persist($seo);
97+
$seo->setContent($manager->find(null, "$content_path/demo-seo-extractor"));
98+
$manager->persist($seo);
9399
}
94100

95101
// demo features of routing
@@ -98,67 +104,67 @@ public function load(ObjectManager $dm)
98104

99105
$demo = new Route;
100106
$demo->setPosition($parent, 'demo');
101-
$demo->setContent($dm->find(null, "$content_path/demo"));
107+
$demo->setContent($manager->find(null, "$content_path/demo"));
102108
$demo->setDefault(RouteObjectInterface::TEMPLATE_NAME, 'SandboxMainBundle:Demo:template_explicit.html.twig');
103-
$dm->persist($demo);
109+
$manager->persist($demo);
104110

105111
// explicit template
106112
$template = new Route;
107113
$template->setPosition($demo, 'atemplate');
108-
$template->setContent($dm->find(null, "$content_path/demo_template"));
114+
$template->setContent($manager->find(null, "$content_path/demo_template"));
109115
$template->setDefault(RouteObjectInterface::TEMPLATE_NAME, 'SandboxMainBundle:Demo:template_explicit.html.twig');
110-
$dm->persist($template);
116+
$manager->persist($template);
111117

112118
// explicit controller
113119
$controller = new Route;
114120
$controller->setPosition($demo, 'controller');
115-
$controller->setContent($dm->find(null, "$content_path/demo_controller"));
121+
$controller->setContent($manager->find(null, "$content_path/demo_controller"));
116122
$controller->setDefault('_controller', 'sandbox_main.controller:specialAction');
117-
$dm->persist($controller);
123+
$manager->persist($controller);
118124

119125
// type to controller mapping
120126
$type = new Route;
121127
$type->setPosition($demo, 'type');
122-
$type->setContent($dm->find(null, "$content_path/demo_type"));
128+
$type->setContent($manager->find(null, "$content_path/demo_type"));
123129
$type->setDefault('type', 'demo_type');
124-
$dm->persist($type);
130+
$manager->persist($type);
125131

126132
// class to controller mapping
127133
$class = new Route;
128134
$class->setPosition($demo, 'class');
129-
$class->setContent($dm->find(null, "$content_path/demo_class"));
130-
$dm->persist($class);
135+
$class->setContent($manager->find(null, "$content_path/demo_class"));
136+
$manager->persist($class);
131137

132138
// redirections
133139

134140
// redirect to uri
135141
$redirect = new RedirectRoute();
136142
$redirect->setPosition($parent, 'external');
137143
$redirect->setUri('http://cmf.symfony.com');
138-
$dm->persist($redirect);
144+
$manager->persist($redirect);
139145

140146
// redirect to other doctrine route
141147
$redirectRoute = new RedirectRoute();
142148
$redirectRoute->setPosition($parent, 'short');
143149
$redirectRoute->setRouteTarget($cmf);
144-
$dm->persist($redirectRoute);
150+
$manager->persist($redirectRoute);
145151

146152
// redirect to Symfony route
147153
$redirectS = new RedirectRoute();
148154
$redirectS->setPosition($parent, 'short1');
149155
$redirectS->setRouteName('test');
150-
$dm->persist($redirectS);
156+
$manager->persist($redirectS);
151157

152158
// class to template mapping is used for all the rest
153159

154160
$default_locale = $this->container->getParameter('locale');
155161
$singlelocale = new Route;
156-
$singlelocale->setPosition($dm->find(null, "$basepath/$default_locale"), 'singlelocale');
162+
$singlelocale->setPosition($manager->find(null, "$basepath/$default_locale"), 'singlelocale');
157163
$singlelocale->setDefault('_locale', $default_locale);
158164
$singlelocale->setRequirement('_locale', $default_locale);
159-
$singlelocale->setContent($dm->find(null, "$content_path/singlelocale"));
160-
$dm->persist($singlelocale);
165+
$singlelocale->setContent($manager->find(null, "$content_path/singlelocale"));
166+
$manager->persist($singlelocale);
161167

162-
$dm->flush();
168+
$manager->flush();
163169
}
164170
}

src/Sandbox/MainBundle/DataFixtures/PHPCR/LoadSimpleCmsData.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public function getOrder()
2020

2121
public function load(ObjectManager $manager)
2222
{
23+
if (!$manager instanceof DocumentManager) {
24+
$class = get_class($manager);
25+
throw new \RuntimeException("Fixture requires a PHPCR ODM DocumentManager instance, instance of '$class' given.");
26+
}
27+
2328
$basepath = $this->container->getParameter('cmf_simple_cms.persistence.phpcr.basepath');
2429
$root = $manager->find(null, $basepath);
2530
$root->setTitle('simple cms root (hidden by the home route in the sandbox)');

src/Sandbox/MainBundle/DataFixtures/PHPCR/LoadStaticPageData.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public function getOrder()
2626
*/
2727
public function load(ObjectManager $manager)
2828
{
29+
if (!$manager instanceof DocumentManager) {
30+
$class = get_class($manager);
31+
throw new \RuntimeException("Fixture requires a PHPCR ODM DocumentManager instance, instance of '$class' given.");
32+
}
33+
2934
$session = $manager->getPhpcrSession();
3035

3136
$basepath = $this->container->getParameter('cmf_media.persistence.phpcr.media_basepath');

0 commit comments

Comments
 (0)