Skip to content

Commit bae5b55

Browse files
committed
make sure we update the locale after move, check default and requirement separately
1 parent a4e3712 commit bae5b55

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

Doctrine/Phpcr/LocaleListener.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,30 @@ public function __construct($prefix, array $locales)
4949
}
5050

5151
/**
52+
* The repository path prefix where routes handled by this listener are located.
53+
*
5254
* @param $prefix
5355
*/
5456
public function setPrefix($prefix)
5557
{
5658
$this->idPrefix = $prefix;
5759
}
5860

61+
/**
62+
* Specify the list of allowed locales.
63+
*
64+
* @param array $locales
65+
*/
5966
public function setLocales(array $locales)
6067
{
6168
$this->locales = $locales;
6269
}
6370

71+
/**
72+
* Update locale after loading a route.
73+
*
74+
* @param LifecycleEventArgs $args
75+
*/
6476
public function postLoad(LifecycleEventArgs $args)
6577
{
6678
$doc = $args->getObject();
@@ -70,6 +82,11 @@ public function postLoad(LifecycleEventArgs $args)
7082
$this->updateLocale($doc, $doc->getId());
7183
}
7284

85+
/**
86+
* Update locale after persisting a route.
87+
*
88+
* @param LifecycleEventArgs $args
89+
*/
7390
public function postPersist(LifecycleEventArgs $args)
7491
{
7592
$doc = $args->getObject();
@@ -79,16 +96,31 @@ public function postPersist(LifecycleEventArgs $args)
7996
$this->updateLocale($doc, $doc->getId());
8097
}
8198

99+
/**
100+
* Update a locale after the route has been moved.
101+
*
102+
* @param MoveEventArgs $args
103+
*/
82104
public function postMove(MoveEventArgs $args)
83105
{
84106
$doc = $args->getObject();
85107
if (! $doc instanceof Route) {
86108
return;
87109
}
88-
$this->updateLocale($doc, $args->getTargetPath());
110+
$this->updateLocale($doc, $args->getTargetPath(), true);
89111
}
90112

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)
92124
{
93125
$matches = array();
94126

@@ -98,12 +130,13 @@ protected function updateLocale(Route $doc, $id)
98130
return;
99131
}
100132

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+
}
107140
}
108141
}
109142
}

0 commit comments

Comments
 (0)