Magento 2判断文件是否存在

原文地址 

如果浏览不顺畅请到原文章出处:www.sky8g.com/technology/2964/

请注意可能会提示风险,这是csdn官网如果不是他们的网址,其他的网址都会提示有风险,这是CSDN网站设置的问题,本网站全部文章为免费技术分享,请放心访问,无需担心。

原文章出处:www.sky8g.com/technology/2964/

Magento 2开发相对于Magento 1开发难度有所增加,Magento 2完全和M1结构不同。接下来我将讲述Magento 2中判断文件是否存在?

php中判断文件是否存在有两种方式 

第一种是is_file()函数

第二种是file_exists()函数

这两个函数都可以判断文件是否存在,但是我们应该使用哪一个呢?作为开发者为了提高效率我们需要选择适合性能好的方法,有的php开发者使用is_file()函数,但是有个开发又使用file_exists()函数,他们两个函数之间有什么关系和联系呢?其实:file_exists() 相当于is_dir()或者is_file()函数,既可以判断目录是否存在又可以判断文件存在。存在返回值为true,否则为false。 is_file()函数只能判断文件是否存在,存在返回值为true,否者为false

通过Magento 2 controller方法比较他们的性能

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

<?php

/**

* @WEBSITE https://www.sky8g.com

*/

namespace Sky8g\Hello\Controller\Test;

use Sky8g\Hello\Helper\Data;

class Index extends \Magento\Framework\App\Action\Action

{

protected $_coreRegistry;

protected $_pageFactory;

protected $_helper;

protected $_dir;

protected $_storeManager;

public function __construct(

\Magento\Framework\App\Action\Context $context,

\Magento\Framework\Registry $registry,

\Magento\Store\Model\StoreManagerInterface $storeManager,

Data $helper,

\Magento\Framework\View\Result\PageFactory $pageFactory)

{

$this->_pageFactory  = $pageFactory;

$this->_coreRegistry = $registry;

$this->_storeManager = $storeManager;

$this->_helper   = $helper;

return parent::__construct($context);

}

public function execute()

{

$this->benchmark('is_file');

$this->benchmark('file_exists');

}

protected function getDir(){

return $this->_objectManager->get( \Magento\Framework\Filesystem\DirectoryList::class);

}

protected function benchmark($funcName) {

    $numCycles = 10000;

    $time_start = microtime(true);

    for ($i = 0; $i < $numCycles; $i++) {

        clearstatcache();

        $funcName(BP.'/LICENSE.txt'); // or 'path/to/file.php' instead of __FILE__

    }

    $time_end = microtime(true);

    $time = $time_end - $time_start;

    echo "{$funcName}  函数循环{$numCycles}次,用了{$time}秒 <br>\n";

}

}

结果如下:

文件存在的情况下

1

2

is_file     函数循环10000次,用了3.6892459392548秒

file_exists 函数循环10000次,用了3.6762230396271秒

文件不存在的情况下

1

2

is_file     函数循环10000次,用了2.277281999588秒

file_exists 函数循环10000次,用了2.2435050010681秒

通过上述可以看到在10000次内性能反而,file_exists()方法最佳。

所以Magento 2自动加载文件代码如下,通过file_exists()判断文件是否存在来实现的。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<?php

/**

* Register basic autoloader that uses include path

*

* Copyright © Magento, Inc. All rights reserved.

* See COPYING.txt for license details.

*/

use Magento\Framework\Autoload\AutoloaderRegistry;

use Magento\Framework\Autoload\ClassLoaderWrapper;

/**

* Shortcut constant for the root directory

*/

define('BP', dirname(__DIR__));

define('VENDOR_PATH', BP . '/app/etc/vendor_path.php');

if (!file_exists(VENDOR_PATH)) {

    throw new \Exception(

        'We can\'t read some files that are required to run the Magento application. '

         . 'This usually means file permissions are set incorrectly.'

    );

}

$vendorDir = require VENDOR_PATH;

$vendorAutoload = BP . "/{$vendorDir}/autoload.php";

/* 'composer install' validation */

if (file_exists($vendorAutoload)) {

    $composerAutoloader = include $vendorAutoload;

} else {

    throw new \Exception(

        'Vendor autoload is not found. Please run \'composer install\' under application root directory.'

    );

}

AutoloaderRegistry::registerAutoloader(new ClassLoaderWrapper($composerAutoloader));通过

总结 

Magento 2加载了一个文件,所以使用了函数方法file_exists,这样性能有所提高,如果大于100k我们就要使用了is_file()函数了。我们可以看出如果文件存在,则加载进来,否者报错。

如果有不懂的地方请留言,SKY8G网站编辑者专注于研究IT源代码研究与开发。希望你下次光临,你的认可和留言是对我们最大的支持,谢谢!

发布了65 篇原创文章 · 获赞 6 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/jimbooks/article/details/98100170