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

Using ide-helper outside the framework with just illuminate/database #756

Closed
mfn opened this issue Jan 23, 2019 · 5 comments
Closed

Using ide-helper outside the framework with just illuminate/database #756

mfn opened this issue Jan 23, 2019 · 5 comments
Labels

Comments

@mfn
Copy link
Collaborator

mfn commented Jan 23, 2019

I'm creating this issue because maybe it helps others which are interested in a similar solution and maybe there's interest in making the ide-helper code slightly more agnostic.

My scenario:

But working with models without ide-helper is very annoying (also for static analysis like phpstan) so I was seeking a solution to make it possible to use ide-helper for two things:

  • models phpdoc generation
  • metadata generation for PhpStorm

I did not evaluate the generation of Facade data as I'm not using them.

To make the models command work

  • use of base_path() global helper from the framework
    In \Barryvdh\LaravelIdeHelper\Console\ModelsCommand::loadModels it uses the global helper base_path() which isn't available outside of the framework (it's defined in vendor/laravel/framework/src/Illuminate/Foundation/helpers.php)
    • My solution was to write my own global base_path() which I only load in the context of generating the models which creates me the correct path
  • Use of the Config service
    Multiple times the $this->laravel['config'] service is access to load the configuration. However I'm not using illuminate/config either. I've created a one-shot anonymous class with my code which emulates the most basic methods to make this work. I used below code and and bound it to my container via $laravelContainer->instance('config', …); in below code Arr::get can be used because of the dependencies of other packages to illuminate/support:
            return new class($config) implements ArrayAccess {
                /** @var array */
                private $config;
    
                public function __construct(array $config)
                {
                    $this->config = $config;
                }
    
                public function get($key, $defaultVaue = null)
                {
                    return Arr::get($this->config, $key, $defaultVaue);
                }
    
                public function offsetExists($offset)
                {
                    throw new RuntimeException('Not implemented because we didn\'t need it yet');
                }
    
                public function offsetGet($offset)
                {
                    return $this->get($offset);
                }
    
                public function offsetSet($offset, $value)
                {
                    throw new RuntimeException('Not implemented because we didn\'t need it yet');
                }
    
                public function offsetUnset($offset)
                {
                    throw new RuntimeException('Not implemented because we didn\'t need it yet');
                }
            };
    • Of course this means you need to somehow provide the configuration to the class which is custom in my case
  • The final bootstrap to invoke the command was this:
    $fileSystem = new Filesystem();
    $command = new ModelsCommand($fileSystem);
    $command->setLaravel($laravelContainer);
    $command->run(
      new ArrayInput(['--write' => true, '--reset' => true]),
      new ConsoleOutput()
    );

To make the meta command work

  • I used the same config approach as outlined above
  • The command uses the https://github.com/illuminate/view component to render the final metafile which I don't use, either. I therefore created a minimal anonymous class I passed to the MetaCommand constructor; it's looks like a hack but works :
    return new class() {
        public function make(string $view, array $bindings)
        {
            return new class($view, $bindings) {
                private $view;
                private $bindings;
    
                public function __construct(string $view, array $bindings)
                {
                    $this->view = $view;
                    $this->bindings = $bindings;
                }
    
                public function render(): string
                {
                    $viewFile = base_path("vendor/barryvdh/laravel-ide-helper/resources/views/$this->view.php");
                    extract($this->bindings, EXTR_SKIP);
    
                    ob_start();
                    require $viewFile;
    
                    return ob_get_clean();
                }
            };
        }
    };
  • Additionally, I've my own ::make() method which I wanted to add to the generated file. I couldn't find a way to change \Barryvdh\LaravelIdeHelper\Console\MetaCommand::$methods so I created my own MyMetaCommand extends MetaCommand class and just override the protected $methods property
  • After that, the final bootstrap call looks like this:
    $fileSystem = new Filesystem();
    $command = new MyMetaCommand($fileSystem, $viewClass, $configClass);
    $command->setLaravel($laravelContainer);
    $command->run(
        new ArrayInput([]),
        new ConsoleOutput()
    );

Finishing words

I didn't really knew upfront if I could make it work. I'm really happy it did, having known the ide-helper for years form real Laravel project I truly didn't wanted to miss it!

I think the only suggestions I would have:

  • don't use base_path() inside \Barryvdh\LaravelIdeHelper\Console\ModelsCommand, but pass it as string $basePath property to the commands constructor via \Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider
  • Provide a way to alter \Barryvdh\LaravelIdeHelper\Console\MetaCommand::$methods once the command is created.
  • Unsolved: in \Barryvdh\LaravelIdeHelper\Console\ModelsCommand::createPhpDocs the @mixin \Eloquent is harcoded. I'm not even using Facades so if I still would would the mixin I would need them to point both the Eloquent\BuilderandQuery\Builder`; I left it as it for now

Otherwise: really very great package and I hope I continues to work also on the standalone versions in the future.

PS: feel free to close the issue, I merely wanted to document it for my future self/others

@stale
Copy link

stale bot commented Jul 29, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this issue is still present on the latest version of this library on supported Laravel versions, please let us know by replying to this issue so we can investigate further.
Thank you for your contribution! Apologies for any delayed response on our side.

@stale stale bot added the stale label Jul 29, 2020
@mfn mfn mentioned this issue Jul 31, 2020
@stale stale bot closed this as completed Sep 27, 2020
@mfn
Copy link
Collaborator Author

mfn commented Sep 28, 2020

😱

@dennisameling
Copy link

dennisameling commented May 2, 2022

This still helped me in 2022 😄 thanks!!

@mfn
Copy link
Collaborator Author

mfn commented May 2, 2022

Nice!

Also: I'm still using it (which also means that part has not migrated to Laravel, which is the sad part)

@ricardoapaes
Copy link

@mfn I created a library with your recommendations, for me it is working correctly. Thanks for the contributions.

https://github.com/likesistemas/eloquent-ide-helper

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

No branches or pull requests

3 participants