@@ -49,18 +49,30 @@ public function __construct($prefix, array $locales)
49
49
}
50
50
51
51
/**
52
+ * The repository path prefix where routes handled by this listener are located.
53
+ *
52
54
* @param $prefix
53
55
*/
54
56
public function setPrefix ($ prefix )
55
57
{
56
58
$ this ->idPrefix = $ prefix ;
57
59
}
58
60
61
+ /**
62
+ * Specify the list of allowed locales.
63
+ *
64
+ * @param array $locales
65
+ */
59
66
public function setLocales (array $ locales )
60
67
{
61
68
$ this ->locales = $ locales ;
62
69
}
63
70
71
+ /**
72
+ * Update locale after loading a route.
73
+ *
74
+ * @param LifecycleEventArgs $args
75
+ */
64
76
public function postLoad (LifecycleEventArgs $ args )
65
77
{
66
78
$ doc = $ args ->getObject ();
@@ -70,6 +82,11 @@ public function postLoad(LifecycleEventArgs $args)
70
82
$ this ->updateLocale ($ doc , $ doc ->getId ());
71
83
}
72
84
85
+ /**
86
+ * Update locale after persisting a route.
87
+ *
88
+ * @param LifecycleEventArgs $args
89
+ */
73
90
public function postPersist (LifecycleEventArgs $ args )
74
91
{
75
92
$ doc = $ args ->getObject ();
@@ -79,16 +96,31 @@ public function postPersist(LifecycleEventArgs $args)
79
96
$ this ->updateLocale ($ doc , $ doc ->getId ());
80
97
}
81
98
99
+ /**
100
+ * Update a locale after the route has been moved.
101
+ *
102
+ * @param MoveEventArgs $args
103
+ */
82
104
public function postMove (MoveEventArgs $ args )
83
105
{
84
106
$ doc = $ args ->getObject ();
85
107
if (! $ doc instanceof Route) {
86
108
return ;
87
109
}
88
- $ this ->updateLocale ($ doc , $ args ->getTargetPath ());
110
+ $ this ->updateLocale ($ doc , $ args ->getTargetPath (), true );
89
111
}
90
112
91
- protected function updateLocale (Route $ doc , $ id )
113
+ /**
114
+ * Update the locale of a route if $id starts with the prefix and has a
115
+ * valid locale right after.
116
+ *
117
+ * @param Route $doc The route object
118
+ * @param string $id The id (in move case, this is not the current id
119
+ * of $route)
120
+ * @param boolean $force Whether to update the locale even if the route
121
+ * already has a locale.
122
+ */
123
+ protected function updateLocale (Route $ doc , $ id , $ force = false )
92
124
{
93
125
$ matches = array ();
94
126
@@ -98,12 +130,13 @@ protected function updateLocale(Route $doc, $id)
98
130
return ;
99
131
}
100
132
101
- if (in_array ($ matches [1 ], $ this ->locales )
102
- && ! $ doc ->getDefault ('_locale ' )
103
- && ! $ doc ->getRequirement ('_locale ' )
104
- ) {
105
- $ doc ->setDefault ('_locale ' , $ matches [1 ]);
106
- $ doc ->setRequirement ('_locale ' , $ matches [1 ]);
133
+ if (in_array ($ matches [1 ], $ this ->locales )) {
134
+ if ($ force || ! $ doc ->getDefault ('_locale ' )) {
135
+ $ doc ->setDefault ('_locale ' , $ matches [1 ]);
136
+ }
137
+ if ($ force || ! $ doc ->getRequirement ('_locale ' )) {
138
+ $ doc ->setRequirement ('_locale ' , $ matches [1 ]);
139
+ }
107
140
}
108
141
}
109
142
}
0 commit comments