forked from misterion/ko-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathko-package
More file actions
183 lines (157 loc) · 5.62 KB
/
ko-package
File metadata and controls
183 lines (157 loc) · 5.62 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
#!/usr/bin/env php
<?php
/**
* The MIT License
*
* Copyright (c) 2014 Nikolay Bondarenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* PHP version 5.4
*
* @package Ko
* @author Nikolay Bondarenko
* @copyright 2014 Nikolay Bondarenko. All rights reserved.
* @license MIT http://opensource.org/licenses/MIT
*/
use GetOpt\GetOpt;
use GetOpt\Option;
define('KO_PACKAGE_VERSION', '0.0.1');
$files = [
__DIR__ . '/../../../../autoload.php',
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../../autoload.php',
__DIR__ . '/../autoload.php',
__DIR__ . '/vendor/autoload.php'
];
foreach ($files as $file) {
if (file_exists($file)) {
break;
}
}
if (empty($file)) {
die(
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
'wget http://getcomposer.org/composer.phar' . PHP_EOL .
'php composer.phar install' . PHP_EOL
);
}
/** @noinspection PhpIncludeInspection */
require_once $file;
$cmdArguments = [
(new Option('p', 'path', GetOpt::REQUIRED_ARGUMENT))
->setDescription('Path to application code')
->setValidation(function($value) {
return file_exists($value);
}),
(new Option('o', 'output', GetOpt::OPTIONAL_ARGUMENT))
->setDefaultValue('app.phar')
->setDescription('Output file name'),
(new Option('v', 'version', GetOpt::OPTIONAL_ARGUMENT))
->setDefaultValue('1.0.0')
->setDescription('Application version'),
(new Option('n', 'name', GetOpt::OPTIONAL_ARGUMENT))
->setDefaultValue('MyApp')
->setDescription('Application name'),
(new Option('c', 'composer', GetOpt::OPTIONAL_ARGUMENT))
->setDefaultValue(false)
->setDescription('Use composer.json file for name and version'),
(new Option('e', 'exclude', GetOpt::OPTIONAL_ARGUMENT))
->setDescription('Excluded folders like logs, test, etc.'),
];
$opts = new GetOpt($cmdArguments);
try {
$opts->parse();
} catch (\UnexpectedValueException $e) {
showAbout($opts);
};
if (empty($opts['path'])) {
showAbout($opts);
}
$files = [];
$exclude = !empty($opts['exclude']) ? explode(',', $opts['exclude']) : [];
/** @var SplFileInfo[] $rd */
$rd = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($opts['path']));
foreach ($rd as $file) {
if (!$file->isFile()) {
continue;
}
$path = $file->getPathname();
foreach ($exclude as $ignorePattern) {
if (strpos($path, $ignorePattern) === 0) {
continue 2;
}
}
$files[substr($path, 2)] = $path;
}
$composerFile = $opts['path'] . 'composer.json';
if ($opts['composer'] == true && file_exists($composerFile)) {
$data = json_decode(file_get_contents($composerFile), true);
$name = $data['name'];
$version = !empty($data['version']) ? $data['version'] : $opts['version'];
} else {
$name = trim($opts['name']);
$version = trim($opts['version']);
}
$targetFile = new SplFileInfo($opts['output']);
$alias = $targetFile->getFilename();
$phar = new Phar($targetFile->getPathname(), 0, $alias);
$phar->startBuffering();
$phar->setSignatureAlgorithm(Phar::SHA1);
$phar->buildFromIterator(new ArrayIterator($files));
$phar->setStub(getStub($name, $version, $alias));
//INFO Comment due behavior http://stackoverflow.com/questions/29413013/phar-internal-corruption-crc32-mismatch-during-process-fork
//$phar->compressFiles(Phar::GZ);
$phar->stopBuffering();
$sig = $phar->getSignature();
echo sprintf('%s signature %s (%s)', $opts['output'], $sig['hash'], $sig['hash_type']) . PHP_EOL;
/**
* @param Getopt $opts
*/
function showAbout($opts) {
echo sprintf("ko-package version %s", KO_PACKAGE_VERSION) . PHP_EOL;
echo $opts->getHelpText();
exit;
}
function getStub($name, $version, $alias) {
$stub = <<<EOF
#!/usr/bin/env php
<?php
/**
* APC protection code from https://github.com/acoulton/composer/commit/9ca99762f9e41dec12be28b346f6fb71f72d85a5
*/
if (extension_loaded('apc') && ini_get('apc.enable_cli') && ini_get('apc.cache_by_default')) {
if (version_compare(phpversion('apc'), '3.0.12', '>=')) {
ini_set('apc.cache_by_default', 0);
} else {
fwrite(STDERR, 'Warning: APC <= 3.0.12 may cause fatal errors when running application.'.PHP_EOL);
fwrite(STDERR, 'Update APC, or set apc.enable_cli or apc.cache_by_default to 0 in your php.ini.'.PHP_EOL);
}
}
Phar::mapPhar('$alias');
require_once 'phar://$alias/vendor/autoload.php';
\$app = new \Ko\Worker\Application();
\$app->setProcessManager(new \Ko\ProcessManager())
->setVersion('$version')
->setName('$name')
->run();
__HALT_COMPILER();
EOF;
return $stub;
}