Skip to content

Commit 84e7830

Browse files
authored
Merge pull request #1 from PHPExperts-Contribs/distribute_as_phar
Distribute as phar instead of source code.
2 parents 2937bab + 73e4bd6 commit 84e7830

File tree

11 files changed

+197
-115
lines changed

11 files changed

+197
-115
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
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
912
/phpunit.xml.dist export-ignore
13+
/composer.build.json export-ignore
14+
/composer.release.json export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
/build
33
/vendor
44
/.phpunit.result.cache
5+
/composer.release.json
56
/composer.lock

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ Afterwards make sure you have the global Composer binaries directory in your ``P
7474
```bash
7575
$ export PATH="$PATH:$HOME/.composer/vendor/bin"
7676
```
77+
### For Development
78+
79+
Because this repo is now a phar-first repo, you need to manually use the dev composer.json:
80+
81+
```
82+
mv composer.json composer.release.json
83+
cp composer.build.json composer.json
84+
composer install
85+
```
86+
87+
If you wish to build the phar:
88+
89+
```
90+
./bin/build-phar.sh
91+
```
92+
93+
If you wish to test the phar file, it will not work if there is an existing vendor directory.
7794

7895
## Usage Example
7996

@@ -178,6 +195,11 @@ $percent = $number / intval(100);
178195
```
179196
would mark 100 as not magic.
180197

198+
## Installation
199+
200+
201+
202+
181203
## Contributing
182204

183205
Please see [CONTRIBUTING.md](CONTRIBUTING.md) for more information.

bin/build-phar.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
git checkout composer.json
32+
mv composer.json composer.release.json
33+
cp composer.build.json composer.json
34+
composer install --no-dev
35+
36+
37+
# Build the phar
38+
echo "Building phpmnd.phar..."
39+
box compile
40+
41+
mv composer.release.json composer.json
42+
rm -rf vendor

bin/createPhar

Lines changed: 0 additions & 56 deletions
This file was deleted.

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.phar

2.34 MB
Binary file not shown.

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.build.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "povils/phpmnd",
3+
"type": "application",
4+
"description": "A tool to detect Magic numbers in codebase",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Povilas Susinskas",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"support": {
13+
"issues": "https://github.com/povils/phpmnd/issues"
14+
},
15+
"require": {
16+
"php": "^7.4 || ^8.0",
17+
"composer-runtime-api": "^2.0",
18+
"nikic/php-parser": "^4.18 || ^5.0",
19+
"php-parallel-lint/php-console-highlighter": "^1.0",
20+
"phpunit/php-timer": "^2.0 || ^3.0 || ^4.0 || ^5.0 || ^6.0 || ^7.0",
21+
"symfony/console": "^4.4 || ^5.0 || ^6.0 || ^7.0",
22+
"symfony/finder": "^4.4 || ^5.0 || ^6.0 || ^7.0"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "^9.6",
26+
"squizlabs/php_codesniffer": "^2.8.1||^3.5"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"Povils\\PHPMND\\": "src/"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"Povils\\PHPMND\\Tests\\": "tests/"
36+
}
37+
},
38+
"config": {
39+
"sort-packages": true
40+
},
41+
"bin": [
42+
"bin/phpmnd",
43+
"bin/phpmnd.phar"
44+
],
45+
"scripts": {
46+
"test": "phpunit",
47+
"cs-check": "phpcs -p --standard=PSR2 --runtime-set ignore_warnings_on_exit 1 src tests --ignore=tests/Fixtures/Files",
48+
"cs-fix": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests --ignore=tests/Fixtures/Files"
49+
}
50+
}

0 commit comments

Comments
 (0)