2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\DependencyInjection\Loader;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
2010-07-16 08:12:58 +01:00
|
|
|
|
2010-01-04 14:26:20 +00:00
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
|
|
|
* (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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FileLoader is the abstract class used by all built-in loaders that are file based.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
abstract class FileLoader extends Loader
|
|
|
|
{
|
2010-07-18 10:26:47 +01:00
|
|
|
protected $currentDir;
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $paths;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance
|
|
|
|
* @param string|array $paths A path or an array of paths where to look for resources
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-07-16 08:12:58 +01:00
|
|
|
public function __construct(ContainerBuilder $container, $paths = array())
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-07-16 08:12:58 +01:00
|
|
|
parent::__construct($container);
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!is_array($paths)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$paths = array($paths);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->paths = $paths;
|
2010-01-17 10:21:31 +00:00
|
|
|
}
|
|
|
|
|
2010-07-18 10:26:47 +01:00
|
|
|
/**
|
|
|
|
* Adds definitions and parameters from a resource.
|
|
|
|
*
|
|
|
|
* @param mixed $resource A Resource
|
|
|
|
*/
|
2010-08-24 15:36:29 +01:00
|
|
|
public function import($resource, $ignoreErrors = false)
|
2010-07-18 10:26:47 +01:00
|
|
|
{
|
2010-08-24 15:36:29 +01:00
|
|
|
try {
|
|
|
|
$loader = $this->resolve($resource);
|
2010-07-18 10:26:47 +01:00
|
|
|
|
2010-08-24 15:36:29 +01:00
|
|
|
if ($loader instanceof FileLoader && null !== $this->currentDir) {
|
|
|
|
$resource = $this->getAbsolutePath($resource, $this->currentDir);
|
|
|
|
}
|
2010-07-18 10:26:47 +01:00
|
|
|
|
2010-08-24 15:36:29 +01:00
|
|
|
$loader->load($resource);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
if (!$ignoreErrors) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2010-07-18 10:26:47 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException When provided file does not exist
|
|
|
|
*/
|
|
|
|
protected function findFile($file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$path = $this->getAbsolutePath($file);
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!file_exists($path)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist (in: %s).', $file, implode(', ', $this->paths)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
protected function getAbsolutePath($file, $currentPath = null)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (self::isAbsolutePath($file)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $file;
|
2010-05-07 15:09:11 +01:00
|
|
|
} else if (null !== $currentPath && file_exists($currentPath.DIRECTORY_SEPARATOR.$file)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $currentPath.DIRECTORY_SEPARATOR.$file;
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-08 14:32:30 +01:00
|
|
|
foreach ($this->paths as $path) {
|
2010-05-07 15:09:11 +01:00
|
|
|
if (file_exists($path.DIRECTORY_SEPARATOR.$file)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $path.DIRECTORY_SEPARATOR.$file;
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $file;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
static protected function isAbsolutePath($file)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
'/' == $file[0]
|
|
|
|
||
|
|
|
|
'\\' == $file[0]
|
|
|
|
||
|
|
|
|
(
|
|
|
|
3 < strlen($file)
|
|
|
|
&&
|
|
|
|
ctype_alpha($file[0])
|
|
|
|
&&
|
|
|
|
':' == $file[1]
|
|
|
|
&&
|
|
|
|
(
|
|
|
|
'\\' == $file[2]
|
|
|
|
||
|
|
|
|
'/' == $file[2]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|