Skip to content

Commit

Permalink
moved common configuration classes to a new Config component
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Feb 13, 2011
1 parent d01a4e3 commit fa11ec6
Show file tree
Hide file tree
Showing 2,692 changed files with 217 additions and 144,022 deletions.
3 changes: 0 additions & 3 deletions .gitignore

This file was deleted.

126 changes: 126 additions & 0 deletions ConfigCache.php
@@ -0,0 +1,126 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config;

/**
* ConfigCache manages PHP cache files.
*
* When debug is enabled, it knows when to flush the cache
* thanks to an array of ResourceInterface instances.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class ConfigCache
{
protected $debug;
protected $cacheDir;
protected $file;

/**
* Constructor.
*
* @param string $cacheDir The cache directory
* @param string $file The cache file name (without the .php extension)
* @param Boolean $debug Whether debugging is enabled or not
*/
public function __construct($cacheDir, $file, $debug)
{
$this->cacheDir = $cacheDir;
$this->file = $file;
$this->debug = (Boolean) $debug;
}

/**
* Gets the cache file path.
*
* @return string The cache file path
*/
public function __toString()
{
return $this->cacheDir.'/'.$this->file.'.php';
}

/**
* Checks if the cache is still fresh.
*
* This method always returns true is debug is on and the cache file exists.
*
* @return Boolean true if the cache is fresh, false otherwise
*/
public function isFresh()
{
$file = $this->getCacheFile();
if (!file_exists($file)) {
return false;
}

if (!$this->debug) {
return true;
}

$metadata = $this->getCacheFile('meta');
if (!file_exists($metadata)) {
return false;
}

$time = filemtime($file);
$meta = unserialize(file_get_contents($metadata));
foreach ($meta as $resource) {
if (!$resource->isFresh($time)) {
return false;
}
}

return true;
}

/**
* Writes cache.
*
* @param string $content The content to write in the cache
* @param array $metadata An array of ResourceInterface instances
*
* @throws \RuntimeException When cache file can't be wrote
*/
public function write($content, array $metadata = null)
{
$file = $this->getCacheFile();
$dir = dirname($file);
if (!is_dir($dir)) {
if (false === @mkdir($dir, 0777, true)) {
throw new \RuntimeException(sprintf('Unable to create the %s directory', $dir));
}
} elseif (!is_writable($dir)) {
throw new \RuntimeException(sprintf('Unable to write in the %s directory', $dir));
}

$tmpFile = tempnam(dirname($file), basename($file));
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
chmod($file, 0666);
} else {
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $this->file));
}

if (null !== $metadata && true === $this->debug) {
$file = $this->getCacheFile('meta');
$tmpFile = tempnam(dirname($file), basename($file));
if (false !== @file_put_contents($tmpFile, serialize($metadata)) && @rename($tmpFile, $file)) {
chmod($file, 0666);
}
}
}

protected function getCacheFile($extension = 'php')
{
return $this->cacheDir.'/'.$this->file.'.'.$extension;
}
}
55 changes: 24 additions & 31 deletions .../Component/Routing/Loader/FileLocator.php → FileLocator.php
Expand Up @@ -9,14 +9,14 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\Routing\Loader;
namespace Symfony\Component\Config;

/**
* FileLocator uses an array of pre-defined paths to find files.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class FileLocator
class FileLocator implements FileLocatorInterface
{
protected $paths;

Expand All @@ -34,48 +34,41 @@ public function __construct($paths = array())
}

/**
* Returns a full path for a given file.
* Returns a full path for a given file name.
*
* @param string $file A file path
* @param mixed $name The file name to locate
* @param string $currentPath The current path
*
* @return string The full path for the file
*
* @throws \InvalidArgumentException When file is not found
*/
public function locate($file, $currentPath = null)
public function locate($name, $currentPath = null, $first = true)
{
$path = $this->getAbsolutePath($file, $currentPath);
if (!file_exists($path)) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist (in: %s).', $file, implode(', ', $this->paths)));
if ($this->isAbsolutePath($name)) {
if (!file_exists($name)) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $name));
}

return $name;
}

return $path;
}
$filepaths = array();
if (null !== $currentPath && file_exists($currentPath.DIRECTORY_SEPARATOR.$name)) {
$filepaths[] = $currentPath.DIRECTORY_SEPARATOR.$name;
}

/**
* Gets the absolute path for the file path if possible.
*
* @param string $file A file path
* @param string $currentPath The current path
*
* @return string
*/
public function getAbsolutePath($file, $currentPath = null)
{
if ($this->isAbsolutePath($file)) {
return $file;
} else if (null !== $currentPath && file_exists($currentPath.DIRECTORY_SEPARATOR.$file)) {
return $currentPath.DIRECTORY_SEPARATOR.$file;
} else {
foreach ($this->paths as $path) {
if (file_exists($path.DIRECTORY_SEPARATOR.$file)) {
return $path.DIRECTORY_SEPARATOR.$file;
}
foreach ($this->paths as $path) {
if (file_exists($path.DIRECTORY_SEPARATOR.$name)) {
$filepaths[] = $path.DIRECTORY_SEPARATOR.$name;
}
}

return $file;
if (!$filepaths) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist (in: %s%s).', $name, null !== $currentPath ? $currentPath.', ' : '', implode(', ', $this->paths)));
}

return true === $first ? $filepaths[0] : $filepaths;
}

/**
Expand All @@ -85,7 +78,7 @@ public function getAbsolutePath($file, $currentPath = null)
*
* @return Boolean
*/
public function isAbsolutePath($file)
protected function isAbsolutePath($file)
{
if ($file[0] == '/' || $file[0] == '\\'
|| (strlen($file) > 3 && ctype_alpha($file[0])
Expand Down
30 changes: 30 additions & 0 deletions FileLocatorInterface.php
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config;

/**
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
interface FileLocatorInterface
{
/**
* Returns a full path for a given file name.
*
* @param mixed $name The file name to locate
* @param string $currentPath The current path
*
* @return string The full path for the file
*
* @throws \InvalidArgumentException When file is not found
*/
function locate($name, $currentPath = null, $first = true);
}
19 changes: 0 additions & 19 deletions LICENSE

This file was deleted.

Expand Up @@ -9,12 +9,10 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\Routing\Loader;

use Symfony\Component\Routing\RouteCollection;
namespace Symfony\Component\Config\Loader;

/**
* DelegatingLoader delegates route loading to other loaders using a loader resolver.
* DelegatingLoader delegates loading to other loaders using a loader resolver.
*
* This loader acts as an array of LoaderInterface objects - each having
* a chance to load a given resource (handled by the resolver)
Expand Down Expand Up @@ -43,17 +41,13 @@ public function __construct(LoaderResolverInterface $resolver)
*
* @param mixed $resource A resource
* @param string $type The resource type
*
* @return RouteCollection A RouteCollection instance
*
* @throws \InvalidArgumentException When the resource cannot be loaded
*/
public function load($resource, $type = null)
{
$loader = $this->resolver->resolve($resource, $type);

if (false === $loader) {
throw new \InvalidArgumentException(sprintf('Unable to load the "%s" routing resource.', is_string($resource) ? $resource : (is_object($resource) ? get_class($resource) : 'RESOURCE')));
throw new \InvalidArgumentException(sprintf('Unable to load the "%s" resource.', is_string($resource) ? $resource : (is_object($resource) ? get_class($resource) : 'RESOURCE')));
}

return $loader->load($resource, $type);
Expand All @@ -65,7 +59,7 @@ public function load($resource, $type = null)
* @param mixed $resource A resource
* @param string $type The resource type
*
* @return Boolean True if this class supports the given resource, false otherwise
* @return Boolean true if this class supports the given resource, false otherwise
*/
public function supports($resource, $type = null)
{
Expand Down
Expand Up @@ -9,9 +9,9 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Loader;
namespace Symfony\Component\Config\Loader;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocatorInterface;

/**
* FileLoader is the abstract class used by all built-in loaders that are file based.
Expand All @@ -25,14 +25,10 @@ abstract class FileLoader extends Loader

/**
* Constructor.
*
* @param ContainerBuilder $container A ContainerBuilder instance
*/
public function __construct(ContainerBuilder $container, FileLocator $locator)
public function __construct(FileLocatorInterface $locator)
{
$this->locator = $locator;

parent::__construct($container);
}

public function getLocator()
Expand All @@ -43,18 +39,22 @@ public function getLocator()
/**
* Adds definitions and parameters from a resource.
*
* @param mixed $resource A Resource
* @param mixed $resource A Resource
* @param string $type The resource type
* @param Boolean $ignoreErrors Whether to ignore import errors or not
*
* @return mixed
*/
public function import($resource, $ignoreErrors = false)
public function import($resource, $type = null, $ignoreErrors = false)
{
try {
$loader = $this->resolve($resource);
$loader = $this->resolve($resource, $type);

if ($loader instanceof FileLoader && null !== $this->currentDir) {
$resource = $this->locator->locate($resource, $this->currentDir);
}

$loader->load($resource);
return $loader->load($resource);
} catch (\Exception $e) {
if (!$ignoreErrors) {
throw $e;
Expand Down

0 comments on commit fa11ec6

Please sign in to comment.