Browse Source

Speed up file existence checks where feasible.

file_exists() is a lot slower than is_file() / is_dir().
Refs: https://bugs.php.net/bug.php?id=78285
ADmad 5 years ago
parent
commit
f3c41deaf4

+ 1 - 1
src/Cache/Engine/FileEngine.php

@@ -365,7 +365,7 @@ class FileEngine extends CacheEngine
             $this->_File->getBasename() !== $key ||
             $this->_File->getBasename() !== $key ||
             $this->_File->valid() === false
             $this->_File->valid() === false
         ) {
         ) {
-            $exists = file_exists($path->getPathname());
+            $exists = is_file($path->getPathname());
             try {
             try {
                 $this->_File = $path->openFile('c+');
                 $this->_File = $path->openFile('c+');
             } catch (Exception $e) {
             } catch (Exception $e) {

+ 2 - 2
src/Core/BasePlugin.php

@@ -257,7 +257,7 @@ class BasePlugin implements PluginInterface
     public function routes(RouteBuilder $routes): void
     public function routes(RouteBuilder $routes): void
     {
     {
         $path = $this->getConfigPath() . 'routes.php';
         $path = $this->getConfigPath() . 'routes.php';
-        if (file_exists($path)) {
+        if (is_file($path)) {
             require $path;
             require $path;
         }
         }
     }
     }
@@ -268,7 +268,7 @@ class BasePlugin implements PluginInterface
     public function bootstrap(PluginApplicationInterface $app): void
     public function bootstrap(PluginApplicationInterface $app): void
     {
     {
         $bootstrap = $this->getConfigPath() . 'bootstrap.php';
         $bootstrap = $this->getConfigPath() . 'bootstrap.php';
-        if (file_exists($bootstrap)) {
+        if (is_file($bootstrap)) {
             require $bootstrap;
             require $bootstrap;
         }
         }
     }
     }

+ 1 - 1
src/Core/Configure.php

@@ -421,7 +421,7 @@ class Configure
         }
         }
 
 
         $path = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'config/config.php';
         $path = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'config/config.php';
-        if (file_exists($path)) {
+        if (is_file($path)) {
             $config = require $path;
             $config = require $path;
             static::write($config);
             static::write($config);
 
 

+ 2 - 2
src/Core/PluginCollection.php

@@ -94,9 +94,9 @@ class PluginCollection implements Iterator, Countable
             return;
             return;
         }
         }
         $vendorFile = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'cakephp-plugins.php';
         $vendorFile = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'cakephp-plugins.php';
-        if (!file_exists($vendorFile)) {
+        if (!is_file($vendorFile)) {
             $vendorFile = dirname(dirname(dirname(dirname(__DIR__)))) . DIRECTORY_SEPARATOR . 'cakephp-plugins.php';
             $vendorFile = dirname(dirname(dirname(dirname(__DIR__)))) . DIRECTORY_SEPARATOR . 'cakephp-plugins.php';
-            if (!file_exists($vendorFile)) {
+            if (!is_file($vendorFile)) {
                 Configure::write(['plugins' => []]);
                 Configure::write(['plugins' => []]);
 
 
                 return;
                 return;

+ 2 - 2
src/Log/Engine/FileLog.php

@@ -133,7 +133,7 @@ class FileLog extends BaseLog
             return;
             return;
         }
         }
 
 
-        $exists = file_exists($pathname);
+        $exists = is_file($pathname);
         file_put_contents($pathname, $output, FILE_APPEND);
         file_put_contents($pathname, $output, FILE_APPEND);
         static $selfError = false;
         static $selfError = false;
 
 
@@ -184,7 +184,7 @@ class FileLog extends BaseLog
         clearstatcache(true, $filePath);
         clearstatcache(true, $filePath);
 
 
         if (
         if (
-            !file_exists($filePath) ||
+            !is_file($filePath) ||
             filesize($filePath) < $this->_size
             filesize($filePath) < $this->_size
         ) {
         ) {
             return null;
             return null;

+ 4 - 4
src/Routing/Asset.php

@@ -249,7 +249,7 @@ class Asset
                 urldecode($path)
                 urldecode($path)
             );
             );
             $webrootPath = Configure::read('App.wwwRoot') . str_replace('/', DIRECTORY_SEPARATOR, $filepath);
             $webrootPath = Configure::read('App.wwwRoot') . str_replace('/', DIRECTORY_SEPARATOR, $filepath);
-            if (file_exists($webrootPath)) {
+            if (is_file($webrootPath)) {
                 return $path . '?' . filemtime($webrootPath);
                 return $path . '?' . filemtime($webrootPath);
             }
             }
             // Check for plugins and org prefixed plugins.
             // Check for plugins and org prefixed plugins.
@@ -265,7 +265,7 @@ class Asset
                     . 'webroot'
                     . 'webroot'
                     . DIRECTORY_SEPARATOR
                     . DIRECTORY_SEPARATOR
                     . implode(DIRECTORY_SEPARATOR, $segments);
                     . implode(DIRECTORY_SEPARATOR, $segments);
-                if (file_exists($pluginPath)) {
+                if (is_file($pluginPath)) {
                     return $path . '?' . filemtime($pluginPath);
                     return $path . '?' . filemtime($pluginPath);
                 }
                 }
             }
             }
@@ -304,12 +304,12 @@ class Asset
                 $file = str_replace('/', '\\', $file);
                 $file = str_replace('/', '\\', $file);
             }
             }
 
 
-            if (file_exists(Configure::read('App.wwwRoot') . $theme . $file)) {
+            if (is_file(Configure::read('App.wwwRoot') . $theme . $file)) {
                 $webPath = $requestWebroot . $theme . $asset[0];
                 $webPath = $requestWebroot . $theme . $asset[0];
             } else {
             } else {
                 $themePath = Plugin::path($themeName);
                 $themePath = Plugin::path($themeName);
                 $path = $themePath . 'webroot/' . $file;
                 $path = $themePath . 'webroot/' . $file;
-                if (file_exists($path)) {
+                if (is_file($path)) {
                     $webPath = $requestWebroot . $theme . $asset[0];
                     $webPath = $requestWebroot . $theme . $asset[0];
                 }
                 }
             }
             }

+ 1 - 1
src/Routing/Middleware/AssetMiddleware.php

@@ -73,7 +73,7 @@ class AssetMiddleware implements MiddlewareInterface
         }
         }
 
 
         $assetFile = $this->_getAssetFile($url);
         $assetFile = $this->_getAssetFile($url);
-        if ($assetFile === null || !file_exists($assetFile)) {
+        if ($assetFile === null || !is_file($assetFile)) {
             return $handler->handle($request);
             return $handler->handle($request);
         }
         }
 
 

+ 3 - 3
src/View/View.php

@@ -1346,7 +1346,7 @@ class View implements EventDispatcherInterface
         $name .= $this->_ext;
         $name .= $this->_ext;
         $paths = $this->_paths($plugin);
         $paths = $this->_paths($plugin);
         foreach ($paths as $path) {
         foreach ($paths as $path) {
-            if (file_exists($path . $name)) {
+            if (is_file($path . $name)) {
                 return $this->_checkFilePath($path . $name, $path);
                 return $this->_checkFilePath($path . $name, $path);
             }
             }
         }
         }
@@ -1440,7 +1440,7 @@ class View implements EventDispatcherInterface
         $name .= $this->_ext;
         $name .= $this->_ext;
 
 
         foreach ($this->getLayoutPaths($plugin) as $path) {
         foreach ($this->getLayoutPaths($plugin) as $path) {
-            if (file_exists($path . $name)) {
+            if (is_file($path . $name)) {
                 return $this->_checkFilePath($path . $name, $path);
                 return $this->_checkFilePath($path . $name, $path);
             }
             }
         }
         }
@@ -1483,7 +1483,7 @@ class View implements EventDispatcherInterface
 
 
         $name .= $this->_ext;
         $name .= $this->_ext;
         foreach ($this->getElementPaths($plugin) as $path) {
         foreach ($this->getElementPaths($plugin) as $path) {
-            if (file_exists($path . $name)) {
+            if (is_file($path . $name)) {
                 return $path . $name;
                 return $path . $name;
             }
             }
         }
         }