forked from iron-io/laraworker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaraWorker.php
125 lines (109 loc) · 4.44 KB
/
LaraWorker.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
<?php
$version = get_laravel_version();
$current_path = getcwd() . '/vendor/iron-io/laraworker/';
$workers_dir_path = getcwd() . '/workers';
$install_option = getopt("i:");
@$install_option = $install_option['i'] === 'true' ? true : false;
if (!$install_option)
return;
if ($version == 4){
$command_destination_path = getcwd() . '/app/commands';
$config_destination_path = getcwd() . '/app/config';
$artisan_file_path = getcwd() . '/app/start/artisan.php';
$commands = array('RunWorker.php', 'UploadWorker.php');
//register commands
foreach ($commands as $command) {
$register_command_text = "Artisan::add(new " . remove_extension($command) . ");";
if (!is_command_registered($artisan_file_path, $register_command_text))
file_put_contents($artisan_file_path, "\r\n" . $register_command_text, FILE_APPEND);
}
} elseif ($version == 5){
$command_destination_path = getcwd() . '/app/Console/Commands';
$config_destination_path = getcwd() . '/config';
$artisan_file_path = getcwd() . '/app/Console/Kernel.php';
$commands = array('App\Console\Commands\RunWorker', 'App\Console\Commands\UploadWorker');
$worker_boot_path = $current_path . '/workers/libs/worker_boot.php';
$register_command_text = "";
foreach ($commands as $command) {
$register_command_text .= "'" . $command . "',\n";
}
insert_to_file($artisan_file_path, "protected \$commands", $register_command_text);
insert_to_file($current_path.'commands/UploadWorker.php', "<?php", "namespace App\Console\Commands;\nuse Config;\n");
insert_to_file($current_path.'commands/RunWorker.php', "<?php", "namespace App\Console\Commands;\nuse Queue;\n");
replace_in_file(getcwd().'/vendor/laravel/framework/src/Illuminate/Queue/Connectors/IronConnector.php', 'IronMQ\IronMQ', 'IronMQ');
replace_in_file(getcwd().'/vendor/laravel/framework/src/Illuminate/Queue/IronQueue.php', 'IronMQ\IronMQ', 'IronMQ');
replace_in_file($worker_boot_path,'start.php','app.php');
replace_in_file($worker_boot_path,'$app->setRequestForConsoleEnvironment();','');
replace_in_file($worker_boot_path,'$app->boot()','$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap()');
replace_in_file($worker_boot_path,'Config::get(\'app.key\')','config(\'app.key\'), config(\'app.cipher\')');
} else {
echo "Error: unrecognized version of Laravel";
return;
}
//copy config
if (!file_exists($config_destination_path . '/ironworker.php'))
recurse_copy($current_path . '/config', $config_destination_path);
recurse_copy($current_path . '/config', $config_destination_path);
//copy commands
recurse_copy($current_path . '/commands', $command_destination_path);
//copy example worker with worker_boot
recurse_copy($current_path . '/workers', $workers_dir_path);
echo "LaraWorker package installed." . PHP_EOL;
function remove_extension($filename)
{
return preg_replace("/\\.[^.\\s]{3,4}$/", "", $filename);
}
function recurse_copy($src, $dst)
{
$dir = opendir($src);
@mkdir($dst);
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
if (is_dir($src . '/' . $file)) {
recurse_copy($src . '/' . $file, $dst . '/' . $file);
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
closedir($dir);
}
function is_command_registered($artisan_file_path, $register_command_text)
{
$artisan_file = file_get_contents($artisan_file_path);
$pattern = preg_quote($register_command_text, '/');
$pattern = "/^.*$pattern.*\$/m";
if (preg_match_all($pattern, $artisan_file, $matches))
return true;
else
return false;
}
function get_laravel_version()
{
$composer_content = file_get_contents('composer.json');
$content_arr = json_decode($composer_content, true);
$version_arr = $content_arr["require"]["laravel/framework"];
return intval($version_arr[0]);
}
function insert_to_file($filename, $after_line, $text)
{
if(is_command_registered($filename,$text))
{
return;
}
$st = "";
$fp = fopen($filename, "r+");
while($buf = fgets($fp)){
$st .= $buf;
if(strpos($buf, $after_line) > -1) {
$st .= $text;
}
}
file_put_contents($filename, $st);
}
function replace_in_file($file, $from, $to)
{
$content = file_get_contents($file);
$res = str_replace($from, $to, $content);
file_put_contents($file, $res);
}