-
-
Notifications
You must be signed in to change notification settings - Fork 684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Help wanted] Add PHAR #319
Conversation
bin/rector
Outdated
@@ -1,5 +1,5 @@ | |||
#!/usr/bin/env php | |||
<?php declare(strict_types=1); | |||
<?php |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? 😢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunatelly it's needed for PHAR to make work.
See PHPUnit:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a way to revert this 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 on the move. But because the difference between the code actually tested and the code shipped in the PHAR (there's a transformation in-between + you filter some files), I highly recommend to have a couple of end to end tests to ensure the PAHR is working correctly
composer.json
Outdated
@@ -24,23 +24,24 @@ | |||
"webmozart/assert": "^1.2" | |||
}, | |||
"require-dev": { | |||
"humbug/box": "^3.0@dev", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are using bamarni/composer-bin-plugin
I would recommend you to install humbug/box
with it as the later doesn't need to execute any of your code (same for php-scoper)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you automate that to don't bother user with that?
I've added it to composer.json
script, but it takes too much perfromance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could do composer bin {box|php-scoper} install
in your composer script. Even if unnecessary once already installed it's like not even half a second. Alternatively I'm using Makefiles lately for this kind of stuff. You can see some examples on alice, AliceDataFixture, php-scoper or box: just do make test
or make build
and everything is taken care of
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would still bother the user.
Maybe add bash script, that would check if vendor-bin directory for package exist, and install it only if not. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah you can definitely have a compile
bash script or something instead.
Remember most of your users/contributors won't have to build the PHAR so I don't think it's gonna bother them much. But otherwise a bash/Makefile gives more flexibility on how to do things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll look on it.
Remember most of your users/contributors won't have to build the PHAR so I don't think it's gonna bother them much. But otherwise a bash/Makefile gives more flexibility on how to do things
Speed matter for us as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah but I think the time taken by composer to dump the autoloader (which is the only thing it will do if the packages are already installed) is negligible compared to the rest.
IMO the places of this process where perf. can be improved:
- The scoping itself, i.e. the core of PHP-Scoper
- PHP-Scoper as standalone as we could leverage parallelism for example. But I don't think I'll invest much time on it tbh. I'm more interesting in integrating with Box which case the parallelisation already happens there
- Box itself
- PHP-Parser
Any optimisation in any of those above is more likely to have a higher impact that the half second spent on dumping the autoloader.
That said if it's bugging you, there's nothing wrong with using bash/Makefile to handle this
composer.json
Outdated
"phpstan": "vendor/bin/phpstan.phar analyse packages src tests --level max --configuration phpstan.neon" | ||
"phpstan": "vendor/bin/phpstan.phar analyse packages src tests --level max --configuration phpstan.neon", | ||
"build-phar": [ | ||
"vendor/bin/php-scoper add-prefix --output-dir=build --force", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove the dev dependencies first there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did that before but:
- it took so much time to remove, install dev, then install all
- it's covered by finder already
- it's not possible due to php-cs-fixer, that has
/src
file in/tests
dir; which fails oncomposer dump
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Finder doesn't cover that. You are ignoring a library test files with the current config but not the whole dev library.
Yeah PHP-CS-Fixer is a bit annoying here cf. https://github.com/api-platform/schema-generator/blob/master/scoper.inc.php#L39-L42 and PHP-CS-Fixer/PHP-CS-Fixer#3462
Although you are right you can also ignore this. This is something that will be handled in Box at some point which will be way more performant for PHP-Scoper as well as it will only scope the files filtered by Box
scoper.inc.php
Outdated
// that this does not work with functions or constants neither with classes belonging to the global namespace. | ||
// | ||
// Fore more see https://github.com/humbug/php-scoper#whitelist | ||
'whitelist' => [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can remove that one
box.json
Outdated
@@ -0,0 +1,16 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can optimise it later but I would recommend to enable compression ("compression": "GZ"
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That failed for me before, but now works. Thanks 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC this can fail if the PHAR is too big (with the current implementation) but this should be ok unless you have over 10K files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was missing argument in constructor error, but I can't reproduce it now
So first step - building phar file - is done. Now we need to test it, that it works :) Any idea for simple test, that could be added to test suite and run on Travis? |
@theofidry Thanks for the feedback, much appreciated from the package's maintainer/author. Do you have some simple idea how to test |
composer.json
Outdated
"phpstan": "vendor/bin/phpstan.phar analyse packages src tests --level max --configuration phpstan.neon" | ||
"phpstan": "vendor/bin/phpstan.phar analyse packages src tests --level max --configuration phpstan.neon", | ||
"build-phar": [ | ||
"composer bin php-scoper require --dev humbug/php-scoper", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@theofidry Here is the bin install used
You're welcome. My recommendation for testing is to have a couple of e2e tests, i.e. tests where you run your application with the binary (be it from the source The way I did it with PHP-Scoper is relatively simple: I have a few directories where you have the classes/composer.json and stuff and a file containing the expected output. I run PHP-Scoper and Box to build a PHAR and test if the output is the same as the expected one. You can find the examples there. For box the e2e testing is a bit lighter, I have a few integration tests to make sure Box works and for testing the PHAR I simply try to build the Box PHAR with the PHAR. In other words I generate the PHAR once (with the regular binary |
@theofidry Thank you, that seems like great simple trick! I'll investigave those sources. |
a0ea591
to
ede205d
Compare
tests/Phar/PharTest.php
Outdated
{ | ||
public function test(): void | ||
{ | ||
$this->assertTrue(file_exists(__DIR__ . '/../../rector.phar')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe assertFileExists
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is WIP. I have new problems with buidling phar :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll follow your discussion with @theofidry and see what I can help 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍
fcd1fe2
to
b62a43e
Compare
@theofidry I've added detailed steps with prefixing and phar building to Travis to show the error. I have no idea how to fix that or what it means, being stuck for hours. Could you give me some hints? It would save me https://travis-ci.org/rectorphp/rector/builds/342723839#L817 |
I'll have a look in a bit |
Actually maybe a bit later. I need to fix a couple of things in php-scoper and box but that won't take too long. I'll have a look at Rector today still |
@theofidry No problem, thank you for any help you find time for |
My last finding...
composer dump --working-dir=build But no classes from I see in every |
I'm checking that now :) |
Is it possible, that |
No that part definitely works. I think I got a lead on the issue it's just frigging slow (the scoping takes 1-2min each time...) |
Ok I found too bugs I'll need to fix in PHP-Scoper:
use Foo\Bar;
$x = new Bar(); The name resolver will resolve the The bug here is that the name resolver doesn't take into account the types. It looks at the namespace and use statement, but without any further check, so if you have: use Foo\Bar();
bar(); // Call the function not the class It will work in PHP (provided You can see some of my changes there. If you have any question regarding them feel free to ask |
86ec694
to
95871c4
Compare
Yeah arrays are not supported as well yet I should add it. That said prefixing the autoloader is not supported and won't ever, it requires serious levels of hacking and a tight coupling to Composer internals |
I see, I guess this would be fixed by the other 2 issues you mentioned. |
Did some PRs so it should work better now. Also the patcher I registered shouldn't be needed anymore either |
Thanks for the work! When I rerun the whole script It fails on PHP error for me: PHP Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in phar:///var/www/Rector/rector.phar/bin/rector on line 3 Related code in Phar #!/usr/bin/env php
<?php # ← here is extra space, that might cause that, at least what I saw in Stackoverflow answer
namespace PhpScoper5a89bf1332dcc; |
That’s weird, it’s not what I’m getting on my machine at all. Do you try my
bin/compile?
…On Sun 18 Feb 2018 at 18:05, Tomáš Votruba ***@***.***> wrote:
Thanks for the work!
When I rerun the script
<https://github.com/rectorphp/rector/pull/319/files#diff-b5d0ee8c97c7abd7e3fa29b9a27d1780R71>
It fails on PHP error for me:
PHP Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in phar:///var/www/Rector/rector.phar/bin/rector on line 3
Related code in Phar bin/rector looks like this:
#!/usr/bin/env php<?php # ← here is extra space, that might cause that, at least what I saw in Stackoverflow answernamespace PhpScoper5a89bf1332dcc;
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#319 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AE76gYC7t-OkIJnzuhNxpwhyXZe2leGnks5tWGZ5gaJpZM4SF4oO>
.
|
I run the script I linked. What should I run instead? |
No that’s what I did as well. Very strange
…On Mon 19 Feb 2018 at 07:42, Tomáš Votruba ***@***.***> wrote:
I run the script I linked. What should I run instead?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#319 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AE76gSS1Fl9omYoWJp5IQ7QrUw84Y8vpks5tWSXlgaJpZM4SF4oO>
.
|
Hey @TomasVotruba , what is missing to make it work? Prefixing? |
Hey, well, build doesn't work yet neither. |
Building phar itself works for me. |
@mssimi Really? What does it mean? We can close this then. Could you send new PR please? |
@TomasVotruba can we discuss it on google hangouts? |
@mssimi Sure 👍 |
@TomasVotruba check this and let me know what you think. |
@mssimi That looks awesome 👍 I just love green colour :D PR please! |
@TomasVotruba I was loading wrong autoloader. Now I noticed problem which we need to solve to make it work. Method |
Symfony is definitely not compatible with PHAR, Loaders use glob, Kernel uses glob, Finder uses glob. What do you propose @TomasVotruba to overcome this issue? |
Not sure, after bit Googling: https://github.com/cakephp/phinx/pull/1042/files |
I found this solution too, but Symfony logic is more complex as it uses patterns, we will have to re-implement this class. Maybe we can discuss it on Symfony's github. |
That is doable. I do simalar changes in ECS: https://github.com/Symplify/Symplify/blob/master/packages/EasyCodingStandard/src/DependencyInjection/EasyCodingStandardKernel.php#L96 |
have just built this phar manually and it work on my symfony project! performance is a problem, but that's another issue i guess. |
@bendavies How you solved autowiring issue? |
I'm not seeing any... |
Are you sure PHAR is autoloading files inside PHAR not outside? |
no idea. I just know it's working and upgrading my symfony 2.8 project to 3.0 very nicely... |
Maybe you could provide PHAR you compiled so we can check it. |
i checked out this branch and ran |
This branch is not working yet, compilator does not include vendor at all, you actually probably run non-phar files. |
Same thing happend to me. |
you are right. it didn't autoload from the phar. |
Continues in #389 |
rectorphp/rector-src@1a87a3d [TypeDeclaration] Prevent checking class like has external fully qualifieds when class like is null (#319)
…ifieds when class like is null (rectorphp#319)
Closes #177
On the Menu
prepare composer script to install and make phar
prepare own phar build script
make local
rector.phar
run workbox and php-scoper shown as projects WIP not usable in state to this day
Related Sources