-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from jerguslejko/classifiers
Introduce Classifiers
- Loading branch information
Showing
25 changed files
with
493 additions
and
453 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class Classifier | ||
{ | ||
const CLASSIFIERS = [ | ||
ControllerClassifier::class, | ||
ModelClassifier::class, | ||
CommandClassifier::class, | ||
RuleClassifier::class, | ||
PolicyClassifier::class, | ||
MiddlewareClassifier::class, | ||
EventClassifier::class, | ||
MailClassifier::class, | ||
NotificationClassifier::class, | ||
JobClassifier::class, | ||
MigrationClassifier::class, | ||
RequestClassifier::class, | ||
ResourceClassifier::class, | ||
SeederClassifier::class, | ||
ServiceProviderClassifier::class, | ||
]; | ||
|
||
public function classify(ReflectionClass $class) | ||
{ | ||
foreach (self::CLASSIFIERS as $classifier) { | ||
$c = new $classifier; | ||
|
||
if ($c->satisfies($class)) { | ||
return $c->getName(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class CommandClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Commands'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
return $class->isSubclassOf(\Illuminate\Console\Command::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class ControllerClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Controllers'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
return $class->isSubclassOf(\Illuminate\Routing\Controller::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class EventClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Events'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
foreach ($class->getTraits() as $trait) { | ||
if ($trait->name == \Illuminate\Foundation\Events\Dispatchable::class) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class JobClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Jobs'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
foreach ($class->getTraits() as $trait) { | ||
if ($trait->name == \Illuminate\Foundation\Bus\Dispatchable::class) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class MailClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Mails'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
return $class->isSubclassOf(\Illuminate\Mail\Mailable::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
use Illuminate\Contracts\Auth\Access\Gate; | ||
|
||
class MiddlewareClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Middlewares'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
$kernel = resolve(\Illuminate\Contracts\Http\Kernel::class); | ||
|
||
if ($kernel->hasMiddleware($class->getName())) { | ||
return true; | ||
} | ||
|
||
$router = resolve('router'); | ||
$middlewares = collect($router->getMiddleware())->flatten(); | ||
$groupMiddlewares = collect($router->getMiddlewareGroups())->flatten(); | ||
$mergedMiddlewares = $middlewares->merge($groupMiddlewares); | ||
|
||
return $mergedMiddlewares->contains($class->getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class MigrationClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Migrations'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
return $class->isSubclassOf(\Illuminate\Database\Migrations\Migration::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class ModelClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Models'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
return $class->isSubclassOf(\Illuminate\Database\Eloquent\Model::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class NotificationClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Notifications'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
return $class->isSubclassOf(\Illuminate\Notifications\Notification::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
use Illuminate\Contracts\Auth\Access\Gate; | ||
|
||
class PolicyClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Policies'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
return in_array( | ||
$class->getName(), resolve(Gate::class)->policies() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class RequestClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Requests'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
return $class->isSubclassOf(\Illuminate\Foundation\Http\FormRequest::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class ResourceClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Resources'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
return $class->isSubclassOf(\Illuminate\Http\Resources\Json\Resource::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class RuleClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Rules'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
return $class->implementsInterface(\Illuminate\Contracts\Validation\Rule::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class SeederClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Seeders'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
return $class->isSubclassOf(\Illuminate\Database\Seeder::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Wnx\LaravelStats\Classifiers; | ||
|
||
use Wnx\LaravelStats\ReflectionClass; | ||
|
||
class ServiceProviderClassifier extends Classifier | ||
{ | ||
public function getName() | ||
{ | ||
return 'Service Providers'; | ||
} | ||
|
||
public function satisfies(ReflectionClass $class) | ||
{ | ||
return $class->isSubclassOf(\Illuminate\Support\ServiceProvider::class); | ||
} | ||
} |
Oops, something went wrong.