1
+ <?php
2
+
3
+ namespace Symfony \Cmf \Bundle \SimpleCmsBundle \Migrator ;
4
+
5
+ use Symfony \Component \Yaml \Parser ;
6
+ use Symfony \Component \Console \Output \OutputInterface ;
7
+ use Symfony \Cmf \Component \Routing \RouteObjectInterface ;
8
+
9
+ use PHPCR \Util \NodeHelper ;
10
+ use PHPCR \SessionInterface ;
11
+
12
+ use Doctrine \Bundle \PHPCRBundle \ManagerRegistry ;
13
+ use Doctrine \Bundle \PHPCRBundle \Migrator \MigratorInterface ;
14
+ use Doctrine \ODM \PHPCR \DocumentManager ;
15
+
16
+ class Page implements MigratorInterface
17
+ {
18
+ /**
19
+ * @var PHPCR\SessionInterface
20
+ */
21
+ protected $ session ;
22
+
23
+ /*
24
+ * @var Symfony\Component\Console\Output\OutputInterface
25
+ */
26
+ protected $ output ;
27
+
28
+ /**
29
+ * @var \Doctrine\ODM\PHPCR\DocumentManager
30
+ */
31
+ protected $ dm ;
32
+
33
+ protected $ basepath ;
34
+
35
+ protected $ dataDir ;
36
+
37
+ public function __construct (ManagerRegistry $ registry , $ basepath , $ dataDir )
38
+ {
39
+ $ this ->dm = $ registry ->getManager ();
40
+ $ this ->session = $ registry ->getConnection ();
41
+ $ this ->basepath = $ basepath ;
42
+ $ this ->dataDir = $ dataDir ;
43
+ }
44
+
45
+ public function init (SessionInterface $ session , OutputInterface $ output )
46
+ {
47
+ $ this ->session = $ session ;
48
+ $ this ->output = $ output ;
49
+ }
50
+
51
+ protected function createPageInstance ($ className )
52
+ {
53
+ return new $ className (true );
54
+ }
55
+
56
+ public function migrate ($ path = '/ ' , $ depth = -1 )
57
+ {
58
+ if (0 !== strpos ($ path , $ this ->basepath )) {
59
+ throw new \RuntimeException ("The provided identifier ' $ path' does not start with the base path ' {$ this ->basepath }' " );
60
+ }
61
+
62
+ $ yaml = new Parser ();
63
+ $ contentPath = substr ($ path , strlen ($ this ->basepath ));
64
+ $ data = $ yaml ->parse (file_get_contents ($ this ->dataDir .$ contentPath .'.yml ' ));
65
+
66
+ NodeHelper::createPath ($ this ->session , preg_replace ('#/[^/]*$# ' , '' , $ this ->basepath ));
67
+
68
+ $ class = isset ($ data ['class ' ]) ? $ data ['class ' ] : 'Symfony \\Cmf \\Bundle \\SimpleCmsBundle \\Document \\MultilangPage ' ;
69
+
70
+ $ page = $ this ->dm ->find ($ class , $ path );
71
+ if (!$ page ) {
72
+ $ page = $ this ->createPageInstance ($ class );
73
+ $ page ->setId ($ path );
74
+ }
75
+
76
+ if (isset ($ data ['formats ' ])) {
77
+ $ page ->setDefault ('_format ' , reset ($ data ['formats ' ]));
78
+ $ page ->setRequirement ('_format ' , implode ('| ' , $ data ['formats ' ]));
79
+ }
80
+
81
+ if (!empty ($ data ['template ' ])) {
82
+ $ page ->setDefault (RouteObjectInterface::TEMPLATE_NAME , $ data ['template ' ]);
83
+ }
84
+
85
+ if (!empty ($ data ['controller ' ])) {
86
+ $ page ->setDefault (RouteObjectInterface::CONTROLLER_NAME , $ data ['controller ' ]);
87
+ }
88
+
89
+ if (!empty ($ data ['options ' ])) {
90
+ $ page ->setOptions ($ data ['options ' ]);
91
+ }
92
+
93
+ $ this ->dm ->persist ($ page );
94
+
95
+ if (is_array ($ data ['title ' ])) {
96
+ foreach ($ data ['title ' ] as $ locale => $ title ) {
97
+ $ page ->setTitle ($ title );
98
+ if (isset ($ data ['label ' ][$ locale ]) && $ data ['label ' ][$ locale ]) {
99
+ $ page ->setLabel ($ data ['label ' ][$ locale ]);
100
+ } elseif (!isset ($ data ['label ' ][$ locale ])) {
101
+ $ page ->setLabel ($ title );
102
+ }
103
+ $ page ->setBody ($ data ['body ' ][$ locale ]);
104
+ $ this ->dm ->bindTranslation ($ page , $ locale );
105
+ }
106
+ } else {
107
+ $ page ->setTitle ($ data ['title ' ]);
108
+ if (isset ($ data ['label ' ])) {
109
+ if ($ data ['label ' ]) {
110
+ $ page ->setLabel ($ data ['label ' ]);
111
+ }
112
+ } elseif (!isset ($ data ['label ' ])) {
113
+ $ page ->setLabel ($ data ['title ' ]);
114
+ }
115
+ $ page ->setBody ($ data ['body ' ]);
116
+ }
117
+
118
+ if (isset ($ data ['create_date ' ])) {
119
+ $ page ->setCreateDate (date_create_from_format ('U ' , strtotime ($ data ['create_date ' ])));
120
+ }
121
+
122
+ if (isset ($ data ['publish_start_date ' ])) {
123
+ $ page ->setPublishStartDate (date_create_from_format ('U ' , strtotime ($ data ['publish_start_date ' ])));
124
+ }
125
+
126
+ if (isset ($ data ['publish_end_date ' ])) {
127
+ $ page ->setPublishEndDate (date_create_from_format ('U ' , strtotime ($ data ['publish_end_date ' ])));
128
+ }
129
+
130
+ $ this ->dm ->flush ();
131
+
132
+ return 0 ;
133
+ }
134
+ }
0 commit comments