Skip to content
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

Custom error hook #24

Closed
arxeiss opened this issue Apr 6, 2020 · 7 comments · Fixed by #30
Closed

Custom error hook #24

arxeiss opened this issue Apr 6, 2020 · 7 comments · Fixed by #30

Comments

@arxeiss
Copy link

arxeiss commented Apr 6, 2020

Thank you for keeping this tool alive, it is amazing! But I would like to propose a custom error hook or some callback which I can use to modify error before proceeding. This is the reason

Laravel & Blade

Laravel FW uses Blade as a template, which is then compiled into PHP files. So I'm running this code

php artisan view:cache # Compile all Blade templates
./vendor/bin/parallel-lint ./storage/framework/views

But if there is an error in Blade template, it shows error like this:
Parse error: ./storage/framework/views/b89d10e4ca8386fb9f234e0b5a62798e71014114.php:66 Which says nothing, because it points into already compiled. However, there is a way how to map this error back into the original Blade file.

So if there would be some possible callbacks, I would use it to modify error back to the original file and line.


Is there a way how to make this possible? It would maybe require some config file instead of CLI arguments only. If you don't want to require any other package for config parsing (like Yaml etc), maybe just include PHP file with an array.

@grogy
Copy link
Member

grogy commented Apr 8, 2020

Thank you. You have interesting trouble :-)

Can you send me how Blade map errors from compiled template to original file?

Thank you

@arxeiss
Copy link
Author

arxeiss commented Apr 8, 2020

I just had an idea about it, because there is a package https://github.com/facade/ignition which does that. If you have an error in Blade, it shows on Error 500 the original line and a file. I was thinking if it would be possible to hook it into this tool. However, it can be challenging.

Step 1: Requires no additional packages:
Every compiled Blade template has a comment on the last line, which contains something like this
<?php /**PATH /var/www/.../layout.blade.php ENDPATH**/ ?> So it is enough to parse the file, take this path and print.

Step 2 (maybe impossible) Requires the Blade compiler:
The mentioned package basically runs compilation of the Blade template again with a flag, which puts into the final code some debugging info (Comments in PHP). Then they just pattern match line in those debug info. The source Blade file we already know from Step 1.


Edit: I tried to dig deeper and I found this: BladeSourceMapCompiler.php which is doing it.

@grogy
Copy link
Member

grogy commented Apr 16, 2020

I mean that support for this concrete library is not good idea.

What about universal callback? Something like:

./vendor/bin/parallel-lint --fail-callback ./my-custom-blade-file.php ./storage/framework/views When check failed then will be called file ./my-custom-blade-file.php

Structure for ./my-custom-blade-file.php:


...
class FailedBladeCallback implements \JakubOnderka\FailCallback` {
   public function say($inputFromPhpParallelLint) : something {
       ...
   }
}

What do you mean? Can it solve your trouble? I think about universal solution. Solution for extend in future.

@arxeiss
Copy link
Author

arxeiss commented Apr 16, 2020

Looks great, I never thought about implementing any library directly. Some universal hook like you are suggesting is the correct way I think. So yes, I agree. Thank you

Just some ideas about your approach

  1. If you include a file, you also have to know which class to use. So name of the file should be the same as class name.
  2. Or another possibility is to create a PHP config file, in which you can create an instance and pass it back.

@grogy
Copy link
Member

grogy commented Apr 17, 2020

I am sorry, I was mean that you can add it into tool :-)

  1. I agree. What about inicialization? When class need dependencies in construction.
  2. You think something like service definition? Can you say any example, please?

@arxeiss
Copy link
Author

arxeiss commented Apr 17, 2020

Sorry, I'm confused now a little bit. You want me to add this custom callback option into codebase via Pull Request? I can do this. Or which tool you meant?
For sure, I don't want to implement any concrete library into this repo/tool.

About that config possibility, I thought something like this. Similar approach using FTP Deployment.
./vendor/bin/parallel-lint --config config.php
where config.php file will return an array:

class FailedBladeCallback implements \JakubOnderka\FailCallback` {
   public function say($inputFromPhpParallelLint) : something {
       ...
   }
}

return [
    'paths' => ['./app', './storage/framework/views', './bootstrap/app.php'],
    'json_output' => true,
    'fail_callback' => new FailedBladeCallback,
];

@grogy
Copy link
Member

grogy commented Apr 18, 2020

When do you have a time, please send PR - draft. Together we can improve it :-)

Config - this is second problem, when you can add it, please send another PR. Config file and parameters can be compatible like PHPStan tool, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants