Merge branch '2.2' into 2.3
* 2.2: [Process] Use a consistent way to reset data of the process latest run CS fix [HttpFoundation] Fixed removing a nonexisting namespaced attribute. [Validation] Fixed IdentityTranslator to pass correct Locale to MessageSelector SwiftMailerHandler in Monolog bridge now able to react to kernel.terminate event Conflicts: src/Symfony/Component/Process/Process.php
This commit is contained in:
commit
4cfbdf11c3
79
src/Symfony/Bridge/Monolog/Handler/SwiftMailerHandler.php
Normal file
79
src/Symfony/Bridge/Monolog/Handler/SwiftMailerHandler.php
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Bridge\Monolog\Handler;
|
||||||
|
|
||||||
|
use Monolog\Handler\SwiftMailerHandler as BaseSwiftMailerHandler;
|
||||||
|
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extended SwiftMailerHandler that flushes mail queue if necessary
|
||||||
|
*
|
||||||
|
* @author Philipp Kräutli <pkraeutli@astina.ch>
|
||||||
|
*/
|
||||||
|
class SwiftMailerHandler extends BaseSwiftMailerHandler
|
||||||
|
{
|
||||||
|
protected $transport;
|
||||||
|
|
||||||
|
protected $instantFlush = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Swift_Transport $transport
|
||||||
|
*/
|
||||||
|
public function setTransport(\Swift_Transport $transport)
|
||||||
|
{
|
||||||
|
$this->transport = $transport;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* After the kernel has been terminated we will always flush messages
|
||||||
|
*
|
||||||
|
* @param PostResponseEvent $event
|
||||||
|
*/
|
||||||
|
public function onKernelTerminate(PostResponseEvent $event)
|
||||||
|
{
|
||||||
|
$this->instantFlush = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function send($content, array $records)
|
||||||
|
{
|
||||||
|
parent::send($content, $records);
|
||||||
|
|
||||||
|
if ($this->instantFlush) {
|
||||||
|
$this->flushMemorySpool();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flushes the mail queue if a memory spool is used
|
||||||
|
*/
|
||||||
|
private function flushMemorySpool()
|
||||||
|
{
|
||||||
|
$mailerTransport = $this->mailer->getTransport();
|
||||||
|
if (!$mailerTransport instanceof \Swift_Transport_SpoolTransport) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$spool = $mailerTransport->getSpool();
|
||||||
|
if (!$spool instanceof \Swift_MemorySpool) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null === $this->transport) {
|
||||||
|
throw new \Exception('No transport available to flush mail queue');
|
||||||
|
}
|
||||||
|
|
||||||
|
$spool->flushQueue($this->transport);
|
||||||
|
}
|
||||||
|
}
|
@ -86,7 +86,7 @@ class NamespacedAttributeBag extends AttributeBag
|
|||||||
$retval = null;
|
$retval = null;
|
||||||
$attributes = & $this->resolveAttributePath($name);
|
$attributes = & $this->resolveAttributePath($name);
|
||||||
$name = $this->resolveKey($name);
|
$name = $this->resolveKey($name);
|
||||||
if (array_key_exists($name, $attributes)) {
|
if (null !== $attributes && array_key_exists($name, $attributes)) {
|
||||||
$retval = $attributes[$name];
|
$retval = $attributes[$name];
|
||||||
unset($attributes[$name]);
|
unset($attributes[$name]);
|
||||||
}
|
}
|
||||||
|
@ -143,6 +143,16 @@ class NamespacedAttributeBagTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertNull($this->bag->get('user.login'));
|
$this->assertNull($this->bag->get('user.login'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRemoveExistingNamespacedAttribute()
|
||||||
|
{
|
||||||
|
$this->assertSame('cod', $this->bag->remove('category/fishing/first'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRemoveNonexistingNamespacedAttribute()
|
||||||
|
{
|
||||||
|
$this->assertNull($this->bag->remove('foo/bar/baz'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testClear()
|
public function testClear()
|
||||||
{
|
{
|
||||||
$this->bag->clear();
|
$this->bag->clear();
|
||||||
|
@ -167,17 +167,7 @@ class Process
|
|||||||
|
|
||||||
public function __clone()
|
public function __clone()
|
||||||
{
|
{
|
||||||
$this->callback = null;
|
$this->resetProcessData();
|
||||||
$this->exitcode = null;
|
|
||||||
$this->fallbackExitcode = null;
|
|
||||||
$this->processInformation = null;
|
|
||||||
$this->stdout = null;
|
|
||||||
$this->stderr = null;
|
|
||||||
$this->pipes = null;
|
|
||||||
$this->process = null;
|
|
||||||
$this->status = self::STATUS_READY;
|
|
||||||
$this->fileHandles = null;
|
|
||||||
$this->readBytes = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -233,11 +223,8 @@ class Process
|
|||||||
throw new RuntimeException('Process is already running');
|
throw new RuntimeException('Process is already running');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->resetProcessData();
|
||||||
$this->starttime = microtime(true);
|
$this->starttime = microtime(true);
|
||||||
$this->stdout = '';
|
|
||||||
$this->stderr = '';
|
|
||||||
$this->incrementalOutputOffset = 0;
|
|
||||||
$this->incrementalErrorOutputOffset = 0;
|
|
||||||
$this->callback = $this->buildCallback($callback);
|
$this->callback = $this->buildCallback($callback);
|
||||||
$descriptors = $this->getDescriptors();
|
$descriptors = $this->getDescriptors();
|
||||||
|
|
||||||
@ -1280,4 +1267,25 @@ class Process
|
|||||||
|
|
||||||
return $this->exitcode;
|
return $this->exitcode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets data related to the latest run of the process.
|
||||||
|
*/
|
||||||
|
private function resetProcessData()
|
||||||
|
{
|
||||||
|
$this->starttime = null;
|
||||||
|
$this->callback = null;
|
||||||
|
$this->exitcode = null;
|
||||||
|
$this->fallbackExitcode = null;
|
||||||
|
$this->processInformation = null;
|
||||||
|
$this->stdout = null;
|
||||||
|
$this->stderr = null;
|
||||||
|
$this->pipes = null;
|
||||||
|
$this->process = null;
|
||||||
|
$this->status = self::STATUS_READY;
|
||||||
|
$this->fileHandles = null;
|
||||||
|
$this->readBytes = null;
|
||||||
|
$this->incrementalOutputOffset = 0;
|
||||||
|
$this->incrementalErrorOutputOffset = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -243,6 +243,27 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertTrue(strlen($process->getOutput()) > 0);
|
$this->assertTrue(strlen($process->getOutput()) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetExitCodeIsNullOnStart()
|
||||||
|
{
|
||||||
|
$process = $this->getProcess('php -r "usleep(200000);"');
|
||||||
|
$this->assertNull($process->getExitCode());
|
||||||
|
$process->start();
|
||||||
|
$this->assertNull($process->getExitCode());
|
||||||
|
$process->wait();
|
||||||
|
$this->assertEquals(0, $process->getExitCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetExitCodeIsNullOnWhenStartingAgain()
|
||||||
|
{
|
||||||
|
$process = $this->getProcess('php -r "usleep(200000);"');
|
||||||
|
$process->run();
|
||||||
|
$this->assertEquals(0, $process->getExitCode());
|
||||||
|
$process->start();
|
||||||
|
$this->assertNull($process->getExitCode());
|
||||||
|
$process->wait();
|
||||||
|
$this->assertEquals(0, $process->getExitCode());
|
||||||
|
}
|
||||||
|
|
||||||
public function testGetExitCode()
|
public function testGetExitCode()
|
||||||
{
|
{
|
||||||
$process = $this->getProcess('php -m');
|
$process = $this->getProcess('php -m');
|
||||||
|
@ -21,6 +21,22 @@ class SigchildDisabledProcessTest extends AbstractProcessTest
|
|||||||
parent::testGetExitCode();
|
parent::testGetExitCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \Symfony\Component\Process\Exception\RuntimeException
|
||||||
|
*/
|
||||||
|
public function testGetExitCodeIsNullOnStart()
|
||||||
|
{
|
||||||
|
parent::testGetExitCodeIsNullOnStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \Symfony\Component\Process\Exception\RuntimeException
|
||||||
|
*/
|
||||||
|
public function testGetExitCodeIsNullOnWhenStartingAgain()
|
||||||
|
{
|
||||||
|
parent::testGetExitCodeIsNullOnWhenStartingAgain();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \Symfony\Component\Process\Exception\RuntimeException
|
* @expectedException \Symfony\Component\Process\Exception\RuntimeException
|
||||||
*/
|
*/
|
||||||
|
@ -21,6 +21,7 @@ namespace Symfony\Component\Translation;
|
|||||||
class IdentityTranslator implements TranslatorInterface
|
class IdentityTranslator implements TranslatorInterface
|
||||||
{
|
{
|
||||||
private $selector;
|
private $selector;
|
||||||
|
private $locale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
@ -41,6 +42,7 @@ class IdentityTranslator implements TranslatorInterface
|
|||||||
*/
|
*/
|
||||||
public function setLocale($locale)
|
public function setLocale($locale)
|
||||||
{
|
{
|
||||||
|
$this->locale = $locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,6 +52,7 @@ class IdentityTranslator implements TranslatorInterface
|
|||||||
*/
|
*/
|
||||||
public function getLocale()
|
public function getLocale()
|
||||||
{
|
{
|
||||||
|
return $this->locale ?: \Locale::getDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,6 +72,6 @@ class IdentityTranslator implements TranslatorInterface
|
|||||||
*/
|
*/
|
||||||
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
|
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
|
||||||
{
|
{
|
||||||
return strtr($this->selector->choose((string) $id, (int) $number, $locale), $parameters);
|
return strtr($this->selector->choose((string) $id, (int) $number, $locale ?: $this->getLocale()), $parameters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,19 +29,43 @@ class IdentityTranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @dataProvider getTransChoiceTests
|
* @dataProvider getTransChoiceTests
|
||||||
*/
|
*/
|
||||||
public function testTransChoice($expected, $id, $number, $parameters)
|
public function testTransChoiceWithExplicitLocale($expected, $id, $number, $parameters)
|
||||||
{
|
{
|
||||||
|
$translator = new IdentityTranslator(new MessageSelector());
|
||||||
|
$translator->setLocale('en');
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $translator->transChoice($id, $number, $parameters));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getTransChoiceTests
|
||||||
|
*/
|
||||||
|
public function testTransChoiceWithDefaultLocale($expected, $id, $number, $parameters)
|
||||||
|
{
|
||||||
|
\Locale::setDefault('en');
|
||||||
|
|
||||||
$translator = new IdentityTranslator(new MessageSelector());
|
$translator = new IdentityTranslator(new MessageSelector());
|
||||||
|
|
||||||
$this->assertEquals($expected, $translator->transChoice($id, $number, $parameters));
|
$this->assertEquals($expected, $translator->transChoice($id, $number, $parameters));
|
||||||
}
|
}
|
||||||
|
|
||||||
// noop
|
|
||||||
public function testGetSetLocale()
|
public function testGetSetLocale()
|
||||||
{
|
{
|
||||||
$translator = new IdentityTranslator(new MessageSelector());
|
$translator = new IdentityTranslator(new MessageSelector());
|
||||||
$translator->setLocale('en');
|
$translator->setLocale('en');
|
||||||
$translator->getLocale();
|
|
||||||
|
$this->assertEquals('en', $translator->getLocale());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetLocaleReturnsDefaultLocaleIfNotSet()
|
||||||
|
{
|
||||||
|
$translator = new IdentityTranslator(new MessageSelector());
|
||||||
|
|
||||||
|
\Locale::setDefault('en');
|
||||||
|
$this->assertEquals('en', $translator->getLocale());
|
||||||
|
|
||||||
|
\Locale::setDefault('pt_BR');
|
||||||
|
$this->assertEquals('pt_BR', $translator->getLocale());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTransTests()
|
public function getTransTests()
|
||||||
@ -55,7 +79,12 @@ class IdentityTranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function getTransChoiceTests()
|
public function getTransChoiceTests()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array('There is 10 apples', '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples', 10, array('%count%' => 10)),
|
array('There is no apple', '{0} There is no apple|{1} There is one apple|]1,Inf] There are %count% apples', 0, array('%count%' => 0)),
|
||||||
|
array('There is one apple', '{0} There is no apple|{1} There is one apple|]1,Inf] There are %count% apples', 1, array('%count%' => 1)),
|
||||||
|
array('There are 10 apples', '{0} There is no apple|{1} There is one apple|]1,Inf] There are %count% apples', 10, array('%count%' => 10)),
|
||||||
|
array('There are 0 apples', 'There is 1 apple|There are %count% apples', 0, array('%count%' => 0)),
|
||||||
|
array('There is 1 apple', 'There is 1 apple|There are %count% apples', 1, array('%count%' => 1)),
|
||||||
|
array('There are 10 apples', 'There is 1 apple|There are %count% apples', 10, array('%count%' => 10)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user