Skip to content

Commit

Permalink
Replace class_uses_recursive helper with own service method
Browse files Browse the repository at this point in the history
  • Loading branch information
romanzipp committed Mar 27, 2020
1 parent 0edcfbc commit bce2d38
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
70 changes: 70 additions & 0 deletions src/Services/ClassUses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* Used from https://github.com/laravel/helpers
*
* The MIT License (MIT)
*
* Copyright (c) Taylor Otwell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace romanzipp\QueueMonitor\Services;

class ClassUses
{
/**
* Returns all traits used by a class, its parent classes and trait of their traits.
*
* @param object|string $class
* @return array
*/
public static function classUsesRecursive($class)
{
if (is_object($class)) {
$class = get_class($class);
}

$results = [];

foreach (array_reverse(class_parents($class)) + [$class => $class] as $class) {
$results += self::traitUsesRecursive($class);
}

return array_unique($results);
}

/**
* Returns all traits used by a trait and its traits.
*
* @param string $trait
* @return array
*/
public static function traitUsesRecursive($trait)
{
$traits = class_uses($trait);

foreach ($traits as $trait) {
$traits += self::traitUsesRecursive($trait);
}

return $traits;
}
}
2 changes: 1 addition & 1 deletion src/Services/QueueMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected static function jobFinished(JobContract $job, bool $failed = false, $e
*/
public static function shouldBeMonitored(JobContract $job): bool
{
return array_key_exists(IsMonitored::class, class_uses_recursive(
return array_key_exists(IsMonitored::class, ClassUses::classUsesRecursive(
$job->resolveName()
));
}
Expand Down

0 comments on commit bce2d38

Please sign in to comment.