public
Description: MIT licensed pieces
Homepage: http://mark-story.com
Clone URL: git://github.com/markstory/story-scribbles.git
markstory (author)
Sat Mar 28 13:44:51 -0700 2009
commit  bb752d6f29a3a4d155dbf6ff609fc7dc4a153a0c
tree    207031143659cd4843507f8d0add952751ce547e
parent  74a535dd8458996b2b7ffa070420e253177ab5d7
story-scribbles / cakephp / shells / aco_sync.php
100644 231 lines (216 sloc) 5.981 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
<?php
/**
* Aco Shell.
*
* Automates the creation of ACO nodes for CakePHP applications.
*
* Copyright 2008, Mark Story.
* 823 millwood rd.
* toronto, ontario M4G 1W3
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2008, Mark Story.
* @link http://mark-story.com
* @version 0.5
* @author Mark Story <mark@mark-story.com>
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
App::import('Component', 'Acl');
App::import('Model', 'DbAcl');
/**
* Shell for ACO sync
*
* @package cake
* @subpackage cake.cake.console.libs
*/
class AcoSyncShell extends Shell {
/**
* Contains instance of AclComponent
*
* @var object
* @access public
*/
var $Acl;
/**
* Contains arguments parsed from the command line.
*
* @var array
* @access public
*/
var $args;
/**
* Contains database source to use
*
* @var string
* @access public
*/
var $dataSource = 'default';
 
/**
* Root node name.
*
* @var string
**/
var $rootNode = 'controllers';
 
/**
* Internal Clean Actions switch
*
* @var boolean
**/
var $_clean = false;
 
/**
* Start up And load Acl Component / Aco model
*
* @return void
**/
function startup() {
$this->Acl =& new AclComponent();
$controller = null;
$this->Acl->startup($controller);
$this->Aco =& $this->Acl->Aco;
}
 
/**
* Override main() for help message hook
*
* @access public
*/
function main() {
$out = __("Available ACO sync commands:", true) . "\n";
$out .= "\t - update\n";
$out .= "\t - sync\n";
$out .= "\t - help\n\n";
$out .= __("For help, run the 'help' command. For help on a specific command, run 'help <command>'", true);
$this->out($out);
}
 
/**
* undocumented function
*
* @return void
**/
function update() {
$root = $this->_checkNode($this->rootNode, $this->rootNode, null);
App::import('Core', array('Controller'));
 
$Controllers = Configure::listObjects('controller');
$appIndex = array_search('App', $Controllers);
if ($appIndex !== false ) {
unset($Controllers[$appIndex]);
}
// look at each controller in app/controllers
foreach ($Controllers as $ctrlName) {
App::import('Controller', $ctrlName);
// find / make controller node
$controllerNode = $this->_checkNode($this->rootNode .'/'.$ctrlName, $ctrlName, $root['Aco']['id']);
$this->_checkMethods($ctrlName, $controllerNode, $this->_clean);
}
if ($this->_clean) {
$this->Aco->id = $root['Aco']['id'];
$controllerNodes = $this->Aco->children(null, true);
$ctrlFlip = array_flip($Controllers);
foreach ($controllerNodes as $ctrlNode) {
if (!isset($ctrlFlip[$ctrlNode['Aco']['alias']])) {
$this->Aco->id = $ctrlNode['Aco']['id'];
if ($this->Aco->delete()) {
$this->out(sprintf(__('Deleted %s and all children', true), $this->rootNode . '/' . $ctrlNode['Aco']['alias']));
}
}
}
}
$this->out(__('Aco Update Complete', true));
return true;
}
 
/**
* Sync the ACO table
*
* @return void
**/
function sync() {
$this->_clean = true;
$this->update();
}
 
/**
* Check a node for existance, create it if it doesn't exist.
*
* @param string $path
* @param string $alias
* @param int $parentId
* @return array Aco Node array
*/
function _checkNode($path, $alias, $parentId = null) {
$node = $this->Aco->node($path);
if (!$node) {
$this->Aco->create(array('parent_id' => $parentId, 'model' => null, 'alias' => $alias));
$node = $this->Aco->save();
$node['Aco']['id'] = $this->Aco->id;
$this->out(sprintf(__('Created Aco node: %s', true), $path));
} else {
$node = $node[0];
}
return $node;
}
 
/**
* Check and Add/delete controller Methods
*
* @param string $controller
* @param array $node
* @param bool $cleanup
* @return void
*/
function _checkMethods($controller, $node, $cleanup = false) {
$className = $controller . 'Controller';
$baseMethods = get_class_methods('Controller');
$actions = get_class_methods($className);
$methods = array_diff($actions, $baseMethods);
foreach ($methods as $action) {
if (strpos($action, '_', 0) === 0) {
continue;
}
$this->_checkNode($this->rootNode . '/' . $controller . '/' . $action, $action, $node['Aco']['id']);
}
if ($cleanup) {
$actionNodes = $this->Aco->children($node['Aco']['id']);
$methodFlip = array_flip($methods);
foreach ($actionNodes as $action) {
if (!isset($methodFlip[$action['Aco']['alias']])) {
$this->Aco->id = $action['Aco']['id'];
if ($this->Aco->delete()) {
$path = $this->rootNode . '/' . $controller . '/' . $action['Aco']['alias'];
$this->out(sprintf(__('Deleted Aco node %s', true), $path));
}
}
}
}
return true;
}
 
 
/**
* Show help screen.
*
* @access public
*/
function help() {
$head = __("Usage: cake aco_sync <command>", true) . "\n";
$head .= "-----------------------------------------------\n";
$head .= __("Commands:", true) . "\n\n";
 
$commands = array(
'update' => "\tcake aco_sync update\n" .
"\t\t" . __("Add new ACOs for new controllers and actions", true) . "\n" .
"\t\t" . __("Create new ACO's for controllers and their actions. Does not remove any nodes from ACO table", true),
 
'sync' => "\tcake aco_sync sync\n" .
"\t\tPerform a full sync on the ACO table.\n" .
"\t\t" . __("Creates new ACO's for missing controllers and actions. Removes orphaned entries in the ACO table.", true) . "\n",
 
'help' => "\thelp [<command>]\n" .
"\t\t" . __("Displays this help message, or a message on a specific command.", true) . "\n"
);
 
$this->out($head);
if (!isset($this->args[0])) {
foreach ($commands as $cmd) {
$this->out("{$cmd}\n\n");
}
} elseif (isset($commands[low($this->args[0])])) {
$this->out($commands[low($this->args[0])] . "\n");
} else {
$this->out(sprintf(__("Command '%s' not found", true), $this->args[0]));
}
}
}
?>