|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Application; |
|
4
|
|
|
|
|
5
|
|
|
use Composer\Autoload\ClassLoader; |
|
6
|
|
|
use Magento\Framework\Autoload\AutoloaderRegistry; |
|
7
|
|
|
use N98\Magento\Framework\App\Magerun; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Magento2Initializer |
|
11
|
|
|
* @package N98\Magento\Application |
|
12
|
|
|
*/ |
|
13
|
|
|
class Magento2Initializer |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var \Composer\Autoload\ClassLoader |
|
17
|
|
|
*/ |
|
18
|
|
|
private $autoloader; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Magento2Initializer constructor. |
|
22
|
|
|
* @param \Composer\Autoload\ClassLoader $autoloader |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(ClassLoader $autoloader) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->autoloader = $autoloader; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $magentoRootFolder |
|
31
|
|
|
* @return \N98\Magento\Framework\App\Magerun |
|
32
|
|
|
* @throws \Exception |
|
33
|
|
|
*/ |
|
34
|
|
|
public function init($magentoRootFolder) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->requireOnce($magentoRootFolder . '/app/bootstrap.php'); |
|
37
|
|
|
|
|
38
|
|
|
// Magento 2.3.1 removes phar stream wrapper. |
|
39
|
|
|
if (!in_array('phar', \stream_get_wrappers(), true)) { |
|
40
|
|
|
\stream_wrapper_restore('phar'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$magentoAutoloader = AutoloaderRegistry::getAutoloader(); |
|
44
|
|
|
|
|
45
|
|
|
// Prevent an infinite loop of autoloaders |
|
46
|
|
|
if (!$magentoAutoloader instanceof AutoloaderDecorator) { |
|
47
|
|
|
AutoloaderRegistry::registerAutoloader( |
|
48
|
|
|
new AutoloaderDecorator( |
|
49
|
|
|
$magentoAutoloader, |
|
50
|
|
|
$this->autoloader |
|
51
|
|
|
) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$params = $_SERVER; |
|
56
|
|
|
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'admin'; |
|
57
|
|
|
$params[\Magento\Store\Model\Store::CUSTOM_ENTRY_POINT_PARAM] = true; |
|
58
|
|
|
$params['entryPoint'] = basename(__FILE__); |
|
59
|
|
|
|
|
60
|
|
|
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params); |
|
61
|
|
|
/** @var \Magento\Framework\App\Cron $app */ |
|
62
|
|
|
$app = $bootstrap->createApplication(Magerun::class, []); |
|
63
|
|
|
/* @var $app \N98\Magento\Framework\App\Magerun */ |
|
64
|
|
|
$app->launch(); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
return $app; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* use require-once inside a function with it's own variable scope w/o any other variables |
|
71
|
|
|
* and $this unbound. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $path |
|
74
|
|
|
*/ |
|
75
|
|
View Code Duplication |
private function requireOnce($path) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$requireOnce = function () { |
|
78
|
|
|
require_once func_get_arg(0); |
|
79
|
|
|
}; |
|
80
|
|
|
if (50400 <= PHP_VERSION_ID) { |
|
81
|
|
|
$requireOnce->bindTo(null); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$requireOnce($path); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: