mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.26k stars 217 forks source link

Standalone usage without relying on app() and config() #262

Closed liepumartins closed 5 years ago

liepumartins commented 5 years ago

Is your feature request related to a problem? Please describe. I would like to use caching without Laravel, just eloquent aka illuminate/database component in a different framework/vanilla PHP. Package requires app() and config() helper methods and possibly other Laravel internals.

Describe the solution you'd like Is it viable for package to rely on a config file of its own? For Laravel context it could use a default config file, without requiring modifications.

Describe alternatives you've considered Other caching packages either are too simple/unmaintained or also relies on Laravel functions to be present. I have considered forking and making changes I require, but I do not feel 100% comfortable with all the magic methods Laravel uses.

mikebronner commented 5 years ago

@liepumartins Thank you for the suggestions. I will consider this moving forward, and investigate what we can do.

mikebronner commented 5 years ago

@liepumartins This should now be fixed in release 0.5.6. Could you give it a try and report back? Thanks!

liepumartins commented 5 years ago

Fatal error: Uncaught ReflectionException: Class config does not exist in /home/vagrant/site/vendor/illuminate/container/Container.php on line 790

Excerpt from stack trace:

Illuminate\Container\Container->make( ) .../Caching.php:274
Illuminate\Container\Container->resolve( )  .../Container.php:615
Illuminate\Container\Container->build( )    .../Container.php:667
__construct ( ) .../Container.php:790
mikebronner commented 5 years ago

@liepumartins Are you sure you pulled in the latest version? Please

liepumartins commented 5 years ago

Yes, I am sure.

             "name": "genealabs/laravel-model-caching",
            "version": "0.5.6",
            "source": {
                "type": "git",
                "url": "https://github.com/GeneaLabs/laravel-model-caching.git",
                "reference": "6cb0629b114e547e8e315dbdf4d953968d429480"
            },

This line reflectively tries to get config https://github.com/GeneaLabs/laravel-model-caching/blob/fc1389452cc39cc5c6fd1105cfc1391df6cea2f2/src/Traits/Caching.php#L274

Other parts of stacktrace are irrelevant.

#   Time    Memory  Function    Location
1   0.0016  419232  {main}( )   .../index.php:0
2   0.1255  1049136 Illuminate\Database\Eloquent\Model::first( )    .../index.php:57
3   0.1255  1049192 Illuminate\Database\Eloquent\Model::__callStatic( ) .../index.php:57
4   0.1258  1051656 eloquent\MyModel->first( )  .../Model.php:1626
5   0.1258  1051712 eloquent\MyModel->__call( ) .../Model.php:1626
6   0.1258  1051712 eloquent\MyModel->__call( ) .../Caching.php:22
7   0.1258  1051712 eloquent\MyModel->newQuery( )   .../Model.php:1614
8   0.1258  1051712 eloquent\MyModel->newQueryWithoutScopes( )  .../Model.php:953
9   0.1258  1051712 eloquent\MyModel->newModelQuery( )  .../Model.php:1000
10  0.1473  1253504 eloquent\MyModel->newEloquentBuilder( ) .../Model.php:964
11  0.1473  1253504 eloquent\MyModel->isCachable( ) .../ModelCaching.php:105
12  0.1473  1253824 Illuminate\Container\Container->make( ) .../Caching.php:274
13  0.1473  1253824 Illuminate\Container\Container->resolve( )  .../Container.php:615
14  0.1473  1254280 Illuminate\Container\Container->build( )    .../Container.php:667
15  0.1474  1254392 __construct ( ) .../Container.php:790
mikebronner commented 5 years ago

@liepumartins To properly debug, the entire stack trace is needed in order to follow which code was ran in each successive step.

Thank you for posting the relevant bit of code that is causing the issue. With that I can now employ a fix. The reason I ask to double-check the version of the package, is because often people report bugs and assume they have the latest, when in fact composer just installed a cached version.

I will try to get this fixed soon, but probably won't be for a few more days, as I need to get other priorities finished first.

mikebronner commented 5 years ago

@liepumartins In looking at the problem now in detail, I don't see a way to not rely on the config in the application container. I am open to PRs if you can figure something out. Any suggestions?

This package depends on Laravel's configuration functionality to load defaults, as well as allow customization. This package is not intended to be used outside of Laravel, but I will be happy to make some concessions to facilitate that, if it does not require restructuring and rearchitecting of the package.

liepumartins commented 5 years ago

Looks like it is not feasible or requires too much effort to

Now I am looking to integrate my project as a sub-project in Laravel/Lumen environment. Initial attempts look promising. Of course the overhead will be noticeable, but hey, it is PHP. :)

Thank you for your work. Sorry, I have wasted your time.