Skip to content

Commit c9ea10b

Browse files
committed
Distribute the project as a phar instead of source code.
1 parent 2937bab commit c9ea10b

File tree

6 files changed

+111
-55
lines changed

6 files changed

+111
-55
lines changed

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
# Do not include the following files when creating repo artifacts, e.g. the zip
44
/.* export-ignore
5+
/bin/build-phar.sh export-ignore
6+
/bin/phpmnd.src export-ignore
57
/build/ export-ignore
8+
/src/ export-ignore
69
/tests/ export-ignore
710
/CHANGELOG.md export-ignore
811
/CONTRIBUTING.md export-ignore

bin/build-phar.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
3+
if ! command -v box 2>&1 > /dev/null; then
4+
echo "It doesn't look like you have the box CLI installed for creating PHARs..."
5+
read -p "Would you like to install it now? (y/N) " YES_NO
6+
if [ "$YES_NO" != "y" ]; then
7+
echo "Please install https://github.com/box-project/box to continue."
8+
exit 1
9+
fi
10+
11+
wget -O box.phar "https://github.com/box-project/box/releases/latest/download/box.phar"
12+
wget -O box.phar.asc "https://github.com/box-project/box/releases/latest/download/box.phar.asc"
13+
14+
# Check that the signature matches
15+
gpg --keyserver hkps://keys.openpgp.org --recv-keys 41539BBD4020945DB378F98B2DF45277AEF09A2F
16+
gpg --verify box.phar.asc box.phar
17+
18+
rm box.phar.asc
19+
mv box.phar box
20+
chmod +x box
21+
22+
echo sudo mv box /usr/local/bin
23+
sudo mv box /usr/local/bin
24+
fi
25+
26+
# Run composer install to insure that everything is good.
27+
echo "Running a fresh \`composer install --no-dev\` to insure the latest dependencies are installed..."
28+
echo "Removing vendor/ and composer.lock ..."
29+
rm -v composer.lock
30+
rm -rf vendor
31+
composer install --no-dev
32+
33+
# Build the phar
34+
echo "Building phpmnd.phar..."
35+
box compile

bin/phpmnd

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,14 @@
11
#!/usr/bin/env php
22
<?php
33

4-
use Povils\PHPMND\Console\Application;
5-
use Povils\PHPMND\Container;
4+
declare (strict_types=1);
65

7-
if (\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) === false) {
8-
echo \PHP_EOL . 'PHPMND may only be invoked from a command line, got "' . \PHP_SAPI . '"' . \PHP_EOL;
9-
10-
exit(1);
11-
}
12-
13-
if (\version_compare('7.4.0', \PHP_VERSION, '>')) {
14-
\fwrite(
15-
\STDERR,
16-
\sprintf(
17-
'This version of PHPMND is supported on PHP 7.4.' . \PHP_EOL .
18-
'You are using PHP %s%s.' . \PHP_EOL,
19-
\PHP_VERSION,
20-
\defined('PHP_BINARY') ? ' (' . \PHP_BINARY . ')' : ''
21-
)
22-
);
23-
exit(1);
6+
if (!file_exists(__DIR__ . '/phpmnd.phar') || is_dir(__DIR__ . '/../vendor')) {
7+
// Run the internal phpmnd binary directly.
8+
require __DIR__ . '/phpmnd.src';
9+
exit;
2410
}
2511

26-
// PHPMND autoloading
27-
(static function (): void {
28-
if (\file_exists($autoload = __DIR__ . '/../../../autoload.php')) {
29-
// Is installed via Composer
30-
include_once $autoload;
31-
32-
return;
33-
}
34-
35-
if (\file_exists($autoload = __DIR__ . '/../vendor/autoload.php')) {
36-
// Is installed locally
37-
include_once $autoload;
38-
39-
return;
40-
}
41-
42-
\fwrite(
43-
\STDERR,
44-
<<<'ERROR'
45-
You need to set up the project dependencies using Composer:
46-
$ composer install
47-
You can learn all about Composer on https://getcomposer.org/.
48-
ERROR
49-
);
50-
51-
throw new RuntimeException('Unable to find the Composer autoloader.');
52-
})();
53-
54-
// Project (third-party) autoloading
55-
(static function (): void {
56-
if (\file_exists($autoload = \getcwd() . '/vendor/autoload.php')) {
57-
include_once $autoload;
58-
}
59-
})();
12+
Phar::loadPhar(__DIR__ . '/phpmnd.phar', 'phpmnd.phar');
6013

61-
(new Application(Container::create()))->run();
14+
require 'phar://phpmnd.phar/bin/phpmnd.src';

bin/phpmnd.src

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use Povils\PHPMND\Console\Application;
5+
use Povils\PHPMND\Container;
6+
7+
if (\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) === false) {
8+
echo \PHP_EOL . 'PHPMND may only be invoked from a command line, got "' . \PHP_SAPI . '"' . \PHP_EOL;
9+
10+
exit(1);
11+
}
12+
13+
if (\version_compare('7.4.0', \PHP_VERSION, '>')) {
14+
\fwrite(
15+
\STDERR,
16+
\sprintf(
17+
'This version of PHPMND is supported on PHP 7.4.' . \PHP_EOL .
18+
'You are using PHP %s%s.' . \PHP_EOL,
19+
\PHP_VERSION,
20+
\defined('PHP_BINARY') ? ' (' . \PHP_BINARY . ')' : ''
21+
)
22+
);
23+
exit(1);
24+
}
25+
26+
// PHPMND autoloading
27+
(static function (): void {
28+
if (\file_exists($autoload = __DIR__ . '/../../../autoload.php')) {
29+
// Is installed via Composer
30+
include_once $autoload;
31+
32+
return;
33+
}
34+
35+
if (\file_exists($autoload = __DIR__ . '/../vendor/autoload.php')) {
36+
// Is installed locally
37+
include_once $autoload;
38+
39+
return;
40+
}
41+
42+
\fwrite(
43+
\STDERR,
44+
<<<'ERROR'
45+
You need to set up the project dependencies using Composer:
46+
$ composer install
47+
You can learn all about Composer on https://getcomposer.org/.
48+
ERROR
49+
);
50+
51+
throw new RuntimeException('Unable to find the Composer autoloader.');
52+
})();
53+
54+
// Project (third-party) autoloading
55+
(static function (): void {
56+
if (\file_exists($autoload = \getcwd() . '/vendor/autoload.php')) {
57+
include_once $autoload;
58+
}
59+
})();
60+
61+
(new Application(Container::create()))->run();

box.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"main": "bin/phpmnd.src"
3+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"sort-packages": true
4040
},
4141
"bin": [
42-
"bin/phpmnd"
42+
"bin/phpmnd",
43+
"bin/phpmnd.phar"
4344
],
4445
"scripts": {
4546
"test": "phpunit",

0 commit comments

Comments
 (0)