From 58ac241c1d56b1d6b23976476a58c0533fa1c5ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sun, 22 May 2016 13:56:26 +0200 Subject: [PATCH 1/8] [Serializer] ObjectNormalizer: add missing parameters --- .../Component/Serializer/Normalizer/ObjectNormalizer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php index fc774e6563..2c2457b8ac 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php @@ -68,14 +68,14 @@ class ObjectNormalizer extends AbstractObjectNormalizer $attributeName = lcfirst(substr($name, 2)); } - if (null !== $attributeName && $this->isAllowedAttribute($object, $attributeName)) { + if (null !== $attributeName && $this->isAllowedAttribute($object, $attributeName, $format, $context)) { $attributes[$attributeName] = true; } } // properties foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) { - if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name)) { + if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) { continue; } From 29b4115d1a408105c5395393503beea39cb1669f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sun, 22 May 2016 13:53:30 +0200 Subject: [PATCH 2/8] [Serializer] AbstractObjectNormalizer: be sure that isAllowedAttribute is called --- .../Serializer/Normalizer/AbstractObjectNormalizer.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index d39023a4fd..983f5a704c 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -182,10 +182,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer $attribute = $this->nameConverter->denormalize($attribute); } - $allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes); - $ignored = in_array($attribute, $this->ignoredAttributes); - - if (!$allowed || $ignored) { + if (($allowedAttributes !== false && !in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) { continue; } From 9461750bd0703a01a31c3cdcebf922cda404d14d Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 25 May 2016 09:47:04 +0200 Subject: [PATCH 3/8] [Cache] Drop counting hit/miss in ProxyAdapter --- .../Component/Cache/Adapter/ProxyAdapter.php | 36 ++----------------- .../Cache/Tests/Adapter/ProxyAdapterTest.php | 17 --------- 2 files changed, 2 insertions(+), 51 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php b/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php index ec90e63c46..d4e503cf8b 100644 --- a/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php @@ -24,8 +24,6 @@ class ProxyAdapter implements AdapterInterface private $namespace; private $namespaceLen; private $createCacheItem; - private $hits = 0; - private $misses = 0; public function __construct(CacheItemPoolInterface $pool, $namespace = '', $defaultLifetime = 0) { @@ -54,13 +52,8 @@ class ProxyAdapter implements AdapterInterface { $f = $this->createCacheItem; $item = $this->pool->getItem($this->getId($key)); - if ($isHit = $item->isHit()) { - ++$this->hits; - } else { - ++$this->misses; - } - return $f($key, $item->get(), $isHit); + return $f($key, $item->get(), $item->isHit()); } /** @@ -158,39 +151,14 @@ class ProxyAdapter implements AdapterInterface $f = $this->createCacheItem; foreach ($items as $key => $item) { - if ($isHit = $item->isHit()) { - ++$this->hits; - } else { - ++$this->misses; - } if ($this->namespaceLen) { $key = substr($key, $this->namespaceLen); } - yield $key => $f($key, $item->get(), $isHit); + yield $key => $f($key, $item->get(), $item->isHit()); } } - /** - * Returns the number of cache read hits. - * - * @return int - */ - public function getHits() - { - return $this->hits; - } - - /** - * Returns the number of cache read misses. - * - * @return int - */ - public function getMisses() - { - return $this->misses; - } - private function getId($key) { CacheItem::validateKey($key); diff --git a/src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php index 58594b0e1e..76606e256e 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php @@ -29,21 +29,4 @@ class ProxyAdapterTest extends CachePoolTest { return new ProxyAdapter(new ArrayAdapter()); } - - public function testGetHitsMisses() - { - $pool = $this->createCachePool(); - - $this->assertSame(0, $pool->getHits()); - $this->assertSame(0, $pool->getMisses()); - - $bar = $pool->getItem('bar'); - $this->assertSame(0, $pool->getHits()); - $this->assertSame(1, $pool->getMisses()); - - $pool->save($bar->set('baz')); - $bar = $pool->getItem('bar'); - $this->assertSame(1, $pool->getHits()); - $this->assertSame(1, $pool->getMisses()); - } } From 37cacc4874856128babf6130f7fec18708fd0fcb Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Wed, 25 May 2016 22:21:27 +0200 Subject: [PATCH 4/8] [Serializer][#18837] adding a test --- .../AbstractObjectNormalizerTest.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php new file mode 100644 index 0000000000..5660dbc361 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Normalizer; + +use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; + +class AbstractObjectNormalizerTest extends \PHPUnit_Framework_TestCase +{ + public function testDenormalize() + { + $normalizer = new AbstractObjectNormalizerDummy(); + $normalizedData = $normalizer->denormalize(array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'), __NAMESPACE__.'\Dummy'); + + $this->assertSame('foo', $normalizedData->foo); + $this->assertNull($normalizedData->bar); + $this->assertSame('baz', $normalizedData->baz); + } +} + +class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer +{ + protected function extractAttributes($object, $format = null, array $context = array()) + { + } + + protected function getAttributeValue($object, $attribute, $format = null, array $context = array()) + { + } + + protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array()) + { + $object->$attribute = $value; + } + + protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array()) + { + return in_array($attribute, array('foo', 'baz')); + } +} + +class Dummy +{ + public $foo; + public $bar; + public $baz; +} From dbf45e4a76b7f424fda62946546336e584852463 Mon Sep 17 00:00:00 2001 From: Charles Sarrazin Date: Thu, 26 May 2016 03:56:44 +0200 Subject: [PATCH 5/8] [Ldap] Fixed issue with Entry password attribute containing array of values and made password attribute configurable --- .../Security/UserProvider/LdapFactory.php | 2 + .../Core/Tests/User/LdapUserProviderTest.php | 139 +++++++++++++++++- .../Security/Core/User/LdapUserProvider.php | 41 +++++- 3 files changed, 177 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/LdapFactory.php b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/LdapFactory.php index 2612bf5221..ecaf35364d 100644 --- a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/LdapFactory.php +++ b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/LdapFactory.php @@ -35,6 +35,7 @@ class LdapFactory implements UserProviderFactoryInterface ->replaceArgument(4, $config['default_roles']) ->replaceArgument(5, $config['uid_key']) ->replaceArgument(6, $config['filter']) + ->replaceArgument(7, $config['password_attribute']) ; } @@ -58,6 +59,7 @@ class LdapFactory implements UserProviderFactoryInterface ->end() ->scalarNode('uid_key')->defaultValue('sAMAccountName')->end() ->scalarNode('filter')->defaultValue('({uid_key}={username})')->end() + ->scalarNode('password_attribute')->defaultNull()->end() ->end() ; } diff --git a/src/Symfony/Component/Security/Core/Tests/User/LdapUserProviderTest.php b/src/Symfony/Component/Security/Core/Tests/User/LdapUserProviderTest.php index 6876eec8df..b942e76da1 100644 --- a/src/Symfony/Component/Security/Core/Tests/User/LdapUserProviderTest.php +++ b/src/Symfony/Component/Security/Core/Tests/User/LdapUserProviderTest.php @@ -105,7 +105,10 @@ class LdapUserProviderTest extends \PHPUnit_Framework_TestCase $provider->loadUserByUsername('foo'); } - public function testSuccessfulLoadUserByUsername() + /** + * @expectedException \Symfony\Component\Security\Core\Exception\InvalidArgumentException + */ + public function testLoadUserByUsernameFailsIfMoreThanOneLdapPasswordsInEntry() { $result = $this->getMock(CollectionInterface::class); $query = $this->getMock(QueryInterface::class); @@ -120,8 +123,95 @@ class LdapUserProviderTest extends \PHPUnit_Framework_TestCase ->method('offsetGet') ->with(0) ->will($this->returnValue(new Entry('foo', array( - 'sAMAccountName' => 'foo', - 'userpassword' => 'bar', + 'sAMAccountName' => array('foo'), + 'userpassword' => array('bar', 'baz'), + ) + ))) + ; + $result + ->expects($this->once()) + ->method('count') + ->will($this->returnValue(1)) + ; + $ldap + ->expects($this->once()) + ->method('escape') + ->will($this->returnValue('foo')) + ; + $ldap + ->expects($this->once()) + ->method('query') + ->will($this->returnValue($query)) + ; + + $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, array(), 'sAMAccountName', '({uid_key}={username})', 'userpassword'); + $this->assertInstanceOf( + 'Symfony\Component\Security\Core\User\User', + $provider->loadUserByUsername('foo') + ); + } + + /** + * @expectedException \Symfony\Component\Security\Core\Exception\InvalidArgumentException + */ + public function testLoadUserByUsernameFailsIfEntryHasNoPasswordAttribute() + { + $result = $this->getMock(CollectionInterface::class); + $query = $this->getMock(QueryInterface::class); + $query + ->expects($this->once()) + ->method('execute') + ->will($this->returnValue($result)) + ; + $ldap = $this->getMock(LdapInterface::class); + $result + ->expects($this->once()) + ->method('offsetGet') + ->with(0) + ->will($this->returnValue(new Entry('foo', array( + 'sAMAccountName' => array('foo'), + ) + ))) + ; + $result + ->expects($this->once()) + ->method('count') + ->will($this->returnValue(1)) + ; + $ldap + ->expects($this->once()) + ->method('escape') + ->will($this->returnValue('foo')) + ; + $ldap + ->expects($this->once()) + ->method('query') + ->will($this->returnValue($query)) + ; + + $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, array(), 'sAMAccountName', '({uid_key}={username})', 'userpassword'); + $this->assertInstanceOf( + 'Symfony\Component\Security\Core\User\User', + $provider->loadUserByUsername('foo') + ); + } + + public function testLoadUserByUsernameIsSuccessfulWithoutPasswordAttribute() + { + $result = $this->getMock(CollectionInterface::class); + $query = $this->getMock(QueryInterface::class); + $query + ->expects($this->once()) + ->method('execute') + ->will($this->returnValue($result)) + ; + $ldap = $this->getMock(LdapInterface::class); + $result + ->expects($this->once()) + ->method('offsetGet') + ->with(0) + ->will($this->returnValue(new Entry('foo', array( + 'sAMAccountName' => array('foo'), ) ))) ; @@ -147,4 +237,47 @@ class LdapUserProviderTest extends \PHPUnit_Framework_TestCase $provider->loadUserByUsername('foo') ); } + + public function testLoadUserByUsernameIsSuccessfulWithPasswordAttribute() + { + $result = $this->getMock(CollectionInterface::class); + $query = $this->getMock(QueryInterface::class); + $query + ->expects($this->once()) + ->method('execute') + ->will($this->returnValue($result)) + ; + $ldap = $this->getMock(LdapInterface::class); + $result + ->expects($this->once()) + ->method('offsetGet') + ->with(0) + ->will($this->returnValue(new Entry('foo', array( + 'sAMAccountName' => array('foo'), + 'userpassword' => array('bar'), + ) + ))) + ; + $result + ->expects($this->once()) + ->method('count') + ->will($this->returnValue(1)) + ; + $ldap + ->expects($this->once()) + ->method('escape') + ->will($this->returnValue('foo')) + ; + $ldap + ->expects($this->once()) + ->method('query') + ->will($this->returnValue($query)) + ; + + $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, array(), 'sAMAccountName', '({uid_key}={username})', 'userpassword'); + $this->assertInstanceOf( + 'Symfony\Component\Security\Core\User\User', + $provider->loadUserByUsername('foo') + ); + } } diff --git a/src/Symfony/Component/Security/Core/User/LdapUserProvider.php b/src/Symfony/Component/Security/Core/User/LdapUserProvider.php index a37981cc3f..e722c98e6d 100644 --- a/src/Symfony/Component/Security/Core/User/LdapUserProvider.php +++ b/src/Symfony/Component/Security/Core/User/LdapUserProvider.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Security\Core\User; use Symfony\Component\Ldap\Entry; +use Symfony\Component\Security\Core\Exception\InvalidArgumentException; use Symfony\Component\Security\Core\Exception\UnsupportedUserException; use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Ldap\Exception\ConnectionException; @@ -31,6 +32,7 @@ class LdapUserProvider implements UserProviderInterface private $searchPassword; private $defaultRoles; private $defaultSearch; + private $passwordAttribute; /** * @param LdapInterface $ldap @@ -41,7 +43,7 @@ class LdapUserProvider implements UserProviderInterface * @param string $uidKey * @param string $filter */ - public function __construct(LdapInterface $ldap, $baseDn, $searchDn = null, $searchPassword = null, array $defaultRoles = array(), $uidKey = 'sAMAccountName', $filter = '({uid_key}={username})') + public function __construct(LdapInterface $ldap, $baseDn, $searchDn = null, $searchPassword = null, array $defaultRoles = array(), $uidKey = 'sAMAccountName', $filter = '({uid_key}={username})', $passwordAttribute = null) { $this->ldap = $ldap; $this->baseDn = $baseDn; @@ -49,6 +51,7 @@ class LdapUserProvider implements UserProviderInterface $this->searchPassword = $searchPassword; $this->defaultRoles = $defaultRoles; $this->defaultSearch = str_replace('{uid_key}', $uidKey, $filter); + $this->passwordAttribute = $passwordAttribute; } /** @@ -99,8 +102,42 @@ class LdapUserProvider implements UserProviderInterface return $class === 'Symfony\Component\Security\Core\User\User'; } + /** + * Loads a user from an LDAP entry. + * + * @param string $username + * @param Entry $entry + * + * @return User + */ private function loadUser($username, Entry $entry) { - return new User($username, $entry->getAttribute('userpassword'), $this->defaultRoles); + $password = $this->getPassword($entry); + + return new User($username, $password, $this->defaultRoles); + } + + /** + * Fetches the password from an LDAP entry. + * + * @param null|Entry $entry + */ + private function getPassword(Entry $entry) + { + if (null === $this->passwordAttribute) { + return; + } + + if (!$entry->hasAttribute($this->passwordAttribute)) { + throw new InvalidArgumentException(sprintf('Missing attribute "%s" for user "%s".', $this->passwordAttribute, $entry->getDn())); + } + + $values = $entry->getAttribute($this->passwordAttribute); + + if (1 !== count($values)) { + throw new InvalidArgumentException(sprintf('Attribute "%s" has multiple values.', $this->passwordAttribute)); + } + + return $values[0]; } } From 91a2f5dea193d01596428fe92d2b5333d1a4730d Mon Sep 17 00:00:00 2001 From: Linnaea Von Lavia Date: Thu, 26 May 2016 10:56:28 +0800 Subject: [PATCH 6/8] Fix js comment in profiler --- .../Resources/views/Profiler/base_js.html.twig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig index d9f92ca26a..23e05030c5 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig @@ -81,8 +81,8 @@ requestStack = [], extractHeaders = function(xhr, stackElement) { - // Here we avoid to call xhr.getResponseHeader in order to - // prevent polluting the console with CORS security errors + /* Here we avoid to call xhr.getResponseHeader in order to */ + /* prevent polluting the console with CORS security errors */ var allHeaders = xhr.getAllResponseHeaders(); var ret; From 7cb76558ceca37d06b366dbe46aceb6ae274306d Mon Sep 17 00:00:00 2001 From: Peter Rehm Date: Wed, 25 May 2016 08:27:19 +0200 Subject: [PATCH 7/8] [Console][DX] Fixed ambiguous error message when using a duplicate option shortcut --- .../Component/Console/Command/Command.php | 4 +-- .../Console/Tests/ApplicationTest.php | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php index e4b8fb0aa7..9385b9d2a6 100644 --- a/src/Symfony/Component/Console/Command/Command.php +++ b/src/Symfony/Component/Console/Command/Command.php @@ -287,14 +287,14 @@ class Command return; } + $this->definition->addOptions($this->application->getDefinition()->getOptions()); + if ($mergeArgs) { $currentArguments = $this->definition->getArguments(); $this->definition->setArguments($this->application->getDefinition()->getArguments()); $this->definition->addArguments($currentArguments); } - $this->definition->addOptions($this->application->getDefinition()->getOptions()); - $this->applicationDefinitionMerged = true; if ($mergeArgs) { $this->applicationDefinitionMergedWithArgs = true; diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index 8e44b7ae17..8637b0f58f 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -645,6 +645,33 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase $this->assertSame(1, $exitCode, '->run() returns exit code 1 when exception code is 0'); } + /** + * @expectedException \LogicException + * @expectedExceptionMessage An option with shortcut "e" already exists. + */ + public function testAddingOptionWithDuplicateShortcut() + { + $dispatcher = new EventDispatcher(); + $application = new Application(); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + $application->setDispatcher($dispatcher); + + $application->getDefinition()->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'Environment')); + + $application + ->register('foo') + ->setAliases(['f']) + ->setDefinition(array(new InputOption('survey', 'e', InputOption::VALUE_REQUIRED, 'My option with a shortcut.'))) + ->setCode(function (InputInterface $input, OutputInterface $output) {}) + ; + + $input = new ArrayInput(array('command' => 'foo')); + $output = new NullOutput(); + + $application->run($input, $output); + } + /** * @expectedException \LogicException * @dataProvider getAddingAlreadySetDefinitionElementData From 963fe1d6350e6c56ad84b1e6b76c3f389d33b130 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Sun, 22 May 2016 12:24:43 +0200 Subject: [PATCH 8/8] [Console] SymfonyStyle: Align multi-line/very-long-line blocks Remove SymfonyStyle::comment() changes (out of scope) CS Fixes Add tests --- .../Component/Console/Style/SymfonyStyle.php | 19 ++++++++++++++++--- .../Style/SymfonyStyle/command/command_10.php | 17 +++++++++++++++++ .../Style/SymfonyStyle/command/command_9.php | 11 +++++++++++ .../Style/SymfonyStyle/output/output_10.txt | 7 +++++++ .../Style/SymfonyStyle/output/output_9.txt | 5 +++++ .../Console/Tests/Style/SymfonyStyleTest.php | 2 +- 6 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_10.php create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_9.php create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_10.txt create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_9.txt diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 21fc3b92cf..03550836cf 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -67,23 +67,36 @@ class SymfonyStyle extends OutputStyle { $this->autoPrependBlock(); $messages = is_array($messages) ? array_values($messages) : array($messages); + $indentLength = 0; $lines = array(); - // add type if (null !== $type) { - $messages[0] = sprintf('[%s] %s', $type, $messages[0]); + $typePrefix = sprintf('[%s] ', $type); + $indentLength = strlen($typePrefix); + $lineIndentation = str_repeat(' ', $indentLength); } // wrap and add newlines for each element foreach ($messages as $key => $message) { $message = OutputFormatter::escape($message); - $lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen($prefix), PHP_EOL, true))); + $lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen($prefix) - $indentLength, PHP_EOL, true))); + + // prefix each line with a number of spaces equivalent to the type length + if (null !== $type) { + foreach ($lines as &$line) { + $line = $lineIndentation === substr($line, 0, $indentLength) ? $line : $lineIndentation.$line; + } + } if (count($messages) > 1 && $key < count($messages) - 1) { $lines[] = ''; } } + if (null !== $type) { + $lines[0] = substr_replace($lines[0], $typePrefix, 0, $indentLength); + } + if ($padding && $this->isDecorated()) { array_unshift($lines, ''); $lines[] = ''; diff --git a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_10.php b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_10.php new file mode 100644 index 0000000000..4120df9cb6 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_10.php @@ -0,0 +1,17 @@ +block( + 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum', + 'CUSTOM', + 'fg=white;bg=green', + 'X ', + true + ); +}; diff --git a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_9.php b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_9.php new file mode 100644 index 0000000000..0f0a4e69d0 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_9.php @@ -0,0 +1,11 @@ +block(['Custom block', 'Second custom block line'], 'CUSTOM', 'fg=white;bg=green', 'X ', true); +}; diff --git a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_10.txt b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_10.txt new file mode 100644 index 0000000000..385c6a283c --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_10.txt @@ -0,0 +1,7 @@ + +X [CUSTOM] Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et +X dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea +X commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat +X nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit +X anim id est laborum + diff --git a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_9.txt b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_9.txt new file mode 100644 index 0000000000..069c0d5119 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_9.txt @@ -0,0 +1,5 @@ + +X [CUSTOM] Custom block +X +X Second custom block line + diff --git a/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php b/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php index 6bf64129f4..889a9c82f2 100644 --- a/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php +++ b/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php @@ -57,7 +57,7 @@ class SymfonyStyleTest extends PHPUnit_Framework_TestCase public function testLongWordsBlockWrapping() { - $word = 'Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygon'; + $word = 'Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygovgollhjvhvljfezefeqifzeiqgiqzhrsdgihqzridghqridghqirshdghdghieridgheirhsdgehrsdvhqrsidhqshdgihrsidvqhneriqsdvjzergetsrfhgrstsfhsetsfhesrhdgtesfhbzrtfbrztvetbsdfbrsdfbrn'; $wordLength = strlen($word); $maxLineLength = SymfonyStyle::MAX_LINE_LENGTH - 3;