Skip to content

Commit c38cace

Browse files
author
Ted Kulp
committed
The basics of the silk CLI command
Also, made sure a prefix was put on any indexes created in SilkDatabase.
1 parent 35aa4d3 commit c38cace

File tree

3 files changed

+155
-1
lines changed

3 files changed

+155
-1
lines changed

classes/class.silk_database.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public static function create_index($table, $name, $field)
168168
{
169169
$dbdict = NewDataDictionary(self::get_instance());
170170

171-
$sqlarray = $dbdict->CreateIndexSQL($name, self::get_prefix().$table, $field);
171+
$sqlarray = $dbdict->CreateIndexSQL(self::get_prefix().$name, self::get_prefix().$table, $field);
172172
$dbdict->ExecuteSQLArray($sqlarray);
173173
}
174174

classes/cli/class.silk_cli.php

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
2+
// The MIT License
3+
//
4+
// Copyright (c) 2008 Ted Kulp
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
24+
class SilkCli extends SilkObject
25+
{
26+
public $argc = null;
27+
public $argv = null;
28+
29+
function __construct($argc, $argv)
30+
{
31+
parent::__construct();
32+
$this->argc = $argc;
33+
$this->argv = $argv;
34+
}
35+
36+
public function run()
37+
{
38+
$args = $this->arguments($this->argv);
39+
40+
if (!isset($args['arguments'][0]))
41+
{
42+
die("No task given. Use --help or pass a valid task.\n");
43+
}
44+
45+
$task = array_shift($args['arguments']);
46+
$task_class = camelize($task . '_task');
47+
if (!class_exists($task_class))
48+
{
49+
$task_class_silk = camelize('silk_' . $task . '_task');
50+
if (!class_exists($task_class_silk))
51+
{
52+
die("Task class '{$task_class}' not found. Aborting.\n");
53+
}
54+
else
55+
{
56+
$task_class = $task_class_silk;
57+
}
58+
}
59+
60+
$task_obj = new $task_class;
61+
$task_obj->run($args['arguments'], $args['flags'], $args['options']);
62+
}
63+
64+
/**
65+
* Parses the command line arguments into their various pieces (exec, options,
66+
* flag and arguments).
67+
* Lifted from: http://us.php.net/manual/en/features.commandline.php#86616
68+
*
69+
* @param array $args The args from $argv
70+
* @return array
71+
* @author Anonymous
72+
*/
73+
function arguments($args)
74+
{
75+
$ret = array(
76+
'exec' => '',
77+
'options' => array(),
78+
'flags' => array(),
79+
'arguments' => array(),
80+
);
81+
82+
$ret['exec'] = array_shift( $args );
83+
84+
while (($arg = array_shift($args)) != NULL)
85+
{
86+
// Is it a option? (prefixed with --)
87+
if ( substr($arg, 0, 2) === '--' )
88+
{
89+
$option = substr($arg, 2);
90+
91+
// is it the syntax '--option=argument'?
92+
if (strpos($option,'=') !== FALSE)
93+
array_push( $ret['options'], explode('=', $option, 2) );
94+
else
95+
array_push( $ret['options'], $option );
96+
97+
continue;
98+
}
99+
100+
// Is it a flag or a serial of flags? (prefixed with -)
101+
if ( substr( $arg, 0, 1 ) === '-' )
102+
{
103+
for ($i = 1; isset($arg[$i]) ; $i++)
104+
$ret['flags'][] = $arg[$i];
105+
106+
continue;
107+
}
108+
109+
// finally, it is not option, nor flag
110+
$ret['arguments'][] = $arg;
111+
continue;
112+
}
113+
return $ret;
114+
}
115+
}
116+
117+
?>

silk.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
2+
// The MIT License
3+
//
4+
// Copyright (c) 2009 Ted Kulp
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
24+
define('ROOT_DIR', dirname(__FILE__));
25+
26+
if (!isset($argv))
27+
die('This is a command line tool');
28+
29+
include_once('lib/silk/silk.api.php');
30+
31+
SilkBootstrap::setup();
32+
33+
$cli = new SilkCli($argc, $argv);
34+
$cli->run();
35+
36+
# vim:ts=4 sw=4 noet
37+
?>

0 commit comments

Comments
 (0)