This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Magento1xModuleStarter.php
154 lines (138 loc) · 5.12 KB
/
Magento1xModuleStarter.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// This script uses the process described at
// http://coding.smashingmagazine.com/2012/03/01/basics-creating-magento-module/
// to start Magento 1.x module creation in <MAGE BASEDIR>/app/code/local
// Make sure you have write permissions to the Magento base directory.
// start
// check arguments
if (sizeof($argv)!=4)
exit("usage: " . $argv[0] . " <mage basedir> <namespace> <mod_name>\n");
try {
$mod=new MagentoModuleStarter($argv[1], $argv[2], $argv[3]);
$mod->structureModDir();
$mod->cfgModule();
$mod->activateModule();
print("Log into the Magento admin panel and navigate to "
. "System > Configuration > Advanced > Advanced and view "
. "the Disable Modules Output listing. You should see "
. "this module listed as enabled. Now follows the steps in "
. "the above link to customize the module for your needs.\n");
}
catch (Exception $ex) {
print($ex->getMessage() . "\n");
}
Class MagentoModuleStarter
{
private $mage_basedir="";
private $mod_ns="";
private $mod_nm="";
private $mage_localdir="";
private $mage_moduledir="";
public function __construct($basedir, $ns, $nm)
{
$this->mage_basedir=$basedir;
$this->mod_ns=$ns;
$this->mod_nm=$nm;
// basic check that the directory has Mage source
if (is_dir($this->mage_basedir . '/app/code/core')==FALSE)
throw new Exception($this->mage_basedir
. " does not appear to be a Magento install\n");
}
public function structureModDir()
{
// create app/code/local if not exists
$this->mage_localdir=$this->mage_basedir . '/app/code/local';
if (is_dir($this->mage_localdir)==FALSE) {
$local_created=mkdir($this->mage_localdir, $mode=0755);
if ($local_created==FALSE)
exit('unable to create '
. $this->mage_localdir . "\n");
}
// create dirs for namespace and module
$this->mage_moduledir=
$this->mage_localdir . '/'
. $this->mod_ns . '/' . $this->mod_nm;
$ns_created=mkdir($this->mage_moduledir, 0755, TRUE);
if ($ns_created==FALSE) {
exit('unable to create '
. $this->mage_moduledir . "\n");
}
// create sub-dirs within module dir
$ns_created=mkdir($this->mage_moduledir . '/Block', 0755, TRUE);
if ($ns_created==FALSE) exit("failed to create Block dir\n");
$ns_created=mkdir($this->mage_moduledir . '/controllers', 0755, TRUE);
if ($ns_created==FALSE) exit("failed to create controllers dir\n");
$ns_created=mkdir($this->mage_moduledir . '/Helper', 0755, TRUE);
if ($ns_created==FALSE) exit("failed to create Helper dir\n");
$ns_created=mkdir($this->mage_moduledir . '/Model', 0755, TRUE);
if ($ns_created==FALSE) exit("failed to create Model dir\n");
$ns_created=mkdir($this->mage_moduledir . '/sql', 0755, TRUE);
if ($ns_created==FALSE) exit("failed to create sql dir\n");
}
// create <MODULE_BASEDIR>/etc/config.xml
public function cfgModule()
{
$etc_created=mkdir($this->mage_moduledir . '/etc', 0755, TRUE);
if ($etc_created==FALSE)
exit ("unable to create etc sub-dir\n");
$fh_cfgxml=fopen($this->mage_moduledir . '/etc/config.xml'
, 'w');
if ($fh_cfgxml==FALSE)
exit ("unable to create config.xml\n");
$cfgxml=$this->getConfigXml();
fwrite($fh_cfgxml, $cfgxml);
fclose($fh_cfgxml);
}
// create <MAGE_APPDIR>/etc/modules/Namespace_Module.xml
public function activateModule()
{
$fh_modxml=fopen($this->mage_basedir
. '/app/etc/modules/' . $this->mod_ns . '_'
. $this->mod_nm . '.xml', 'w');
if ($fh_modxml==FALSE)
exit("unable to activate module\n");
$modxml=$this->getModuleCfgXml();
fwrite($fh_modxml, $modxml);
fclose($fh_modxml);
}
private function getConfigXml()
{
$out = '<?xml version="1.0" encoding="UTF-8"?>';
$out = $out . "\n" . '<config>';
$out = $out . "\n\t" . '<modules>';
$out = $out . "\n\t\t" . '<' . $this->mod_ns
. '_' . $this->mod_nm . '>';
$out = $out . "\n\t\t\t" . '<version>1.0.0</version>';
$out = $out . "\n\t\t" . '</' . $this->mod_ns
. '_' . $this->mod_nm . '>';
$out = $out . "\n\t" . '</modules>';
$out = $out . "\n" . '</config>';
return $out;
}
private function getModuleCfgXml()
{
$out = '<?xml version="1.0" encoding="UTF-8"?>';
$out = $out . "\n" . '<config>';
$out = $out . "\n\t" . '<modules>';
$out = $out . "\n\t\t" . '<' . $this->mod_ns
. '_' . $this->mod_nm . '>';
$out = $out . "\n\t\t\t" . '<active>true</active>';
$out = $out . "\n\t\t\t" . '<codePool>local</codePool>';
$out = $out . "\n\t\t" . '</' . $this->mod_ns
. '_' . $this->mod_nm . '>';
$out = $out . "\n\t" . '</modules>';
$out = $out . "\n" . '</config>';
return $out;
}
}
?>