8000 Merge branch '2.7' into 2.8 · symfony/symfony@3adff11 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3adff11

Browse files
Merge branch '2.7' into 2.8
* 2.7: [DI] Add missing check in PhpDumper [Serializer] XmlEncoder: fix negative int and large numbers handling [Console] Fix dispatching throwables from ConsoleEvents::COMMAND
2 parents 76dd7b0 + 9774780 commit 3adff11

File tree

5 files changed

+140
-68
lines changed

5 files changed

+140
-68
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,17 @@ public function run(InputInterface $input = null, OutputInterface $output = null
122122
$this->configureIO($input, $output);
123123

124124
try {
125+
$e = null;
125126
$exitCode = $this->doRun($input, $output);
126-
} catch (\Exception $e) {
127+
} catch (\Exception $x) {
128+
$e = $x;
129+
} catch (\Throwable $x) {
130+
$e = new FatalThrowableError($x);
131+
}
132+
133+
if (null !== $e) {
127134
if (!$this->catchExceptions) {
128-
throw $e;
135+
throw $x;
129136
}
130137

131138
if ($output instanceof ConsoleOutputInterface) {
@@ -187,6 +194,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
187194
$input = new ArrayInput(array('command' => $this->defaultCommand));
188195
}
189196

197+
$this->runningCommand = null;
190198
// the command name MUST be the first element of the input
191199
$command = $this->find($name);
192200

@@ -844,13 +852,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
844852
}
845853

846854
if (null === $this->dispatcher) {
847-
try {
848-
return $command->run($input, $output);
849-
} catch (\Exception $e) {
850-
throw $e;
851-
} catch (\Throwable $e) {
852-
throw new FatalThrowableError($e);
853-
}
855+
return $command->run($input, $output);
854856
}
855857

856858
// bind before the console.command event, so the listeners have access to input options/arguments
@@ -862,37 +864,37 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
862864
}
863865

864866
$event = new ConsoleCommandEvent($command, $input, $output);
865-
$this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
867+
$e = null;
868+
869+
try {
870+
$this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
866871

867-
if ($event->commandShouldRun()) {
868-
try {
869-
$e = null;
872+
if ($event->commandShouldRun()) {
870873
$exitCode = $command->run($input, $output);
871-
} catch (\Exception $x) {
872-
$e = $x;
873-
} catch (\Throwable $x) {
874-
$e = new FatalThrowableError($x);
874+
} else {
875+
$exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
875876
}
876-
if (null !== $e) {
877-
$event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
878-
$this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
879-
880-
if ($e !== $event->getException()) {
881-
$x = $e = $event->getException();
882-
}
883-
884-
$event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
885-
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
877+
} catch (\Exception $e) {
878+
} catch (\Throwable $e) {
879+
}
880+
if (null !== $e) {
881+
$x = $e instanceof \Exception ? $e : new FatalThrowableError($e);
882+
$event = new ConsoleExceptionEvent($command, $input, $output, $x, $x->getCode());
883+
$this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
886884

887-
throw $x;
885+
if ($x !== $event->getException()) {
886+
$e = $event->getException();
888887
}
889-
} else {
890-
$exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
888+
$exitCode = $e->getCode();
891889
}
892890

893891
$event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
894892
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
895893

894+
if (null !== $e) {
895+
throw $e;
896+
}
897+
896898
return $event->getExitCode();
897899
}
898900

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -981,15 +981,28 @@ public function testRunDispatchesAllEventsWithException()
981981
$this->assertContains('before.foo.caught.after.', $tester->getDisplay());
982982
}
983983

984-
public function testRunWithError()
984+
public function testRunDispatchesAllEventsWithExceptionInListener()
985985
{
986-
if (method_exists($this, 'expectException')) {
987-
$this->expectException('Exception');
988-
$this->expectExceptionMessage('dymerr');
989-
} else {
990-
$this->setExpectedException('Exception', 'dymerr');
991-
}
986+
$dispatcher = $this->getDispatcher();
987+
$dispatcher->addListener('console.command', function () {
988+
throw new \RuntimeException('foo');
989+
});
990+
991+
$application = new Application();
992+
$application->setDispatcher($dispatcher);
993+
$application->setAutoExit(false);
994+
995+
$application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
996+
$output->write('foo.');
997+
});
998+
999+
$tester = new ApplicationTester($application);
1000+
$tester->run(array('command' => 'foo'));
1001+
$this->assertContains('before.caught.after.', $tester->getDisplay());
1002+
}
9921003

1004+
public function testRunWithError()
1005+
{
9931006
$application = new Application();
9941007
$application->setAutoExit(false);
9951008
$application->setCatchExceptions(false);
@@ -1001,7 +1014,13 @@ public function testRunWithError()
10011014
});
10021015

10031016
$tester = new ApplicationTester($application);
1004-
$tester->run(array('command' => 'dym'));
1017+
1018+
try {
1019+
$tester->run(array('command' => 'dym'));
1020+
$this->fail('Error expected.');
1021+
} catch (\Error $e) {
1022+
$this->assertSame('dymerr', $e->getMessage());
1023+
}
10051024
}
10061025

10071026
/**
@@ -1148,32 +1167,6 @@ public function testTerminalDimensions()
11481167
$this->assertSame(array($width, 80), $application->getTerminalDimensions());
11491168
}
11501169

1151-
protected function getDispatcher($skipCommand = false)
1152-
{
1153-
$dispatcher = new EventDispatcher();
1154-
$dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) use ($skipCommand) {
1155-
$event->getOutput()->write('before.');
1156-
1157-
if ($skipCommand) {
1158-
$event->disableCommand();
1159-
}
1160-
});
1161-
$dispatcher->addListener('console.terminate', function (ConsoleTerminateEvent $event) use ($skipCommand) {
1162-
$event->getOutput()->writeln('after.');
1163-
1164-
if (!$skipCommand) {
1165-
$event->setExitCode(113);
1166-
}
1167-
});
1168-
$dispatcher->addListener('console.exception', function (ConsoleExceptionEvent $event) {
1169-
$event->getOutput()->write('caught.');
1170-
1171-
$event->setException(new \LogicException('caught.', $event->getExitCode(), $event->getException()));
1172-
});
1173-
1174-
return $dispatcher;
1175-
}
1176-
11771170
public function testSetRunCustomDefaultCommand()
11781171
{
11791172
$command = new \FooCommand();
@@ -1212,6 +1205,32 @@ public function testCanCheckIfTerminalIsInteractive()
12121205
$inputStream = $application->getHelperSet()->get('question')->getInputStream();
12131206
$this->assertEquals($tester->getInput()->isInteractive(), @posix_isatty($inputStream));
12141207
}
1208+
1209+
protected function getDispatcher($skipCommand = false)
1210+
{
1211+
$dispatcher = new EventDispatcher();
1212+
$dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) use ($skipCommand) {
1213+
$event->getOutput()->write('before.');
1214+
1215+
if ($skipCommand) {
1216+
$event->disableCommand();
1217+
}
1218+
});
1219+
$dispatcher->addListener('console.terminate', function (ConsoleTerminateEvent $event) use ($skipCommand) {
1220+
$event->getOutput()->writeln('after.');
1221+
1222+
if (!$skipCommand) {
1223+
$event->setExitCode(ConsoleCommandEvent::RETURN_CODE_DISABLED);
1224+
}
1225+
});
1226+
$dispatcher->addListener('console.exception', function (ConsoleExceptionEvent $event) {
1227+
$event->getOutput()->write('caught.');
1228+
1229+
$event->setException(new \LogicException('caught.', $event->getExitCode(), $event->getException()));
1230+
});
1231+
1232+
return $dispatcher;
1233+
}
12151234
}
12161235

12171236
class CustomApplication extends Application

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,9 +1351,12 @@ private function dumpValue($value, $interpolate = true)
13511351
if (null !== $this->definitionVariables && $this->definitionVariables->contains($value)) {
13521352
return $this->dumpValue($this->definitionVariables->offsetGet($value), $interpolate);
13531353
}
1354-
if (count($value->getMethodCalls()) > 0) {
1354+
if ($value->getMethodCalls()) {
13551355
throw new RuntimeException('Cannot dump definitions which have method calls.');
13561356
}
1357+
if ($value->getProperties()) {
1358+
throw new RuntimeException('Cannot dump definitions which have properties.');
1359+
}
13571360
if (null !== $value->getConfigurator()) {
13581361
throw new RuntimeException('Cannot dump definitions which have a configurator.');
13591362
}

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,19 @@ private function parseXmlAttributes(\DOMNode $node)
301301
$data = array();
302302

303303
foreach ($node->attributes as $attr) {
304-
if (ctype_digit($attr->nodeValue)) {
305-
$data['@'.$attr->nodeName] = (int) $attr->nodeValue;
306-
} else {
304+
if (!is_numeric($attr->nodeValue)) {
307305
$data['@'.$attr->nodeName] = $attr->nodeValue;
306+
307+
continue;
308+
}
309+
310+
if (false !== $val = filter_var($attr->nodeValue, FILTER_VALIDATE_INT)) {
311+
$data['@'.$attr->nodeName] = $val;
312+
313+
continue;
308314
}
315+
316+
$data['@'.$attr->nodeName] = (float) $attr->nodeValue;
309317
}
310318

311319
return $data;

src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,46 @@ public function testDecodeScalar()
222222
$this->assertEquals('foo', $this->encoder->decode($source, 'xml'));
223223
}
224224

225+
public function testDecodeBigDigitAttributes()
226+
{
227+
$source = <<<XML
228+
<?xml version="1.0"?>
229+
<document index="182077241760011681341821060401202210011000045913000000017100">Name</document>
230+
XML;
231+
232+
$this->assertSame(array('@index' => 182077241760011681341821060401202210011000045913000000017100, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
233+
}
234+
235+
public function testDecodeNegativeIntAttribute()
236+
{
237+
$source = <<<XML
238+
<?xml version="1.0"?>
239+
<document index="-1234">Name</document>
240+
XML;
241+
242+
$this->assertSame(array('@index' => -1234, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
243+
}
244+
245+
public function testDecodeFloatAttribute()
246+
{
247+
$source = <<<XML
248+
<?xml version="1.0"?>
249+
<document index="-12.11">Name</document>
250+
XML;
251+
252+
$this->assertSame(array('@index' => -12.11, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
253+
}
254+
255+
public function testDecodeNegativeFloatAttribute()
256+
{
257+
$source = <<<XML
258+
<?xml version="1.0"?>
259+
<document index="-12.11">Name</document>
260+
XML;
261+
262+
$this->assertSame(array('@index' => -12.11, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
263+
}
264+
225265
public function testEncode()
226266
{
227267
$source = $this->getXmlSource();

0 commit comments

Comments
 (0)
0