简体   繁体   English

Xdebug,AngularJS和PhpStorm不应发生的PHP警告

[英]PHP warnings that should not be occurring with Xdebug, AngularJS, and PhpStorm

This is a head-scratcher. 这是一个令人头疼的问题。 I inherited an AngularJS project that's backed by PHP on the server side. 我继承了一个由服务器端PHP支持的AngularJS项目。 I was tasked with writing a little data caching function, which is embarrassingly simple. 我的任务是编写一些数据缓存功能,该功能非常简单。 Unfortunately, that's when things broke on the front end. 不幸的是,那是前端发生问题的时候。 Before I continue further, I'll show my PHP code so I can point to things. 在继续之前,我将展示我的PHP代码,以便我可以指出。 File structure and other assorted names changed due to NDA, and code not related to the problem removed: 文件结构和其他分类名称由于NDA而更改,并且与该问题不相关的代码已删除:

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();

As you can see, it's pretty simple. 如您所见,这非常简单。 If a cache file exists, and is fresh, the data is simply loaded from that. 如果缓存文件存在且是最新的,则仅从中加载数据。 If not, a new cache file is created. 如果不是,则创建一个新的缓存文件。 The cache directory is created if necessary. 如有必要,将创建缓存目录。

My problem is that when AngularJS attempts to grab the data from this PHP file, xdebug generates a warning about mkdir , and I also get a generic Exception : 我的问题是,当AngularJS尝试从此PHP文件中获取数据时,xdebug会生成有关mkdir的警告,并且我还会得到一个通用的Exception

 <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> 

The weird thing is, when I access the PHP file directly, no warnings, errors, or exceptions pop up. 奇怪的是,当我直接访问PHP文件时,没有警告,错误或异常弹出。 The cache file is created properly upon my first visit, and read from properly in subsequent visits. 缓存文件是在我第一次访问时正确创建的,并在以后的访问中正确地读取了。

This issue started when I setup zero configuration PHP debugging in PhpStorm. 当我在PhpStorm中设置零配置PHP调试时,开始出现此问题。 Before that, the project was working perfectly. 在此之前,该项目运行良好。 It's almost as though Angular is grabbing a cached version of an old warning or something, because, again, when I access the PHP file directly, it works as it should. 几乎就像Angular正在抓取旧警告或其他内容的缓存版本一样,因为,再次,当我直接访问PHP文件时,它可以正常工作。

Any ideas? 有任何想法吗? I'm new to Angular, so is there a way I can purge whatever cache it may have? 我是Angular的新手,所以有什么办法可以清除它可能拥有的任何缓存?

You're creating an absolute path: 您正在创建一条绝对路径:

$dirPath = '/cache/categories';
$cacheFileName = '/categories.json';
$cacheFilePath = $dirPath . $cacheFileName;

which produces 产生

/cache/categories/categories.json
^---root of your filesystem

You're trying to do the equivalent of creating c:\\cache\\categories\\categories.json , instead of c:\\path\\to\\your\\site\\cache\\categories.json . 您正在尝试创建c:\\cache\\categories\\categories.json ,而不是c:\\path\\to\\your\\site\\cache\\categories.json

Remember that PHP operates at the filesystem level. 请记住,PHP在文件系统级别运行。 It has no knowledge of the structure of your site from a Web perspective. 从Web角度来看,它不了解您的网站的结构。 /... for PHP is "root of harddrive", not "root of site". /... for PHP是“硬盘驱动器的根目录”,而不是“站点的根目录”。

Figured it out. 弄清楚了。 Like someone said in an apparently now deleted comment to my question, is_dir() cached the result of a failure state from before. 就像有人在我的问题的明显已删除的评论中说的那样, is_dir()从以前缓存了失败状态的结果。 Invoking clearstatcache() cleared it right up. 调用clearstatcache()清除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM