Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DependencyInjection] refactored loaders
* refactored the import mechanism for better flexibility * added two methods to LoaderInterface: supports() and setResolver() * added a LoaderResolver interface * added a Loader base class * added new loaders: DelegatingLoader, PhpFileLoader, and ClosureLoader
- Loading branch information
Showing
21 changed files
with
775 additions
and
52 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/Symfony/Components/DependencyInjection/Loader/ClosureLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Symfony\Components\DependencyInjection\Loader; | ||
|
||
/* | ||
* This file is part of the Symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* ClosureLoader loads service definitions from a PHP closure. | ||
* | ||
* The Closure has access to the container as its first argument. | ||
* | ||
* @package Symfony | ||
* @subpackage Components_DependencyInjection | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
class ClosureLoader extends Loader | ||
{ | ||
/** | ||
* Loads a Closure. | ||
* | ||
* @param \Closure $resource The resource | ||
*/ | ||
public function load($closure) | ||
{ | ||
call_user_func($closure, $this->container); | ||
} | ||
|
||
/** | ||
* Returns true if this class supports the given resource. | ||
* | ||
* @param mixed $resource A resource | ||
* | ||
* @return Boolean true if this class supports the given resource, false otherwise | ||
*/ | ||
public function supports($resource) | ||
{ | ||
return $resource instanceof \Closure; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/Symfony/Components/DependencyInjection/Loader/DelegatingLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Symfony\Components\DependencyInjection\Loader; | ||
|
||
/* | ||
* This file is part of the Symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* DelegatingLoader delegates loading to other loaders using a loader resolver. | ||
* | ||
* @package Symfony | ||
* @subpackage Components_DependencyInjection | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
class DelegatingLoader extends Loader | ||
{ | ||
protected $resolver; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Symfony\Components\DependencyInjection\Loader\LoaderResolverInterface $resolver A LoaderResolverInterface instance | ||
*/ | ||
public function __construct(LoaderResolverInterface $resolver) | ||
{ | ||
$this->resolver = $resolver; | ||
} | ||
|
||
/** | ||
* Loads a resource. | ||
* | ||
* @param mixed $resource A resource | ||
*/ | ||
public function load($resource) | ||
{ | ||
$loader = $this->resolver->resolve($resource); | ||
|
||
if (false === $loader) { | ||
throw new \InvalidArgumentException(sprintf('Unable to load the "%s" container resource.', is_string($resource) ? $resource : (is_object($resource) ? get_class($resource) : 'RESOURCE'))); | ||
} | ||
|
||
return $loader->load($resource); | ||
} | ||
|
||
/** | ||
* Returns true if this class supports the given resource. | ||
* | ||
* @param mixed $resource A resource | ||
* | ||
* @return Boolean true if this class supports the given resource, false otherwise | ||
*/ | ||
public function supports($resource) | ||
{ | ||
foreach ($this->resolver->getLoaders() as $loader) { | ||
if ($loader->supports($resource)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/Symfony/Components/DependencyInjection/Loader/LoaderResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Symfony\Components\DependencyInjection\Loader; | ||
|
||
/* | ||
* This file is part of the Symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* LoaderResolver selects a loader for a given resource.. | ||
* | ||
* @package Symfony | ||
* @subpackage Components_DependencyInjection | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
class LoaderResolver implements LoaderResolverInterface | ||
{ | ||
protected $loaders; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Symfony\Components\DependencyInjection\Loader\LoaderInterface[] $loaders An array of loaders | ||
*/ | ||
public function __construct(array $loaders = array()) | ||
{ | ||
$this->loaders = array(); | ||
foreach ($loaders as $loader) { | ||
$this->addLoader($loader); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a loader able to load the resource. | ||
* | ||
* @param mixed $resource A resource | ||
* | ||
* @return Symfony\Components\DependencyInjection\Loader\LoaderInterface A LoaderInterface instance | ||
*/ | ||
public function resolve($resource) | ||
{ | ||
foreach ($this->loaders as $loader) { | ||
if ($loader->supports($resource)) { | ||
return $loader; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Sets a loader. | ||
* | ||
* @param \Symfony\Components\DependencyInjection\Loader\LoaderInterface $loader A LoaderInterface instance | ||
*/ | ||
public function addLoader(LoaderInterface $loader) | ||
{ | ||
$this->loaders[] = $loader; | ||
$loader->setResolver($this); | ||
} | ||
|
||
/** | ||
* Returns the registered loaders. | ||
* | ||
* @return \Symfony\Components\DependencyInjection\Loader\LoaderInterface[] A array of LoaderInterface instances | ||
*/ | ||
public function getLoaders() | ||
{ | ||
return $this->loaders; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Components/DependencyInjection/Loader/LoaderResolverInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Symfony\Components\DependencyInjection\Loader; | ||
|
||
/* | ||
* This file is part of the Symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* LoaderResolverInterface selects a loader for a given resource. | ||
* | ||
* @package Symfony | ||
* @subpackage Components_DependencyInjection | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
interface LoaderResolverInterface | ||
{ | ||
/** | ||
* Returns a loader able to load the resource. | ||
* | ||
* @param mixed $resource A resource | ||
* | ||
* @return Symfony\Components\DependencyInjection\Loader\LoaderInterface A LoaderInterface instance | ||
*/ | ||
function resolve($resource); | ||
} |
Oops, something went wrong.