Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor calling trait methods from repository #386

Merged
merged 3 commits into from
Nov 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 46 additions & 49 deletions src/Repositories/ModuleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ public function getCountByStatusSlug($slug, $scope = [])
return $this->getCountForTrash();
}

foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'getCountByStatusSlug' . class_basename($trait))) {
if (($count = $this->$method($slug)) !== false) {
return $count;
}
foreach ($this->traitsMethods(__FUNCTION__) as $method) {
if (($count = $this->$method($slug)) !== false) {
return $count;
}
}

Expand Down Expand Up @@ -472,10 +470,8 @@ public function prepareFieldsBeforeCreate($fields)
{
$fields = $this->cleanupFields(null, $fields);

foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'prepareFieldsBeforeCreate' . class_basename($trait))) {
$fields = $this->$method($fields);
}
foreach ($this->traitsMethods(__FUNCTION__) as $method) {
$fields = $this->$method($fields);
}

return $fields;
Expand All @@ -490,10 +486,8 @@ public function prepareFieldsBeforeSave($object, $fields)
{
$fields = $this->cleanupFields($object, $fields);

foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'prepareFieldsBeforeSave' . class_basename($trait))) {
$fields = $this->$method($object, $fields);
}
foreach ($this->traitsMethods(__FUNCTION__) as $method) {
$fields = $this->$method($object, $fields);
}

return $fields;
Expand All @@ -506,10 +500,8 @@ public function prepareFieldsBeforeSave($object, $fields)
*/
public function afterUpdateBasic($object, $fields)
{
foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'afterUpdateBasic' . class_basename($trait))) {
$this->$method($object, $fields);
}
foreach ($this->traitsMethods(__FUNCTION__) as $method) {
$this->$method($object, $fields);
}
}

Expand All @@ -520,10 +512,8 @@ public function afterUpdateBasic($object, $fields)
*/
public function beforeSave($object, $fields)
{
foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'beforeSave' . class_basename($trait))) {
$this->$method($object, $fields);
}
foreach ($this->traitsMethods(__FUNCTION__) as $method) {
$this->$method($object, $fields);
}
}

Expand All @@ -534,10 +524,8 @@ public function beforeSave($object, $fields)
*/
public function afterSave($object, $fields)
{
foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'afterSave' . class_basename($trait))) {
$this->$method($object, $fields);
}
foreach ($this->traitsMethods(__FUNCTION__) as $method) {
$this->$method($object, $fields);
}
}

Expand All @@ -547,10 +535,8 @@ public function afterSave($object, $fields)
*/
public function afterDelete($object)
{
foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'afterDelete' . class_basename($trait))) {
$this->$method($object);
}
foreach ($this->traitsMethods(__FUNCTION__) as $method) {
$this->$method($object);
}
}

Expand All @@ -560,10 +546,8 @@ public function afterDelete($object)
*/
public function afterRestore($object)
{
foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'afterRestore' . class_basename($trait))) {
$this->$method($object);
}
foreach ($this->traitsMethods(__FUNCTION__) as $method) {
$this->$method($object);
}
}

Expand All @@ -574,10 +558,8 @@ public function afterRestore($object)
*/
public function hydrate($object, $fields)
{
foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'hydrate' . class_basename($trait))) {
$object = $this->$method($object, $fields);
}
foreach ($this->traitsMethods(__FUNCTION__) as $method) {
$object = $this->$method($object, $fields);
}

return $object;
Expand All @@ -591,10 +573,8 @@ public function getFormFields($object)
{
$fields = $object->attributesToArray();

foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'getFormFields' . class_basename($trait))) {
$fields = $this->$method($object, $fields);
}
foreach ($this->traitsMethods(__FUNCTION__) as $method) {
$fields = $this->$method($object, $fields);
}

return $fields;
Expand All @@ -609,10 +589,8 @@ public function filter($query, array $scopes = [])
{
$likeOperator = $this->getLikeOperator();

foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'filter' . class_basename($trait))) {
$this->$method($query, $scopes);
}
foreach ($this->traitsMethods(__FUNCTION__) as $method) {
$this->$method($query, $scopes);
}

unset($scopes['search']);
Expand Down Expand Up @@ -648,10 +626,8 @@ public function filter($query, array $scopes = [])
*/
public function order($query, array $orders = [])
{
foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'order' . class_basename($trait))) {
$this->$method($query, $orders);
}
foreach ($this->traitsMethods(__FUNCTION__) as $method) {
$this->$method($query, $orders);
}

foreach ($orders as $column => $direction) {
Expand Down Expand Up @@ -803,6 +779,27 @@ protected function getModelRepository($relation, $model = null)
return App::make(Config::get('twill.namespace') . "\\Repositories\\" . ucfirst($model) . "Repository");
}

/**
* @param string|null $method
* @return array
*/
protected function traitsMethods(string $method = null)
{
$method = $method ?? debug_backtrace()[1]['function'];

$traits = array_values(class_uses_recursive(get_called_class()));

$uniqueTraits = array_unique(array_map('class_basename', $traits));

$methods = array_map(function (string $trait) use ($method) {
return $method . $trait;
}, $uniqueTraits);

return array_filter($methods, function (string $method) {
return method_exists(get_called_class(), $method);
});
}

/**
* @return string
*/
Expand Down