merged 2.0

This commit is contained in:
Fabien Potencier 2012-01-16 07:44:08 +01:00
commit 5fa0f2d92b
19 changed files with 211 additions and 43 deletions

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;

View File

@ -79,7 +79,7 @@ class Configuration implements ConfigurationInterface
->useAttributeAsKey('key')
->prototype('array')
->beforeNormalization()
->ifTrue(function($v){ return is_string($v) && '@' === substr($v, 0, 1); })
->ifTrue(function($v){ return is_string($v) && 0 === strpos($v, '@'); })
->then(function($v){ return array('id' => substr($v, 1), 'type' => 'service'); })
->end()
->beforeNormalization()

View File

@ -441,7 +441,7 @@ abstract class Client
protected function getAbsoluteUri($uri)
{
// already absolute?
if ('http' === substr($uri, 0, 4)) {
if (0 === strpos($uri, 'http')) {
return $uri;
}

View File

@ -77,7 +77,7 @@ class ArgvInput extends Input
{
$this->parsed = $this->tokens;
while (null !== $token = array_shift($this->parsed)) {
if ('--' === substr($token, 0, 2)) {
if (0 === strpos($token, '--')) {
$this->parseLongOption($token);
} elseif ('-' === $token[0]) {
$this->parseShortOption($token);

View File

@ -116,7 +116,7 @@ class ArrayInput extends Input
protected function parse()
{
foreach ($this->parameters as $key => $value) {
if ('--' === substr($key, 0, 2)) {
if (0 === strpos($key, '--')) {
$this->addLongOption(substr($key, 2), $value);
} elseif ('-' === $key[0]) {
$this->addShortOption(substr($key, 1), $value);

View File

@ -46,7 +46,7 @@ class InputOption
*/
public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
{
if ('--' === substr($name, 0, 2)) {
if (0 === strpos($name, '--')) {
$name = substr($name, 2);
}

View File

@ -80,7 +80,7 @@ class Link
$uri = trim($this->getRawUri());
// absolute URL?
if ('http' === substr($uri, 0, 4)) {
if (0 === strpos($uri, 'http')) {
return $uri;
}

View File

@ -632,7 +632,11 @@ class Request
*/
public function getPort()
{
return $this->headers->get('X-Forwarded-Port') ?: $this->server->get('SERVER_PORT');
if (self::$trustProxy && $this->headers->has('X-Forwarded-Port')) {
return $this->headers->get('X-Forwarded-Port');
} else {
return $this->server->get('SERVER_PORT');
}
}
/**

View File

@ -175,7 +175,7 @@ class Response
$charset = $this->charset ?: 'UTF-8';
if (!$headers->has('Content-Type')) {
$headers->set('Content-Type', 'text/html; charset='.$charset);
} elseif ('text/' === substr($headers->get('Content-Type'), 0, 5) && false === strpos($headers->get('Content-Type'), 'charset')) {
} elseif (0 === strpos($headers->get('Content-Type'), 'text/') && false === strpos($headers->get('Content-Type'), 'charset')) {
// add the charset
$headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset);
}

View File

@ -28,7 +28,7 @@ class ServerBag extends ParameterBag
{
$headers = array();
foreach ($this->parameters as $key => $value) {
if ('HTTP_' === substr($key, 0, 5)) {
if (0 === strpos($key, 'HTTP_')) {
$headers[substr($key, 5)] = $value;
}
// CONTENT_* are not prefixed with HTTP_

View File

@ -64,7 +64,7 @@ class Esi
return false;
}
return (Boolean) preg_match('#ESI/1.0#', $value);
return false !== strpos($value, 'ESI/1.0');
}
/**

View File

@ -614,7 +614,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface
{
$parameters = array();
foreach ($_SERVER as $key => $value) {
if ('SYMFONY__' === substr($key, 0, 9)) {
if (0 === strpos($key, 'SYMFONY__')) {
$parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value;
}
}

View File

@ -24,7 +24,7 @@ class MysqlProfilerStorage extends PdoProfilerStorage
protected function initDb()
{
if (null === $this->db) {
if ('mysql' !== substr($this->dsn, 0, 5)) {
if (0 !== strpos($this->dsn, 'mysql')) {
throw new \RuntimeException('Please check your configuration. You are trying to use Mysql with a wrong dsn. "'.$this->dsn.'"');
}

View File

@ -25,7 +25,7 @@ class SqliteProfilerStorage extends PdoProfilerStorage
protected function initDb()
{
if (null === $this->db || $this->db instanceof \SQLite3) {
if ('sqlite' !== substr($this->dsn, 0, 6 )) {
if (0 !== strpos($this->dsn, 'sqlite')) {
throw new \RuntimeException('You are trying to use Sqlite with a wrong dsn. "'.$this->dsn.'"');
}
if (class_exists('SQLite3')) {

View File

@ -431,7 +431,7 @@ class StubIntlDateFormatter
$timeZone = $timeZoneId;
// Get an Etc/GMT time zone that is accepted for \DateTimeZone
if ('GMT' !== $timeZoneId && 'GMT' === substr($timeZoneId, 0, 3)) {
if ('GMT' !== $timeZoneId && 0 === strpos($timeZoneId, 'GMT')) {
try {
$timeZoneId = DateFormat\TimeZoneTransformer::getEtcTimeZoneId($timeZoneId);
} catch (\InvalidArgumentException $e) {

View File

@ -52,7 +52,11 @@ class CollectionValidator extends ConstraintValidator
}
foreach ($constraint->fields as $field => $constraints) {
if (array_key_exists($field, $value)) {
if (
// bug fix issue #2779
(is_array($value) && array_key_exists($field, $value)) ||
($value instanceof \ArrayAccess && $value->offsetExists($field))
) {
// cannot simply cast to array, because then the object is converted to an
// array instead of wrapped inside
$constraints = is_array($constraints) ? $constraints : array($constraints);

View File

@ -114,7 +114,7 @@ class Parser
}
if ('<<' === $key) {
if (isset($values['value']) && '*' === substr($values['value'], 0, 1)) {
if (isset($values['value']) && 0 === strpos($values['value'], '*')) {
$isInPlace = substr($values['value'], 1);
if (!array_key_exists($isInPlace, $this->refs)) {
throw new ParseException(sprintf('Reference "%s" does not exist.', $isInPlace), $this->getRealCurrentLineNb() + 1, $this->currentLine);
@ -188,7 +188,7 @@ class Parser
if (is_array($value)) {
$first = reset($value);
if (is_string($first) && '*' === substr($first, 0, 1)) {
if (is_string($first) && 0 === strpos($first, '*')) {
$data = array();
foreach ($value as $alias) {
$data[] = $this->refs[substr($alias, 1)];
@ -347,7 +347,7 @@ class Parser
*/
private function parseValue($value)
{
if ('*' === substr($value, 0, 1)) {
if (0 === strpos($value, '*')) {
if (false !== $pos = strpos($value, '#')) {
$value = substr($value, 1, $pos - 2);
} else {

View File

@ -53,7 +53,11 @@ class Yaml
{
// if input is a file, process it
$file = '';
if (strpos($input, "\n") === false && is_file($input) && is_readable($input)) {
if (strpos($input, "\n") === false && is_file($input)) {
if (false === is_readable($input)) {
throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $input));
}
$file = $input;
if (self::$enablePhpParsing) {
ob_start();

View File

@ -13,9 +13,71 @@ namespace Symfony\Tests\Component\Validator\Constraints;
use Symfony\Component\Validator\ExecutionContext;
use Symfony\Component\Validator\Constraints\Min;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\CollectionValidator;
/**
* This class is a hand written simplified version of PHP native `ArrayObject`
* class, to show that it behaves different than PHP native implementation.
*/
class TestArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable
{
private $array;
public function __construct(array $array = null)
{
$this->array = (array) ($array ?: array());
}
public function offsetExists($offset)
{
return array_key_exists($offset, $this->array);
}
public function offsetGet($offset)
{
return $this->array[$offset];
}
public function offsetSet($offset, $value)
{
if (null === $offset) {
$this->array[] = $value;
} else {
$this->array[$offset] = $value;
}
}
public function offsetUnset($offset)
{
if (array_key_exists($offset, $this->array)) {
unset($this->array[$offset]);
}
}
public function getIterator()
{
return new \ArrayIterator($this->array);
}
public function count()
{
return count($this->array);
}
public function serialize()
{
return serialize($this->array);
}
public function unserialize($serialized)
{
$this->array = (array) unserialize((string) $serialized);
}
}
class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
{
protected $validator;
@ -49,15 +111,22 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
public function testFieldsAsDefaultOption()
{
$this->validator->isValid(array('foo' => 'foobar'), new Collection(array(
$this->assertTrue($this->validator->isValid(array('foo' => 'foobar'), new Collection(array(
'foo' => new Min(4),
)));
))));
$this->assertTrue($this->validator->isValid(new \ArrayObject(array('foo' => 'foobar')), new Collection(array(
'foo' => new Min(4),
))));
$this->assertTrue($this->validator->isValid(new TestArrayObject(array('foo' => 'foobar')), new Collection(array(
'foo' => new Min(4),
))));
}
/**
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
*/
public function testThrowsExceptionIfNotTraversable()
{
$this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
$this->validator->isValid('foobar', new Collection(array('fields' => array(
'foo' => new Min(4),
))));
@ -112,13 +181,11 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
))));
}
public function testExtraFieldsDisallowed()
/**
* @dataProvider getArgumentsWithExtraFields
*/
public function testExtraFieldsDisallowed($array)
{
$array = array(
'foo' => 5,
'bar' => 6,
);
$this->assertFalse($this->validator->isValid($array, new Collection(array(
'fields' => array(
'foo' => new Min(4),
@ -132,12 +199,15 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
$array = array(
'foo' => null,
);
$this->assertTrue($this->validator->isValid($array, new Collection(array(
$collection = new Collection(array(
'fields' => array(
'foo' => new Min(4),
),
))));
));
$this->assertTrue($this->validator->isValid($array, $collection));
$this->assertTrue($this->validator->isValid(new \ArrayObject($array), $collection));
$this->assertTrue($this->validator->isValid(new TestArrayObject($array), $collection));
}
public function testExtraFieldsAllowed()
@ -146,13 +216,16 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
'foo' => 5,
'bar' => 6,
);
$this->assertTrue($this->validator->isValid($array, new Collection(array(
$collection = new Collection(array(
'fields' => array(
'foo' => new Min(4),
),
'allowExtraFields' => true,
))));
));
$this->assertTrue($this->validator->isValid($array, $collection));
$this->assertTrue($this->validator->isValid(new \ArrayObject($array), $collection));
$this->assertTrue($this->validator->isValid(new TestArrayObject($array), $collection));
}
public function testMissingFieldsDisallowed()
@ -162,6 +235,16 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
'foo' => new Min(4),
),
))));
$this->assertFalse($this->validator->isValid(new \ArrayObject(array()), new Collection(array(
'fields' => array(
'foo' => new Min(4),
),
))));
$this->assertFalse($this->validator->isValid(new TestArrayObject(array()), new Collection(array(
'fields' => array(
'foo' => new Min(4),
),
))));
}
public function testMissingFieldsAllowed()
@ -172,16 +255,58 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
),
'allowMissingFields' => true,
))));
$this->assertTrue($this->validator->isValid(new \ArrayObject(array()), new Collection(array(
'fields' => array(
'foo' => new Min(4),
),
'allowMissingFields' => true,
))));
$this->assertTrue($this->validator->isValid(new TestArrayObject(array()), new Collection(array(
'fields' => array(
'foo' => new Min(4),
),
'allowMissingFields' => true,
))));
}
public function getValidArguments()
{
return array(
// can only test for one entry, because PHPUnits mocking does not allow
// to expect multiple method calls with different arguments
array(array('foo' => 3)),
array(new \ArrayObject(array('foo' => 3))),
);
public function testArrayAccessObject() {
$value = new TestArrayObject();
$value['foo'] = 12;
$value['asdf'] = 'asdfaf';
$this->assertTrue(isset($value['asdf']));
$this->assertTrue(isset($value['foo']));
$this->assertFalse(empty($value['asdf']));
$this->assertFalse(empty($value['foo']));
$result = $this->validator->isValid($value, new Collection(array(
'fields' => array(
'foo' => new NotBlank(),
'asdf' => new NotBlank()
)
)));
$this->assertTrue($result);
}
public function testArrayObject() {
$value = new \ArrayObject(array());
$value['foo'] = 12;
$value['asdf'] = 'asdfaf';
$this->assertTrue(isset($value['asdf']));
$this->assertTrue(isset($value['foo']));
$this->assertFalse(empty($value['asdf']));
$this->assertFalse(empty($value['foo']));
$result = $this->validator->isValid($value, new Collection(array(
'fields' => array(
'foo' => new NotBlank(),
'asdf' => new NotBlank()
)
)));
$this->assertTrue($result);
}
public function testObjectShouldBeLeftUnchanged()
@ -199,4 +324,34 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
'foo' => 3
), (array) $value);
}
public function getValidArguments()
{
return array(
// can only test for one entry, because PHPUnits mocking does not allow
// to expect multiple method calls with different arguments
array(array('foo' => 3)),
array(new \ArrayObject(array('foo' => 3))),
array(new TestArrayObject(array('foo' => 3))),
);
}
public function getArgumentsWithExtraFields()
{
return array(
array(array(
'foo' => 5,
'bar' => 6,
)),
array(new \ArrayObject(array(
'foo' => 5,
'bar' => 6,
))),
array(new TestArrayObject(array(
'foo' => 5,
'bar' => 6,
)))
);
}
}