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
|
|
|
use RuntimeException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Magento initialiser (Magento 1) |
16
|
|
|
* |
17
|
|
|
* @package N98\Magento |
18
|
|
|
*/ |
19
|
|
|
class Initialiser |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Mage filename |
23
|
|
|
*/ |
24
|
|
|
const PATH_APP_MAGE_PHP = 'app/Mage.php'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Mage classname |
28
|
|
|
*/ |
29
|
|
|
const CLASS_MAGE = 'Mage'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string path to Magento root directory |
33
|
|
|
*/ |
34
|
|
|
private $magentoPath; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Bootstrap Magento application |
38
|
|
|
*/ |
39
|
|
|
public static function bootstrap($magentoPath) |
40
|
|
|
{ |
41
|
|
|
$initialiser = new Initialiser($magentoPath); |
42
|
|
|
$initialiser->requireMage(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Initialiser constructor. |
47
|
|
|
* |
48
|
|
|
* @param string $magentoPath |
49
|
|
|
*/ |
50
|
|
|
public function __construct($magentoPath) |
51
|
|
|
{ |
52
|
|
|
$this->magentoPath = $magentoPath; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Require app/Mage.php if class Mage does not yet exists. Preserves auto-loaders |
57
|
|
|
* |
58
|
|
|
* @see \Mage (final class) |
59
|
|
|
*/ |
60
|
|
|
public function requireMage() |
61
|
|
|
{ |
62
|
|
|
if (class_exists(self::CLASS_MAGE, false)) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->requireOnce(); |
67
|
|
|
|
68
|
|
|
if (!class_exists(self::CLASS_MAGE, false)) { |
69
|
|
|
throw new RuntimeException(sprintf('Failed to load definition of "%s" class', self::CLASS_MAGE)); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Require app/Mage.php in it's own scope while preserving all autoloaders. |
75
|
|
|
*/ |
76
|
|
|
private function requireOnce() |
77
|
|
|
{ |
78
|
|
|
// Create a new AutoloadRestorer to capture current auto-loaders |
79
|
|
|
$restorer = new AutoloadRestorer(); |
80
|
|
|
|
81
|
|
|
$path = $this->magentoPath . '/' . self::PATH_APP_MAGE_PHP; |
82
|
|
|
initialiser_require_once($path); |
83
|
|
|
|
84
|
|
|
// Restore auto-loaders that might be removed by extensions that overwrite Varien/Autoload |
85
|
|
|
$restorer->restore(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* use require-once inside a function with it's own variable scope and no $this (?) |
91
|
|
|
*/ |
92
|
|
|
function initialiser_require_once() |
93
|
|
|
{ |
94
|
|
|
require_once func_get_arg(0); |
95
|
|
|
} |
96
|
|
|
|