I started a new project late in 2014 which I knew would be fertile ground to keep pace with the what's happening in Laravel 5. It's been a great learning experience merging in changes from the upstream Laravel project branch as they happened.
The keen observer might have noticed however that Taylor made a commit on December 4th that "broke" support for packages designed to target Laravel 4.
Patience is key, so I was happy to continue waiting for the next great thing.
In Laravel 4, packages allow their configurations to be overridden per environment. A package can ship a default configuration which can then be copied into the project as a template to be combined with the original's settings at runtime. Think of it as a really tricked out array_merge_recursive.
By removing ServiceProvider::package, what's being conveyed is that the automatic cascade behaviour is now deprecated.
This might seem distressing, but don't worry. Any behaviours you've created that might have depended on arrays are still good. It's just more deliberate - and luckily enough, a great discussion sparked up on twitter yesterday which helped clear a way forward.
(I said it before, but I'll say it again, I really appreciate Taylor's patience as I muddled through understanding the changes.)
I was a bit confused at this point, because I was expecting there to be some kind of magical mechanism between the package and the project still. Not realizing of course that rather than expecting the two to couple, you simply leverage your project's ServiceProvider to do any customizations. I had been creating project-level ServiceProviders in my Laravel 4 projects for a while now but more importantly:
This really makes a lot of sense and while I didn't struggle with L4 configs, I'm sure I could see how they came off as a bit arcane to some. There are a few circumstances where I think people went a bit off the rails with trying to extend or modify the config systems. Those are their messes to own, but at least now it won't result in them mis-blaming the framework.
Okay, so where does that leave us? Well, if you're still wondering how you can merge configs and simplify things, there's really just one tweet left in the discussion that helped me understand:
The simplest summary I can offer based on this is that instead of asking Laravel to load and combine configurations, you are now responsible for providing those arrays yourself. Fear not however, because you can still merge them conveniently using Illuminate\Config\Repository::set. The reader is encouraged to also look at the utility method array_set and Arr::set.
If you're going to be parsing config for your pacakge, I'd suggest familiarizing yourself with ServiceProvider::register and ServiceProvider::boot. It's worth noting that boot is optional and supports method-injection from the framework. This explains why the method isn't present or marked as abstract in the parent.
They're run by the framework in order of register and then boot, so my understanding right now is that you'd do all your config parsing in register and then leverage that configuration in boot or closures.
In terms of simplifying resource and configuration file loading - I think Taylor has something in mind. We simply have to be patient.
Hopefully this post has cleared things up for you. If I've missed anything or still have it wrong, let me know with a comment!
The keen observer might have noticed however that Taylor made a commit on December 4th that "broke" support for packages designed to target Laravel 4.
@Omega_ ehhh maybe… i’ll see how it shakes out… need to finish that up next week
— Taylor Otwell (@taylorotwell) January 1, 2015
Patience is key, so I was happy to continue waiting for the next great thing.
In Laravel 4, packages allow their configurations to be overridden per environment. A package can ship a default configuration which can then be copied into the project as a template to be combined with the original's settings at runtime. Think of it as a really tricked out array_merge_recursive.
By removing ServiceProvider::package, what's being conveyed is that the automatic cascade behaviour is now deprecated.
This might seem distressing, but don't worry. Any behaviours you've created that might have depended on arrays are still good. It's just more deliberate - and luckily enough, a great discussion sparked up on twitter yesterday which helped clear a way forward.
(I said it before, but I'll say it again, I really appreciate Taylor's patience as I muddled through understanding the changes.)
@Omega_ there is no publishing… Just override it in your app using config set… Just set it :)
— Taylor Otwell (@taylorotwell) January 4, 2015
I was a bit confused at this point, because I was expecting there to be some kind of magical mechanism between the package and the project still. Not realizing of course that rather than expecting the two to couple, you simply leverage your project's ServiceProvider to do any customizations. I had been creating project-level ServiceProviders in my Laravel 4 projects for a while now but more importantly:
@Omega_ L4 config was wayyyyy overcomplicated and slow
— Taylor Otwell (@taylorotwell) January 4, 2015
This really makes a lot of sense and while I didn't struggle with L4 configs, I'm sure I could see how they came off as a bit arcane to some. There are a few circumstances where I think people went a bit off the rails with trying to extend or modify the config systems. Those are their messes to own, but at least now it won't result in them mis-blaming the framework.
Okay, so where does that leave us? Well, if you're still wondering how you can merge configs and simplify things, there's really just one tweet left in the discussion that helped me understand:
@Omega_ in package: Config::set… in project… Config::set...
— Taylor Otwell (@taylorotwell) January 4, 2015
The simplest summary I can offer based on this is that instead of asking Laravel to load and combine configurations, you are now responsible for providing those arrays yourself. Fear not however, because you can still merge them conveniently using Illuminate\Config\Repository::set. The reader is encouraged to also look at the utility method array_set and Arr::set.
If you're going to be parsing config for your pacakge, I'd suggest familiarizing yourself with ServiceProvider::register and ServiceProvider::boot. It's worth noting that boot is optional and supports method-injection from the framework. This explains why the method isn't present or marked as abstract in the parent.
They're run by the framework in order of register and then boot, so my understanding right now is that you'd do all your config parsing in register and then leverage that configuration in boot or closures.
Thoughts
I'd encourage you not to see these changes as the loss of a feature, but an attempt to help you simplify your projects. There's been a great push to apply 12factor practices in Laravel 5 - namely configuration and parameterizing your applications. When you decide to dive into using Docker and other hosting setups, you'll be glad these sorts of things happened!In terms of simplifying resource and configuration file loading - I think Taylor has something in mind. We simply have to be patient.
Hopefully this post has cleared things up for you. If I've missed anything or still have it wrong, let me know with a comment!
Comments
Post a Comment