Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Twig Bridge] A simpler way to retrieve flash messages #21819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
31 changes: 31 additions & 0 deletions 31 src/Symfony/Bridge/Twig/AppVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,35 @@ public function getDebug()

return $this->debug;
}

/**
* Returns some or all the existing flash messages:
* * getFlashes() returns all the flash messages
* * getFlashes('notice') returns a simple array with flash messages of that type
* * getFlashes(array('notice', 'error')) returns a nested array of type => messages
*
* @return array
*/
public function getFlashes($types = null)
{
// needed to avoid starting the session automatically when looking for flash messages
try {
$session = $this->getSession();
if (null !== $session && !$session->isStarted()) {
return array();
}
} catch (\RuntimeException $e) {
Copy link
Contributor

@ro0NL ro0NL Mar 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be too broad, only to bypass The "app.session" variable is not available.. I guess it's an edge case :) but maybe do a quick return array() as well if there's no request stack and drop the try/catch.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes ... it's tricky. But avoiding the try...catch would require to duplicate some of the code of this very same class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would check requestStack here, and rely on getSession to be available otherwise.

return array();
}

if (null === $types) {
return $session->getFlashBag()->all();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should cast $types to an array first:

$type = (array) $type;

if (1 === count($types)) {
return $session->getFlashBag()->get($types[0]);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling this method with a string like "notice" as argument would make this check pass and get('n') to be called

Copy link
Member

@chalasr chalasr Mar 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, @HeahDude commented meanwhile :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way.. offset 0 may not exist :) What about ->get(reset($types))?

Copy link
Contributor

@ro0NL ro0NL Mar 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and it violates the api (more or less) with getFlashes(['notice']) (i'd expect a nested array... but you get a flat one).

edit: yeah.. already pointed out by @stof , but it's now advertised with

app.flashes(['notice']) nested array with type => messages

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ro0NL good catch about the potential non-existent 0 offset. I've added ... || empty($types) condition to return all the flashes in that case.


return array_intersect($session->getFlashBag()->all(), array_flip($types));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.