Skip to content

Commit

Permalink
Merge branch '2.6' into 2.7
Browse files Browse the repository at this point in the history
Conflicts:
	src/Symfony/Component/PropertyAccess/PropertyAccessor.php
	src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php
  • Loading branch information
Tobion committed Feb 21, 2015
2 parents b2ba690 + a3ac7ac commit 4b9ea2c
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 141 deletions.
40 changes: 32 additions & 8 deletions Mapping/Loader/AbstractLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,39 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\MappingException;

/**
* Base loader for validation metadata.
*
* This loader supports the loading of constraints from Symfony's default
* namespace (see {@link DEFAULT_NAMESPACE}) using the short class names of
* those constraints. Constraints can also be loaded using their fully
* qualified class names. At last, namespace aliases can be defined to load
* constraints with the syntax "alias:ShortName".
*
* @author Bernhard Schussek <[email protected]>
*/
abstract class AbstractLoader implements LoaderInterface
{
/**
* Contains all known namespaces indexed by their prefix.
*
* The namespace to load constraints from by default.
*/
const DEFAULT_NAMESPACE = '\\Symfony\\Component\\Validator\\Constraints\\';

/**
* @var array
*/
protected $namespaces = array();

/**
* Adds a namespace alias.
*
* The namespace alias can be used to reference constraints from specific
* namespaces in {@link newConstraint()}:
*
* $this->addNamespaceAlias('mynamespace', '\\Acme\\Package\\Constraints\\');
*
* $constraint = $this->newConstraint('mynamespace:NotNull');
*
* @param string $alias The alias
* @param string $namespace The PHP namespace
*/
Expand All @@ -37,16 +58,19 @@ protected function addNamespaceAlias($alias, $namespace)
/**
* Creates a new constraint instance for the given constraint name.
*
* @param string $name The constraint name. Either a constraint relative
* to the default constraint namespace, or a fully
* qualified class name
* @param mixed $options The constraint options
* @param string $name The constraint name. Either a constraint relative
* to the default constraint namespace, or a fully
* qualified class name. Alternatively, the constraint
* may be preceded by a namespace alias and a colon.
* The namespace alias must have been defined using
* {@link addNamespaceAlias()}.
* @param mixed $options The constraint options
*
* @return Constraint
*
* @throws MappingException If the namespace prefix is undefined
*/
protected function newConstraint($name, $options)
protected function newConstraint($name, $options = null)
{
if (strpos($name, '\\') !== false && class_exists($name)) {
$className = (string) $name;
Expand All @@ -59,7 +83,7 @@ protected function newConstraint($name, $options)

$className = $this->namespaces[$prefix].$className;
} else {
$className = 'Symfony\\Component\\Validator\\Constraints\\'.$name;
$className = self::DEFAULT_NAMESPACE.$name;
}

return new $className($options);
Expand Down
18 changes: 13 additions & 5 deletions Mapping/Loader/AnnotationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@
use Symfony\Component\Validator\Exception\MappingException;
use Symfony\Component\Validator\Mapping\ClassMetadata;

/**
* Loads validation metadata using a Doctrine annotation {@link Reader}.
*
* @author Bernhard Schussek <[email protected]>
*/
class AnnotationLoader implements LoaderInterface
{
/**
* @var Reader
*/
protected $reader;

public function __construct(Reader $reader)
Expand All @@ -35,7 +43,7 @@ public function loadClassMetadata(ClassMetadata $metadata)
{
$reflClass = $metadata->getReflectionClass();
$className = $reflClass->name;
$loaded = false;
$success = false;

foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) {
if ($constraint instanceof GroupSequence) {
Expand All @@ -46,7 +54,7 @@ public function loadClassMetadata(ClassMetadata $metadata)
$metadata->addConstraint($constraint);
}

$loaded = true;
$success = true;
}

foreach ($reflClass->getProperties() as $property) {
Expand All @@ -56,7 +64,7 @@ public function loadClassMetadata(ClassMetadata $metadata)
$metadata->addPropertyConstraint($property->name, $constraint);
}

$loaded = true;
$success = true;
}
}
}
Expand All @@ -77,11 +85,11 @@ public function loadClassMetadata(ClassMetadata $metadata)
}
}

$loaded = true;
$success = true;
}
}
}

return $loaded;
return $success;
}
}
26 changes: 21 additions & 5 deletions Mapping/Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,42 @@

use Symfony\Component\Validator\Exception\MappingException;

/**
* Base loader for loading validation metadata from a file.
*
* @author Bernhard Schussek <[email protected]>
*
* @see YamlFileLoader
* @see XmlFileLoader
*/
abstract class FileLoader extends AbstractLoader
{
/**
* The file to load.
*
* @var string
*/
protected $file;

/**
* Constructor.
* Creates a new loader.
*
* @param string $file The mapping file to load
*
* @throws MappingException if the mapping file does not exist
* @throws MappingException if the mapping file is not readable
* @throws MappingException If the file does not exist or is not readable
*/
public function __construct($file)
{
if (!is_file($file)) {
throw new MappingException(sprintf('The mapping file %s does not exist', $file));
throw new MappingException(sprintf('The mapping file "%s" does not exist', $file));
}

if (!is_readable($file)) {
throw new MappingException(sprintf('The mapping file %s is not readable', $file));
throw new MappingException(sprintf('The mapping file "%s" is not readable', $file));
}

if (!stream_is_local($this->file)) {
throw new MappingException(sprintf('The mapping file "%s" is not a local file', $file));
}

$this->file = $file;
Expand Down
28 changes: 14 additions & 14 deletions Mapping/Loader/FilesLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@
namespace Symfony\Component\Validator\Mapping\Loader;

/**
* Creates mapping loaders for array of files.
*
* Abstract class, used by
* Base loader for loading validation metadata from a list of files.
*
* @author Bulat Shakirzyanov <[email protected]>
* @author Bernhard Schussek <[email protected]>
*
* @see YamlFileLoader
* @see XmlFileLoader
* @see YamlFilesLoader
* @see XmlFilesLoader
*/
abstract class FilesLoader extends LoaderChain
{
/**
* Array of mapping files.
* Creates a new loader.
*
* @param array $paths Array of file paths
* @param array $paths An array of file paths
*/
public function __construct(array $paths)
{
parent::__construct($this->getFileLoaders($paths));
}

/**
* Array of mapping files.
* Returns an array of file loaders for the given file paths.
*
* @param array $paths Array of file paths
* @param array $paths An array of file paths
*
* @return LoaderInterface[] Array of metadata loaders
* @return LoaderInterface[] The metadata loaders
*/
protected function getFileLoaders($paths)
{
$loaders = array();

foreach ($paths as $path) {
$loaders[] = $this->getFileLoaderInstance($path);
}
Expand All @@ -51,11 +51,11 @@ protected function getFileLoaders($paths)
}

/**
* Takes mapping file path.
* Creates a loader for the given file path.
*
* @param string $file
* @param string $path The file path
*
* @return LoaderInterface
* @return LoaderInterface The created loader
*/
abstract protected function getFileLoaderInstance($file);
abstract protected function getFileLoaderInstance($path);
}
18 changes: 9 additions & 9 deletions Mapping/Loader/LoaderChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@
use Symfony\Component\Validator\Mapping\ClassMetadata;

/**
* Calls multiple LoaderInterface instances in a chain.
* Loads validation metadata from multiple {@link LoaderInterface} instances.
*
* This class accepts multiple instances of LoaderInterface to be passed to the
* constructor. When loadClassMetadata() is called, the same method is called
* in <em>all</em> of these loaders, regardless of whether any of them was
* successful or not.
* Pass the loaders when constructing the chain. Once
* {@link loadClassMetadata()} is called, that method will be called on all
* loaders in the chain.
*
* @author Bernhard Schussek <[email protected]>
*/
class LoaderChain implements LoaderInterface
{
/**
* @var LoaderInterface[]
*/
protected $loaders;

/**
* Accepts a list of LoaderInterface instances.
*
* @param LoaderInterface[] $loaders An array of LoaderInterface instances
* @param LoaderInterface[] $loaders The metadata loaders to use
*
* @throws MappingException If any of the loaders does not implement LoaderInterface
* @throws MappingException If any of the loaders has an invalid type
*/
public function __construct(array $loaders)
{
Expand Down
11 changes: 8 additions & 3 deletions Mapping/Loader/LoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@

use Symfony\Component\Validator\Mapping\ClassMetadata;

/**
* Loads validation metadata into {@link ClassMetadata} instances.
*
* @author Bernhard Schussek <[email protected]>
*/
interface LoaderInterface
{
/**
* Load a Class Metadata.
* Loads validation metadata into a {@link ClassMetadata} instance.
*
* @param ClassMetadata $metadata A metadata
* @param ClassMetadata $metadata The metadata to load
*
* @return bool
* @return bool Whether the loader succeeded
*/
public function loadClassMetadata(ClassMetadata $metadata);
}
15 changes: 15 additions & 0 deletions Mapping/Loader/StaticMethodLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@
use Symfony\Component\Validator\Exception\MappingException;
use Symfony\Component\Validator\Mapping\ClassMetadata;

/**
* Loads validation metadata by calling a static method on the loaded class.
*
* @author Bernhard Schussek <[email protected]>
*/
class StaticMethodLoader implements LoaderInterface
{
/**
* The name of the method to call.
*
* @var string
*/
protected $methodName;

/**
* Creates a new loader.
*
* @param string $methodName The name of the static method to call
*/
public function __construct($methodName = 'loadValidatorMetadata')
{
$this->methodName = $methodName;
Expand Down
Loading

0 comments on commit 4b9ea2c

Please sign in to comment.