[5.5] Let Carbon be Macroable.#19771
[5.5] Let Carbon be Macroable.#19771taylorotwell merged 3 commits intolaravel:masterfrom lucasmichot:feature/master/macroable-carbon
Conversation
|
would it be first better to try and push this upstream to the main repo? |
|
Already tried briannesbitt/Carbon#608 @browner12 Having macros on Laravel side allows to comply the way we use them. People are quite used to it with many objects already Macroable |
|
my only concern is this becomes something of a maintenance nightmare. do we start subclassing all other packages we bring in? not saying this doesn't provide a useful feature, just worried a little about the logistics. |
|
I see your concern, but you only use In the case of The other possibility is to create a service provider with a config item pointing to an extended Carbon class, taking |
|
I don't feel like this is a maintenance nightmare tho, as we do exactly the same with |
|
I'm curious what macros you are adding to Carbon? 😄 |
|
@taylorotwell My main use case is to easily localize Eloquent models in the user's timezone without all the extra setup of a trait or mutator in every Eloquent class for each date I want to manipulate. (Or a base class + Carbon subclass). Alternatively, I've added a function around every output before: I suppose I could have made it more readable with this: But still pretty gross compare to chaining. It would also be really nice to alias a couple of date formats. Especially ones that gets rid of the current year, but shows past or future ones: Another use case I have is to add some helper methods for Carbon to help me output a date range string when two dates are given. (For events.) I've also built a system where we generate PDF brochures for a client with a Vue/Laravel based editor and schedule them to send on the next closest work hour. I made a class that gives me the Carbon instance I want so this is a weaker use-case: I think that is most of my use cases so far for projects I've worked on recently. |
|
in regards to the local timezone use case, I've done the same thing where I have a global helper that all of my dates get wrapped in. |
|
I mostly use macros in Carbon for handling milliseconds, specific time-formats and handling DST differently - and all other "normal" Carbon functions of course |
|
I'm a big fan of this. Also would be interested in your macros @lucasmichot |
- Feeback is welcome -
When using
\Carbon\Carbona lot, there is a lack of possibility to create our own macros.The only way to achieve this was to use two different classes:
This PR allows anyone to add macros to Carbon by using
\Carbon\Support\Carbon.See tests in https://github.com/laravel/framework/pull/19771/files#diff-cdb727ac9aaec34a81ce8fb0479f7f6f
Thus
nesbot/carbonis now only required byilluminate/support