laravel-beyond icon indicating copy to clipboard operation
laravel-beyond copied to clipboard

add trait command

Open dimzeta opened this issue 1 year ago • 6 comments

As https://github.com/regnerisch/laravel-beyond/issues/54 requested, I have created beyond:make:trait command.

The trait is created in Domain\XXX\Traits namespace by default. You can add --support flag to create the trait in Support\Packages\Laravel\Traits namespace.

dimzeta avatar Sep 27 '22 09:09 dimzeta

Thank you for your PR. I made a few comments. I'll provide changes if wanted in the evening.

alexanderkroneis avatar Sep 27 '22 10:09 alexanderkroneis

I would expect traits to live only inside the Support namespace, as you can use them everywhere. See example below:

trait Debug
{
    protected function log($exception)
    {
        // do something special for debug logging
    }
}

regnerisch avatar Sep 27 '22 11:09 regnerisch

Thank you for your comments, I have made the changes, hope everything is good!

dimzeta avatar Sep 27 '22 12:09 dimzeta

You are close :)

I also want to say thank you, for your contribution! It's nice to see people helping us with this little project.

regnerisch avatar Sep 27 '22 14:09 regnerisch

This project is gold to me, so It's an honor to contribute on it!

If I understand correctly, it seems you don't want Traits in the Domain namespace, and only create them in Support.

In my case, I need to create a trait in the Domain, like this:

<?php

namespace Domain\Authentication\Traits;

trait HasActivationCode
{
    // ...
}

And use it in my User model:

<?php

namespace Domain\Users\Models;

use Domain\Authentication\Traits\HasActivationCode;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasActivationCode;

    // ...
}

I am not very experimented in DDD, so maybe this is not the proper way to do that 🤷

dimzeta avatar Sep 27 '22 17:09 dimzeta

Yes, I only want them to be created inside the Support namespace.

Traits are properties and methods you can reuse in every class. It is quite unlikely you will create a trait which is reused in only one specific domain. So I would expect traits to be created inside the Support namespace to not violate separation by accident (e.g. using a trait of another domain)

We have two options here: Forcing creation inside one specific namespace, or remove the command to not force creation in one namespace. I prefer the former. You are always free to move your trait elsewhere if it fits better.

regnerisch avatar Sep 28 '22 20:09 regnerisch