I'm setting up a factory to load my classes and their methods for use across my application. Probably continuing on with the strategy method. I feel as though I'm missing a step with my factory, as it throws an exception to finding the class through class_exists(), meanwhile it loads correctly if I omit the check. So, the question is - how do I load my custom factory and classes in laravel appropriately?
Interestingly enough, it doesn't throw an error inside my tests right now. Only inside my application itself.
I'm newish to the implementation process so I suspect I'm not loading my custom classes appropriately or have a namespacing problem, or maybe should be loading this up in a service provider? Ok that's two questions really. Here's the code:
Factory
namespace app\Support\Humans;
class PeopleFactory
{
public static function build($person)
{
//adding this made it work for my test
$person = __NAMESPACE__ . "\\" . $person;
if (class_exists($person)) {
return new $person();
} else {
//person doesn't exist
throw new Exception;
}
}
}
Person Type
namespace app\Support\Humans;
class PersonType extends BasePerson
{
public function joinHumanity()
{
//etc
}
}
As I'm suspecting a namespacing issue, a segment of my composer.json pointing to the directory of my custom classes.
"autoload": {
"classmap": [
"app/Humans/"
],
"psr-4": {
"Humanity\\": "app/"
}
},
Implementation both in testing or elsewhere use app\Support\Humans\PeopleFactory; ....
$new_human = PeopleFactory::build($this->user->personType);
$new_human->joinHumanity();
Aucun commentaire:
Enregistrer un commentaire