Skip to content

Commit 5b4433b

Browse files
committed
README: Add Pitfalls section
1 parent 129f910 commit 5b4433b

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ PHP Code compiler - Phar executable compiling utility
1818
- [Requirements](#requirements)
1919
- [Quick install](#quick-install)
2020
- [Github Action](#github-action)
21+
- [Pitfalls](#pitfalls)
22+
- [Shebang line in main script](#shebang-line-in-main-script)
23+
- [Local versus compiled files: missing file](#local-versus-compiled-files-missing-file)
24+
- [Local versus compiled files: name collision](#local-versus-compiled-files-name-collision)
25+
- [Size too big](#size-too-big)
2126
- [License](#license)
2227

2328
## Usage
@@ -177,6 +182,100 @@ jobs:
177182
run: bin/acme --version
178183
```
179184
185+
## Pitfalls
186+
187+
Here is a (non-exhaustive) list of the most common mistakes related to PHAR compiling.
188+
189+
### Shebang line in main script
190+
191+
Since the main (entrypoint) script will be **included** in the PHAR stub, it must not contain any shebang line, otherwise this line will be treated as text and printed to standard output when invoking the compiled PHAR.
192+
193+
_Example: Invoking a version of `phpcc` compiled with a shebang line in `bin/compile.php`_
194+
195+
```
196+
$ bin/phpcc --version
197+
#!/usr/bin/env php
198+
PHP Code Compiler version 1.3.0-dev
199+
```
200+
201+
### Local versus compiled files: missing file
202+
203+
Let's consider the following tree (all files required by the app)
204+
205+
```
206+
bin/acme.php
207+
src/Command/Acme.php
208+
src/Command/SomeClass.php
209+
lib/Ufo.php
210+
```
211+
212+
Compile it (Oops... one Unknown File Object has not been included)
213+
214+
```
215+
phpcc -e bin/acme.php -f bin/acme.php -d src/ -o bin/acme
216+
```
217+
218+
#### Problem
219+
220+
Launching the `bin/acme` compiled archive should raise an error because of the missing file.
221+
222+
Well...not. What happens here then ?
223+
224+
If the `bin/acme` compiled archive stays in its place,the `lib/Ufo.php` can still be found from its point of view.
225+
226+
#### Solution
227+
228+
Always move the compiled executable **out** of the project's working directory before testing it.
229+
230+
231+
### Local versus compiled files: name collision
232+
233+
Eg:
234+
235+
```php
236+
require "vendor/autoload.php"
237+
```
238+
239+
Chances are, there might be such a `vendor/autoload.php` file in the project to be compiled.
240+
241+
#### Problem
242+
243+
From the compiled app point of view, `vendor/autoload.php` refers to a relative path in the PHAR archive.
244+
245+
#### Workaround
246+
247+
The phpcc execution dir (i.e the compiled project's top directory) must be added first in the include path.
248+
249+
For example in the main entrypoint script:
250+
251+
```php
252+
// bin/main.php
253+
254+
// ensure we load the execution directory's autoload, not the
255+
// one included in the compiled phar executable
256+
set_include_path(getcwd() . PATH_SEPARATOR . get_include_path());
257+
258+
require 'vendor/autoload.php';
259+
260+
// ...
261+
```
262+
263+
### Size too big
264+
265+
Many projects include some dev libraries, for unit test, local data seeding or code inspection.
266+
267+
Fact is, some of those libs have **A LOT** of dependencies... Hence the `vendor` directory, which is usually included in the archive is really **HUGE**.
268+
269+
Q: How to remediate then ?
270+
271+
A: Before compiling, ensure the `vendor` directory does not contains any dev library:
272+
273+
```
274+
composer install --no-dev
275+
phpcc -e bin/acme.php -f bin/acme.php -d src/:php -d vendor:php -d vendor:yaml -o bin/acme
276+
```
277+
278+
180279
## License
181280

182281
Licensed under the [MIT License](LICENSE).

0 commit comments

Comments
 (0)