-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdependent.php
More file actions
236 lines (196 loc) · 6.11 KB
/
jdependent.php
File metadata and controls
236 lines (196 loc) · 6.11 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* @package JDependent
* @copyright Copyright (C) 2012 NinjaForge. All rights reserved.
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link http://ninjaforge.com
*/
defined('_JEXEC') or die;
/**
* Dependency Class for Joomla 3rd Party Extensions
*/
class JDependent
{
/**
* Boolean value to determine if we created the table
*
* @var bool
*/
protected $_table;
/**
* the path to the dependent files
*/
public $config;
/**
* The JInstaller Instace
*/
public $installer;
/**
* Class constructor.
*
* @param array $config a configuration array
*/
public function __construct($config = array())
{
$this->config = $config;
$this->installer = $this->getInstaller();
if (!isset($config['dependents_path']))
$this->config['dependents_path'] = $this->installer->getPath('source').'/dependents/';
$lang = JFactory::getLanguage();
$lang->load('lib_jdependent', dirname(__FILE__), 'en-GB');
$lang->load('lib_jdependent', dirname(__FILE__), $lang->getDefault(), true);
$this->createTable();
}
/**
* Method for installing a requisite
*
* @param string the identifiying class name
* @param array an array of files/folders/extensions to exclude
*
* @return JDependent
*/
public function install($identifier, $excludes = array())
{
$requisite = $this->_instantiate($identifier);
$requisite->install($identifier, $excludes);
return $this;
}
/**
* Method for uninstalling a requisite
*
* @param string the identifiying class name
* @param array an array of files/folders/extensions to exclude
*
* @return JDependent
*/
public function uninstall($identifier, $excludes = array())
{
$requisite = $this->_instantiate($identifier);
$requisite->uninstall($excludes);
return $this;
}
/**
* Method for adding the joomla_dependents table if it does not exist
*
* @return void
*/
public function createTable()
{
// Attempt to create the table if we have not already tried
if (!$this->_table) {
$db = JFactory::getDBO();
// @todo move this to an sql file and write a parser, just in case we want to add more tables later
$query =
'CREATE TABLE IF NOT EXISTS `#__joomla_dependents` ('.
'id bigint(20) unsigned NOT NULL auto_increment,'.
'dependent VARCHAR( 255 ) NOT NULL,'.
'requisite VARCHAR( 255 ) NOT NULL,'.
'version DECIMAL(10,2) unsigned NOT NULL,'.
'PRIMARY KEY (`id`)'.
') ENGINE=MyISAM DEFAULT CHARSET=utf8;';
$db->setQuery($query);
if (!$db->query())
JError::raiseWarning(1, JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)));
else
$this->_table = true;
}
}
/**
* Method for registering a dependency in the database
*
* @param string the dependent extension name eg: com_ninjaboard
* @param string the required extension/library/framework eg: nooku
*
* @return JDependent
*/
public function registerDependency($dependent, $requisite, $version)
{
$db = JFactory::getDBO();
$query = "SELECT id FROM `#__joomla_dependents` WHERE `dependent` = '".$dependent."' AND `requisite` = '".$requisite."';";
$db->setQuery($query);
//only register this item if it has not been already
if (!$db->loadResult()) {
$query = "INSERT INTO `#__joomla_dependents` (`id` ,`dependent` ,`requisite` ,`version`) VALUES (NULL, '".$dependent."', '".$requisite."', '".$version."');";
$db->setQuery($query);
if (!$db->query())
JError::raiseWarning(1, JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)));
}
return $this;
}
/**
* Method for deregistering a dependency in the database
*
* @param string the dependent extension name eg: com_ninjaboard
* @param string the required extension/library/framework eg: nooku
*
* @return JDependent
*/
public function deregisterDependency($dependent, $requisite)
{
$db = JFactory::getDBO();
$query = "DELETE FROM `#__joomla_dependents` WHERE `dependent` = '".$dependent."' AND `requisite` = '".$requisite."';";
$db->setQuery($query);
if (!$db->query()) {
JError::raiseWarning(1, JText::sprintf('JLIB_INSTALLER_ERROR_SQL_ERROR', $db->stderr(true)));
}
return $this;
}
/**
* Method for checking to see if there are other items that require this dependency
*
* @param string the dependent extension name eg: com_ninjaboard
* @param string the required extension/library/framework eg: nooku
* @return boolean true if we have other dependent extensions else its false
*/
public function checkDependencyRegistry($dependent, $requisite)
{
$db = JFactory::getDBO();
$query = "SELECT COUNT(*) FROM `#__joomla_dependents` WHERE `dependent` != '".$dependent."' AND `requisite` = '".$requisite."';";
$db->setQuery($query);
$result = $db->loadResult();
// simply if there are entries in the db then there must be other extensions using this dependent
if ($result) return true;
return false;
}
/**
* Get an instance of a class based on a class identifier
*
* @param string identifier
*
* @return object Return object on success, throws exception on failure
*
* @throws JException
*/
protected function _instantiate($identifier = null)
{
include_once dirname(__FILE__).'/dependents/plugin.php';
$path = dirname(__FILE__).'/dependents/'.$identifier.'.php';
if (file_exists($path))
include_once $path;
$result = false;
// determine wether we passed the full class string
if (!strstr($identifier, 'JDependentPlugin'))
$identifier = 'JDependentPlugin'.ucfirst($identifier);
// make sure the class exists
if (class_exists($identifier))
{
$result = new $identifier($this->config);
}
// throw an exception if it doesnt
if(!is_object($result)) {
throw new JException('Cannot instantiate object from identifier : '.$identifier);
}
return $result;
}
/**
* Methods for getting the Jinstaller Instance
*
* @return JInstaller The JInstaller instance
*/
public function getInstaller() {
if(!isset($this->installer)) {
$this->installer = JInstaller::getInstance();
}
return $this->installer;
}
}