Skip to content

Commit 16eeebc

Browse files
committed
minor symfony#14365 [3.0]Closures support $this in PHP >= 5.4 (dosten)
This PR was merged into the 3.0-dev branch. Discussion ---------- [3.0]Closures support $this in PHP >= 5.4 | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | License | MIT Closures support `$this` in PHP >= 5.4 ping @aitboudad Commits ------- 38f32c1 $this can be used inside a closure in PHP >= 5.4
2 parents 22c2345 + 38f32c1 commit 16eeebc

File tree

3 files changed

+23
-43
lines changed

3 files changed

+23
-43
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function setInputStream($stream)
7575
}
7676

7777
/**
78-
* Returns the helper's input stream
78+
* Returns the helper's input stream.
7979
*
8080
* @return resource
8181
*/
@@ -95,8 +95,6 @@ public function getName()
9595
/**
9696
* Asks the question to the user.
9797
*
98-
* This method is public for PHP 5.3 compatibility, it should be private.
99-
*
10098
* @param OutputInterface $output
10199
* @param Question $question
102100
*
@@ -105,7 +103,7 @@ public function getName()
105103
* @throws \Exception
106104
* @throws \RuntimeException
107105
*/
108-
public function doAsk(OutputInterface $output, Question $question)
106+
private function doAsk(OutputInterface $output, Question $question)
109107
{
110108
$this->writePrompt($output, $question);
111109

@@ -148,7 +146,7 @@ public function doAsk(OutputInterface $output, Question $question)
148146
* Outputs the question prompt.
149147
*
150148
* @param OutputInterface $output
151-
* @param Question $question
149+
* @param Question $question
152150
*/
153151
protected function writePrompt(OutputInterface $output, Question $question)
154152
{

src/Symfony/Component/Routing/Router.php

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -278,32 +278,27 @@ public function getMatcher()
278278
return $this->matcher;
279279
}
280280

281-
$class = $this->options['matcher_cache_class'];
282-
$baseClass = $this->options['matcher_base_class'];
283-
$expressionLanguageProviders = $this->expressionLanguageProviders;
284-
$that = $this; // required for PHP 5.3 where "$this" cannot be use()d in anonymous functions. Change in Symfony 3.0.
285-
286-
$cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$class.'.php',
287-
function (ConfigCacheInterface $cache) use ($that, $class, $baseClass, $expressionLanguageProviders) {
288-
$dumper = $that->getMatcherDumperInstance();
281+
$cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['matcher_cache_class'].'.php',
282+
function (ConfigCacheInterface $cache) {
283+
$dumper = $this->getMatcherDumperInstance();
289284
if (method_exists($dumper, 'addExpressionLanguageProvider')) {
290-
foreach ($expressionLanguageProviders as $provider) {
285+
foreach ($this->expressionLanguageProviders as $provider) {
291286
$dumper->addExpressionLanguageProvider($provider);
292287
}
293288
}
294289

295290
$options = array(
296-
'class' => $class,
297-
'base_class' => $baseClass,
291+
'class' => $this->options['matcher_cache_class'],
292+
'base_class' => $this->options['matcher_base_class'],
298293
);
299294

300-
$cache->write($dumper->dump($options), $that->getRouteCollection()->getResources());
295+
$cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
301296
}
302297
);
303298

304299
require_once $cache->getPath();
305300

306-
return $this->matcher = new $class($this->context);
301+
return $this->matcher = new $this->options['matcher_cache_class']($this->context);
307302
}
308303

309304
/**
@@ -320,25 +315,22 @@ public function getGenerator()
320315
if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
321316
$this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->logger);
322317
} else {
323-
$class = $this->options['generator_cache_class'];
324-
$baseClass = $this->options['generator_base_class'];
325-
$that = $this; // required for PHP 5.3 where "$this" cannot be use()d in anonymous functions. Change in Symfony 3.0.
326-
$cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$class.'.php',
327-
function (ConfigCacheInterface $cache) use ($that, $class, $baseClass) {
328-
$dumper = $that->getGeneratorDumperInstance();
318+
$cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['generator_cache_class'].'.php',
319+
function (ConfigCacheInterface $cache) {
320+
$dumper = $this->getGeneratorDumperInstance();
329321

330322
$options = array(
331-
'class' => $class,
332-
'base_class' => $baseClass,
323+
'class' => $this->options['generator_cache_class'],
324+
'base_class' => $this->options['generator_base_class'],
333325
);
334326

335-
$cache->write($dumper->dump($options), $that->getRouteCollection()->getResources());
327+
$cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
336328
}
337329
);
338330

339331
require_once $cache->getPath();
340332

341-
$this->generator = new $class($this->context, $this->logger);
333+
$this->generator = new $this->options['generator_cache_class']($this->context, $this->logger);
342334
}
343335

344336
if ($this->generator instanceof ConfigurableRequirementsInterface) {
@@ -354,21 +346,17 @@ public function addExpressionLanguageProvider(ExpressionFunctionProviderInterfac
354346
}
355347

356348
/**
357-
* This method is public because it needs to be callable from a closure in PHP 5.3. It should be converted back to protected in 3.0.
358-
* @internal
359349
* @return GeneratorDumperInterface
360350
*/
361-
public function getGeneratorDumperInstance()
351+
protected function getGeneratorDumperInstance()
362352
{
363353
return new $this->options['generator_dumper_class']($this->getRouteCollection());
364354
}
365355

366356
/**
367-
* This method is public because it needs to be callable from a closure in PHP 5.3. It should be converted back to protected in 3.0.
368-
* @internal
369357
* @return MatcherDumperInterface
370358
*/
371-
public function getMatcherDumperInstance()
359+
protected function getMatcherDumperInstance()
372360
{
373361
return new $this->options['matcher_dumper_class']($this->getRouteCollection());
374362
}

src/Symfony/Component/Translation/Translator.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,9 @@ private function initializeCacheCatalogue($locale)
327327
}
328328

329329
$this->assertValidLocale($locale);
330-
$self = $this; // required for PHP 5.3 where "$this" cannot be use()d in anonymous functions. Change in Symfony 3.0.
331330
$cache = $this->getConfigCacheFactory()->cache($this->getCatalogueCachePath($locale),
332-
function (ConfigCacheInterface $cache) use ($self, $locale) {
333-
$self->dumpCatalogue($locale, $cache);
331+
function (ConfigCacheInterface $cache) use ($locale) {
332+
$this->dumpCatalogue($locale, $cache);
334333
}
335334
);
336335

@@ -343,12 +342,7 @@ function (ConfigCacheInterface $cache) use ($self, $locale) {
343342
$this->catalogues[$locale] = include $cache->getPath();
344343
}
345344

346-
/**
347-
* This method is public because it needs to be callable from a closure in PHP 5.3. It should be made protected (or even private, if possible) in 3.0.
348-
*
349-
* @internal
350-
*/
351-
public function dumpCatalogue($locale, ConfigCacheInterface $cache)
345+
private function dumpCatalogue($locale, ConfigCacheInterface $cache)
352346
{
353347
$this->initializeCatalogue($locale);
354348
$fallbackContent = $this->getFallbackContent($this->catalogues[$locale]);

0 commit comments

Comments
 (0)