forked from limb-php-framework/limb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimb.php
108 lines (90 loc) · 2.54 KB
/
limb.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
<?php
$limb_dir = dirname(__FILE__);
require_once($limb_dir.'/taskman/taskman.inc.php');
lmb_cli_init_limb($limb_dir);
taskman_propsetor('TASKS_MASKS', 'limb/*/cli/*.tasks.php' . PATH_SEPARATOR . 'src/*/cli/*.tasks.php');
if (null === taskman_propor('PROJECT_DIR', null))
taskman_propsetor('PROJECT_DIR', lmb_get_project_dir());
taskman_propsetor('LIMB_DIR', $limb_dir.'/../');
lmb_cli_init_tasks();
taskman_run();
function lmb_get_project_dir()
{
if(!$project_dir = get_cfg_var('limb.project_dir'))
$project_dir = lmb_cli_ask_for_option('Project directory', lmb_cli_find_project_dir(getcwd()));
else
echo "Project directory loaded from PHP config: {$project_dir}\n";
return $project_dir;
}
function lmb_cli_find_project_dir($current_dir)
{
if(file_exists($current_dir . '/setup.php'))
{
ob_start();
register_shutdown_function('lmb_cli_check_limb_instance');
lmb_require($current_dir . '/setup.php');
ob_end_clean();
lmb_cli_check_limb_instance($disable = true);
return $current_dir;
}
else
{
$parent_dir = dirname($current_dir);
if($current_dir != $parent_dir)
return lmb_cli_find_project_dir($parent_dir);
else
return getcwd();
}
}
/**
* Ugly hack for check situation when limb already included from another dir
*/
function lmb_cli_check_limb_instance($disable = false)
{
static $is_disabled = false;
if($disable)
$is_disabled = true;
if($is_disabled || !$error = error_get_last())
return;
ob_end_clean();
if(false !== strstr($error['message'], 'Cannot redeclare lmb_resolve_include_path()'))
{
taskman_sysmsg("You must use the same instance of Limb as in the project!\n");
exit(1);
}
}
function lmb_cli_init_limb($limb_dir)
{
set_include_path(get_include_path() . PATH_SEPARATOR . $limb_dir . '/../');
require_once('limb/core/common.inc.php');
}
function lmb_cli_init_tasks()
{
foreach(explode(PATH_SEPARATOR, taskman_prop('TASKS_MASKS')) as $mask)
lmb_require_glob($mask);
}
function lmb_cli_ask_for_option($option_name, $default_value = '')
{
if($default_value)
$question_str = "{$option_name} [{$default_value}]: ";
else
$question_str = "{$option_name}: ";
do
{
fputs(STDOUT, $question_str);
if(!($user_in = trim(fgets(STDIN))) && $default_value)
$user_in = $default_value;
}
while(!$user_in);
return $user_in;
}
function lmb_cli_ask_for_accept($question)
{
do
{
fputs(STDOUT, "{$question} [y/n]: ");
$user_in = trim(fgets(STDIN));
}
while($user_in != 'y' && $user_in != 'n');
return $user_in == 'y' ? true : false;
}