Skip to content

Commit

Permalink
converted file_exists calls to either is_file or is_dir where it make…
Browse files Browse the repository at this point in the history
…s sense
  • Loading branch information
fabpot committed Aug 29, 2011
1 parent 4e70b1a commit 0eae562
Show file tree
Hide file tree
Showing 26 changed files with 46 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function warmUp($cacheDir)
{
foreach ($this->registry->getEntityManagers() as $em) {
// we need the directory no matter the proxy cache generation strategy
if (!file_exists($proxyCacheDir = $em->getConfiguration()->getProxyDir())) {
if (!is_dir($proxyCacheDir = $em->getConfiguration()->getProxyDir())) {
if (false === @mkdir($proxyCacheDir, 0777, true)) {
throw new \RuntimeException(sprintf('Unable to create the Doctrine Proxy directory "%s".', dirname($proxyCacheDir)));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Bridge/Doctrine/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function initialize()
$this->_classCache = array();
if (null !== $this->_globalBasename) {
foreach ($this->_paths as $path) {
if (file_exists($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) {
if (is_file($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) {
$this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
}
}
Expand All @@ -135,7 +135,7 @@ protected function _findMappingFile($className)
$defaultFileName = str_replace('\\', '.', $className).$this->_fileExtension;
foreach ($this->_paths as $path) {
if (!isset($this->_prefixes[$path])) {
if (file_exists($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
if (is_file($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
return $path.DIRECTORY_SEPARATOR.$defaultFileName;
}

Expand All @@ -149,7 +149,7 @@ protected function _findMappingFile($className)
}

$filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->_fileExtension;
if (file_exists($filename)) {
if (is_file($filename)) {
return $filename;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Bridge/Doctrine/Mapping/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function initialize()
$this->_classCache = array();
if (null !== $this->_globalBasename) {
foreach ($this->_paths as $path) {
if (file_exists($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) {
if (is_file($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) {
$this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
}
}
Expand All @@ -135,7 +135,7 @@ protected function _findMappingFile($className)
$defaultFileName = str_replace('\\', '.', $className).$this->_fileExtension;
foreach ($this->_paths as $path) {
if (!isset($this->_prefixes[$path])) {
if (file_exists($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
if (is_file($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
return $path.DIRECTORY_SEPARATOR.$defaultFileName;
}

Expand All @@ -149,7 +149,7 @@ protected function _findMappingFile($className)
}

$filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->_fileExtension;
if (file_exists($filename)) {
if (is_file($filename)) {
return $filename;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function loadMappingInformation(array $objectManager, ContainerBuilder
$mappingConfig['dir'] = $container->getParameterBag()->resolveValue($mappingConfig['dir']);
// a bundle configuration is detected by realizing that the specified dir is not absolute and existing
if (!isset($mappingConfig['is_bundle'])) {
$mappingConfig['is_bundle'] = !file_exists($mappingConfig['dir']);
$mappingConfig['is_bundle'] = !is_dir($mappingConfig['dir']);
}

if ($mappingConfig['is_bundle']) {
Expand Down Expand Up @@ -234,7 +234,7 @@ protected function assertValidMappingConfiguration(array $mappingConfig, $object
throw new \InvalidArgumentException(sprintf('Mapping definitions for Doctrine manager "%s" require at least the "type", "dir" and "prefix" options.', $objectManagerName));
}

if (!file_exists($mappingConfig['dir'])) {
if (!is_dir($mappingConfig['dir'])) {
throw new \InvalidArgumentException(sprintf('Specified non-existing directory "%s" as Doctrine mapping source.', $mappingConfig['dir']));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/DoctrineBundle/DoctrineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class_exists('Doctrine\ORM\Mapping\Driver\AnnotationDriver');
$className = substr($class, strlen($namespace) +1);
$file = $dir.DIRECTORY_SEPARATOR.$className.'.php';

if (!file_exists($file)) {
if (!is_file($file)) {
throw new \RuntimeException(sprintf('The proxy file "%s" does not exist. If you still have objects serialized in the session, you need to clear the session manually.', $file));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private function getContainerBuilder()
throw new \LogicException(sprintf('Debug information about the container is only available in debug mode.'));
}

if (!file_exists($cachedFile = $this->getContainer()->getParameter('debug.container.dump'))) {
if (!is_file($cachedFile = $this->getContainer()->getParameter('debug.container.dump'))) {
throw new \LogicException(sprintf('Debug information about the container could not be found. Please clear the cache and try again.'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ private function getValidatorXmlMappingFiles(ContainerBuilder $container)

foreach ($container->getParameter('kernel.bundles') as $bundle) {
$reflection = new \ReflectionClass($bundle);
if (file_exists($file = dirname($reflection->getFilename()).'/Resources/config/validation.xml')) {
if (is_file($file = dirname($reflection->getFilename()).'/Resources/config/validation.xml')) {
$files[] = realpath($file);
$container->addResource(new FileResource($file));
}
Expand All @@ -563,7 +563,7 @@ private function getValidatorYamlMappingFiles(ContainerBuilder $container)

foreach ($container->getParameter('kernel.bundles') as $bundle) {
$reflection = new \ReflectionClass($bundle);
if (file_exists($file = dirname($reflection->getFilename()).'/Resources/config/validation.yml')) {
if (is_file($file = dirname($reflection->getFilename()).'/Resources/config/validation.yml')) {
$files[] = realpath($file);
$container->addResource(new FileResource($file));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function formatFile($file, $line, $text = null)
*/
public function getFileLink($file, $line)
{
if ($this->fileLinkFormat && file_exists($file)) {
if ($this->fileLinkFormat && is_file($file)) {
return strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TemplateLocator implements FileLocatorInterface
*/
public function __construct(FileLocatorInterface $locator, $cacheDir = null)
{
if (null !== $cacheDir && file_exists($cache = $cacheDir.'/templates.php')) {
if (null !== $cacheDir && is_file($cache = $cacheDir.'/templates.php')) {
$this->cache = require $cache;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ static protected function getPhpUnitXmlDir()

$dir = static::getPhpUnitCliConfigArgument();
if ($dir === null &&
(file_exists(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml') ||
file_exists(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) {
(is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml') ||
is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) {
$dir = getcwd();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
while ($dir !== $lastDir) {
$lastDir = $dir;

if (file_exists($dir.'/autoload.php')) {
if (is_file($dir.'/autoload.php')) {
require_once $dir.'/autoload.php';
break;
}

if (file_exists($dir.'/autoload.php.dist')) {
if (is_file($dir.'/autoload.php.dist')) {
require_once $dir.'/autoload.php.dist';
break;
}
Expand Down Expand Up @@ -52,7 +52,7 @@ public function __construct($testCase, $rootConfig, $environment, $debug)
$this->testCase = $testCase;

$fs = new Filesystem();
if (!$fs->isAbsolutePath($rootConfig) && !file_exists($rootConfig = __DIR__.'/'.$testCase.'/'.$rootConfig)) {
if (!$fs->isAbsolutePath($rootConfig) && !is_file($rootConfig = __DIR__.'/'.$testCase.'/'.$rootConfig)) {
throw new \InvalidArgumentException(sprintf('The root config "%s" does not exist.', $rootConfig));
}
$this->rootConfig = $rootConfig;
Expand All @@ -62,7 +62,7 @@ public function __construct($testCase, $rootConfig, $environment, $debug)

public function registerBundles()
{
if (!file_exists($filename = $this->getRootDir().'/'.$this->testCase.'/bundles.php')) {
if (!is_file($filename = $this->getRootDir().'/'.$this->testCase.'/bundles.php')) {
throw new \RuntimeException(sprintf('The bundles file "%s" does not exist.', $filename));
}

Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/ClassLoader/ClassCollectionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static public function load($classes, $cacheDir, $name, $autoReload, $adaptive =
$reload = false;
if ($autoReload) {
$metadata = $cacheDir.'/'.$name.$extension.'.meta';
if (!file_exists($metadata) || !file_exists($cache)) {
if (!is_file($metadata) || !is_file($cache)) {
$reload = true;
} else {
$time = filemtime($cache);
Expand All @@ -65,7 +65,7 @@ static public function load($classes, $cacheDir, $name, $autoReload, $adaptive =
$reload = true;
} else {
foreach ($meta[0] as $resource) {
if (!file_exists($resource) || filemtime($resource) > $time) {
if (!is_file($resource) || filemtime($resource) > $time) {
$reload = true;

break;
Expand All @@ -75,7 +75,7 @@ static public function load($classes, $cacheDir, $name, $autoReload, $adaptive =
}
}

if (!$reload && file_exists($cache)) {
if (!$reload && is_file($cache)) {
require_once $cache;

return;
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/ClassLoader/UniversalClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ public function findFile($class)
foreach ($dirs as $dir) {
$className = substr($class, $pos + 1);
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
if (file_exists($file)) {
if (is_file($file)) {
return $file;
}
}
}

foreach ($this->namespaceFallbacks as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
if (is_file($file)) {
return $file;
}
}
Expand All @@ -248,15 +248,15 @@ public function findFile($class)

foreach ($dirs as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
if (is_file($file)) {
return $file;
}
}
}

foreach ($this->prefixFallbacks as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
if (is_file($file)) {
return $file;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Config/ConfigCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __toString()
*/
public function isFresh()
{
if (!file_exists($this->file)) {
if (!is_file($this->file)) {
return false;
}

Expand All @@ -65,7 +65,7 @@ public function isFresh()
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getResource()
*/
public function isFresh($timestamp)
{
if (!file_exists($this->resource)) {
if (!is_dir($this->resource)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/Resource/FileResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function getResource()
*/
public function isFresh($timestamp)
{
if (!file_exists($this->resource)) {
if (!is_file($this->resource)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private function validateSchema(\DOMDocument $dom, $file)
if (($extension = $this->container->getExtension($items[$i])) && false !== $extension->getXsdValidationBasePath()) {
$path = str_replace($extension->getNamespace(), str_replace('\\', '/', $extension->getXsdValidationBasePath()).'/', $items[$i + 1]);

if (!file_exists($path)) {
if (!is_file($path)) {
throw new \RuntimeException(sprintf('Extension "%s" references a non-existent XSD file "%s"', get_class($extension), $path));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function start()

$file = $this->path.'/'.session_id().'.session';

$this->data = file_exists($file) ? unserialize(file_get_contents($file)) : array();
$this->data = is_file($file) ? unserialize(file_get_contents($file)) : array();
$this->started = true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ protected function lock(Request $request, Response $entry)

// wait for the lock to be released
$wait = 0;
while (file_exists($lock) && $wait < 5000000) {
while (is_file($lock) && $wait < 5000000) {
usleep($wait += 50000);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/HttpKernel/HttpCache/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function lookup(Request $request)
}

list($req, $headers) = $match;
if (file_exists($body = $this->getPath($headers['x-content-digest'][0]))) {
if (is_file($body = $this->getPath($headers['x-content-digest'][0]))) {
return $this->restoreResponse($headers, $body);
}

Expand Down Expand Up @@ -282,7 +282,7 @@ private function getMetadata($key)
*/
public function purge($url)
{
if (file_exists($path = $this->getPath($this->getCacheKey(Request::create($url))))) {
if (is_file($path = $this->getPath($this->getCacheKey(Request::create($url))))) {
unlink($path);

return true;
Expand All @@ -302,7 +302,7 @@ private function load($key)
{
$path = $this->getPath($key);

return file_exists($path) ? file_get_contents($path) : false;
return is_file($path) ? file_get_contents($path) : false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public function getContainer()
*/
public function loadClassCache($name = 'classes', $extension = '.php')
{
if (!$this->booted && file_exists($this->getCacheDir().'/classes.map')) {
if (!$this->booted && is_file($this->getCacheDir().'/classes.map')) {
ClassCollectionLoader::load(include($this->getCacheDir().'/classes.map'), $this->getCacheDir(), $name, $this->debug, false, $extension);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpKernel/Util/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public function copy($originFile, $targetFile, $override = false)
$this->mkdir(dirname($targetFile));

$mostRecent = false;
if (file_exists($targetFile)) {
if (is_file($targetFile)) {
$statTarget = stat($targetFile);
$statOrigin = stat($originFile);
$mostRecent = $statOrigin['mtime'] > $statTarget['mtime'];
}

if ($override || !file_exists($targetFile) || $mostRecent) {
if ($override || !is_file($targetFile) || $mostRecent) {
copy($originFile, $targetFile);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Templating/Loader/CacheLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function load(TemplateReferenceInterface $template)
$file = substr($key, 2).'.tpl';
$path = $dir.DIRECTORY_SEPARATOR.$file;

if (file_exists($path)) {
if (is_file($path)) {
if (null !== $this->debugger) {
$this->debugger->log(sprintf('Fetching template "%s" from cache', $template->get('name')));
}
Expand All @@ -69,7 +69,7 @@ public function load(TemplateReferenceInterface $template)

$content = $storage->getContent();

if (!file_exists($dir)) {
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function load(TemplateReferenceInterface $template)
{
$file = $template->get('name');

if (self::isAbsolutePath($file) && file_exists($file)) {
if (self::isAbsolutePath($file) && is_file($file)) {
return new FileStorage($file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function isValid($value, Constraint $constraint)

$path = $value instanceof FileObject ? $value->getPathname() : (string) $value;

if (!file_exists($path)) {
if (!is_file($path)) {
$this->setMessage($constraint->notFoundMessage, array('{{ file }}' => $path));

return false;
Expand Down
Loading

7 comments on commit 0eae562

@craue
Copy link
Contributor

@craue craue commented on 0eae562 Aug 31, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit slows down my application by more than 100% on each request. c8d6c60 doesn't help.

@fabpot
Copy link
Member Author

@fabpot fabpot commented on 0eae562 Aug 31, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@craue: That's weird. Are you sure that the slowdown comes from this specific commit because is_file does the same amount of work as file_exists?

@craue
Copy link
Contributor

@craue craue commented on 0eae562 Aug 31, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabpot: Yes, I tracked the issue down to this commit. My first guess was that some replacements make the cache not being used properly.

@fabpot
Copy link
Member Author

@fabpot fabpot commented on 0eae562 Aug 31, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update to master as we have fixed a bug since then.

@craue
Copy link
Contributor

@craue craue commented on 0eae562 Aug 31, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabpot: Do you have c8d6c60 in mind? I tried it prior to commenting and it doesn't help.

@fabpot
Copy link
Member Author

@fabpot fabpot commented on 0eae562 Sep 16, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@craue: I think the slowdown (which only occurred in the dev environment I think) has been fixed now (see bede420). Can you confirm?

@craue
Copy link
Contributor

@craue craue commented on 0eae562 Sep 16, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabpot: Confirmed. Thank you.

Please sign in to comment.