|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* this file is part of magerun |
|
5
|
|
|
* |
|
6
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace N98\Magento; |
|
10
|
|
|
|
|
11
|
|
|
use N98\Util\AutoloadRestorer; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Magento initialiser (Magento 1) |
|
15
|
|
|
* |
|
16
|
|
|
* @package N98\Magento |
|
17
|
|
|
*/ |
|
18
|
|
|
class Initialiser |
|
19
|
|
|
{ |
|
20
|
|
|
const PATH_APP_MAGE_PHP = 'app/Mage.php'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string path to Magento root directory |
|
24
|
|
|
*/ |
|
25
|
|
|
private $magentoPath; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Bootstrap Magento application |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function bootstrap($magentoPath) |
|
31
|
|
|
{ |
|
32
|
|
|
$initialiser = new Initialiser($magentoPath); |
|
33
|
|
|
$initialiser->requireMage(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Initialiser constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $magentoPath |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($magentoPath) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->magentoPath = $magentoPath; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Require app/Mage.php if class Mage does not yet exists. Preserves auto-loaders |
|
49
|
|
|
* |
|
50
|
|
|
* @see \Mage (final class) |
|
51
|
|
|
*/ |
|
52
|
|
|
public function requireMage() |
|
53
|
|
|
{ |
|
54
|
|
View Code Duplication |
if (!class_exists('Mage', false)) { |
|
|
|
|
|
|
55
|
|
|
// Create a new AutoloadRestorer to capture current auto-loaders |
|
56
|
|
|
$restorer = new AutoloadRestorer(); |
|
57
|
|
|
|
|
58
|
|
|
$path = $this->magentoPath . '/' . self::PATH_APP_MAGE_PHP; |
|
59
|
|
|
|
|
60
|
|
|
// require app/Mage.php from Magento in a function of it's own to have it's own variable scope |
|
61
|
|
|
$this->requireOnce($path); |
|
62
|
|
|
// Restore auto-loaders that might be removed by extensions that overwrite Varien/Autoload |
|
63
|
|
|
$restorer->restore(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* use require-once inside a function with it's own variable scope w/o any other variables |
|
69
|
|
|
* and $this unbound. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $path |
|
72
|
|
|
*/ |
|
73
|
|
|
private function requireOnce($path) |
|
74
|
|
|
{ |
|
75
|
|
|
initialiser_require_once($path); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* use require-once inside a function with it's own variable scope and no $this (?) |
|
81
|
|
|
*/ |
|
82
|
|
|
function initialiser_require_once() |
|
83
|
|
|
{ |
|
84
|
|
|
require_once func_get_arg(0); |
|
85
|
|
|
} |
|
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.