PHP Xdebug、AngularJS 和 PhpStorm 不应出现的警告
PHP warnings that should not be occurring with Xdebug, AngularJS, and PhpStorm
这是一个令人头疼的问题。我在服务器端继承了一个由 PHP 支持的 AngularJS 项目。我的任务是编写一个小的数据缓存函数,它非常简单。不幸的是,那是前端出现问题的时候。在我继续之前,我将展示我的 PHP 代码,以便我可以指出一些事情。文件结构和其他分类名称因保密协议而更改,与问题无关的代码已删除:
define('ROOT', dirname(__FILE__));
require('config/config.php');
require('includes/includes.php');
// caching
function cacheCategories()
{
$dirPath = ROOT . '/cache/categories';
$cacheFileName = '/categories.json';
$cacheFilePath = $dirPath . $cacheFileName;
$cacheTime = 86400;
if (file_exists($cacheFilePath) && (time() - $cacheTime) < filemtime($cacheFilePath)) { // cache file exists and is fresh
$cachedJSON = readfile($cacheFilePath);
if ($cachedJSON === false) {
throw new Exception('Could not read from cache file');
}
} else {
include_once('/site/load.php');
$categories = load_categories_tree();
$cachedJSON = json_encode($categories);
if (!is_dir($dirPath)) {
mkdir($dirPath);
}
$fp = fopen($cacheFilePath, 'w');
if ($fp === false) {
throw new Exception('Could not open cache file');
}
if (flock($fp, LOCK_EX)) {
$writeResult = fwrite($fp, $cachedJSON);
if ($writeResult === false) {
throw new Exception('Could not write to cache');
}
flock($fp, LOCK_UN);
fclose($fp);
}
}
return $cachedJSON;
}
global $page;
$page = new Page('');
$page->setDefaultLanguage('en');
echo cacheCategories();
如您所见,它非常简单。如果一个缓存文件存在,并且是新的,数据只是从中加载。如果没有,则创建一个新的缓存文件。必要时创建缓存目录。
我的问题是,当 AngularJS 试图从这个 PHP 文件中获取数据时,xdebug 生成了一个关于 mkdir
的警告,我也得到了一个通用的 [=15] =]:
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mkdir(): No such file or directory in /home/mp/www/tiles/api_categories.php on line <i>39</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0001</td><td bgcolor='#eeeeec' align='right'>241456</td><td bgcolor='#eeeeec'>{main}( )</td><td title='/home/mp/www/tiles/api_categories.php' bgcolor='#eeeeec'>../api_categories.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0015</td><td bgcolor='#eeeeec' align='right'>715400</td><td bgcolor='#eeeeec'>cacheCategories( )</td><td title='/home/mp/www/tiles/api_categories.php' bgcolor='#eeeeec'>../api_categories.php<b>:</b>67</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>3.8601</td><td bgcolor='#eeeeec' align='right'>54575184</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mkdir' target='_new'>mkdir</a>
( )</td><td title='/home/mp/www/tiles/api_categories.php' bgcolor='#eeeeec'>../api_categories.php<b>:</b>39</td></tr>
</table></font>
<pre>exception 'Exception' with message 'Could not open cache file' in /home/mp/www/tiles/api_categories.php:45
Stack trace:
#0 /home/mp/www/tiles/api_categories.php(67): cacheCategories()
#1 {main}</pre>
奇怪的是,当我直接访问PHP文件时,没有弹出警告、错误或异常。缓存文件在我第一次访问时正确创建,并在后续访问中正确读取。
当我在 PhpStorm 中设置零配置 PHP 调试时出现此问题。在此之前,该项目运行良好。这几乎就像 Angular 正在抓取旧警告或其他东西的缓存版本,因为,再次,当我直接访问 PHP 文件时,它可以正常工作。
有什么想法吗?我是 Angular 的新手,所以有没有办法清除它可能拥有的任何缓存?
您正在创建绝对路径:
$dirPath = '/cache/categories';
$cacheFileName = '/categories.json';
$cacheFilePath = $dirPath . $cacheFileName;
产生
/cache/categories/categories.json
^---root of your filesystem
您正在尝试创建 c:\cache\categories\categories.json
,而不是 c:\path\to\your\site\cache\categories.json
。
请记住,PHP 在文件系统级别运行。从 Web 的角度来看,它不知道您网站的结构。 PHP 的 /...
是 "root of harddrive",而不是 "root of site"。
想通了。就像有人在对我的问题的一条明显现已删除的评论中所说的那样,is_dir()
缓存了之前失败状态的结果。调用 clearstatcache()
立即清除它。
这是一个令人头疼的问题。我在服务器端继承了一个由 PHP 支持的 AngularJS 项目。我的任务是编写一个小的数据缓存函数,它非常简单。不幸的是,那是前端出现问题的时候。在我继续之前,我将展示我的 PHP 代码,以便我可以指出一些事情。文件结构和其他分类名称因保密协议而更改,与问题无关的代码已删除:
define('ROOT', dirname(__FILE__));
require('config/config.php');
require('includes/includes.php');
// caching
function cacheCategories()
{
$dirPath = ROOT . '/cache/categories';
$cacheFileName = '/categories.json';
$cacheFilePath = $dirPath . $cacheFileName;
$cacheTime = 86400;
if (file_exists($cacheFilePath) && (time() - $cacheTime) < filemtime($cacheFilePath)) { // cache file exists and is fresh
$cachedJSON = readfile($cacheFilePath);
if ($cachedJSON === false) {
throw new Exception('Could not read from cache file');
}
} else {
include_once('/site/load.php');
$categories = load_categories_tree();
$cachedJSON = json_encode($categories);
if (!is_dir($dirPath)) {
mkdir($dirPath);
}
$fp = fopen($cacheFilePath, 'w');
if ($fp === false) {
throw new Exception('Could not open cache file');
}
if (flock($fp, LOCK_EX)) {
$writeResult = fwrite($fp, $cachedJSON);
if ($writeResult === false) {
throw new Exception('Could not write to cache');
}
flock($fp, LOCK_UN);
fclose($fp);
}
}
return $cachedJSON;
}
global $page;
$page = new Page('');
$page->setDefaultLanguage('en');
echo cacheCategories();
如您所见,它非常简单。如果一个缓存文件存在,并且是新的,数据只是从中加载。如果没有,则创建一个新的缓存文件。必要时创建缓存目录。
我的问题是,当 AngularJS 试图从这个 PHP 文件中获取数据时,xdebug 生成了一个关于 mkdir
的警告,我也得到了一个通用的 [=15] =]:
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mkdir(): No such file or directory in /home/mp/www/tiles/api_categories.php on line <i>39</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0001</td><td bgcolor='#eeeeec' align='right'>241456</td><td bgcolor='#eeeeec'>{main}( )</td><td title='/home/mp/www/tiles/api_categories.php' bgcolor='#eeeeec'>../api_categories.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0015</td><td bgcolor='#eeeeec' align='right'>715400</td><td bgcolor='#eeeeec'>cacheCategories( )</td><td title='/home/mp/www/tiles/api_categories.php' bgcolor='#eeeeec'>../api_categories.php<b>:</b>67</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>3.8601</td><td bgcolor='#eeeeec' align='right'>54575184</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mkdir' target='_new'>mkdir</a>
( )</td><td title='/home/mp/www/tiles/api_categories.php' bgcolor='#eeeeec'>../api_categories.php<b>:</b>39</td></tr>
</table></font>
<pre>exception 'Exception' with message 'Could not open cache file' in /home/mp/www/tiles/api_categories.php:45
Stack trace:
#0 /home/mp/www/tiles/api_categories.php(67): cacheCategories()
#1 {main}</pre>
奇怪的是,当我直接访问PHP文件时,没有弹出警告、错误或异常。缓存文件在我第一次访问时正确创建,并在后续访问中正确读取。
当我在 PhpStorm 中设置零配置 PHP 调试时出现此问题。在此之前,该项目运行良好。这几乎就像 Angular 正在抓取旧警告或其他东西的缓存版本,因为,再次,当我直接访问 PHP 文件时,它可以正常工作。
有什么想法吗?我是 Angular 的新手,所以有没有办法清除它可能拥有的任何缓存?
您正在创建绝对路径:
$dirPath = '/cache/categories';
$cacheFileName = '/categories.json';
$cacheFilePath = $dirPath . $cacheFileName;
产生
/cache/categories/categories.json
^---root of your filesystem
您正在尝试创建 c:\cache\categories\categories.json
,而不是 c:\path\to\your\site\cache\categories.json
。
请记住,PHP 在文件系统级别运行。从 Web 的角度来看,它不知道您网站的结构。 PHP 的 /...
是 "root of harddrive",而不是 "root of site"。
想通了。就像有人在对我的问题的一条明显现已删除的评论中所说的那样,is_dir()
缓存了之前失败状态的结果。调用 clearstatcache()
立即清除它。