Skip to content

Instantly share code, notes, and snippets.

@foremtehan
Created June 27, 2022 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foremtehan/7640672bf3afe813efb725e98b9884d3 to your computer and use it in GitHub Desktop.
Save foremtehan/7640672bf3afe813efb725e98b9884d3 to your computer and use it in GitHub Desktop.
Getting All Laravel Facades Methods
<?php
use ReflectionClass;
use ReflectionMethod;
use ReflectionParameter;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Symfony\Component\Finder\Finder;
use Illuminate\Support\Facades\File;
use Symfony\Component\Finder\SplFileInfo;
config(['app.key' => Str::random(32)]);
$files = Finder::create()->in(__DIR__.'/../../vendor/laravel/framework/src/Illuminate/Support/Facades/')->files();
collect($files)->map(function (SplFileInfo $r) {
return Str::of($r->getRealPath())
->after('src')
->replace(['/', '.php'], ['\\', ''])
->value();
})
->reject(fn($item) => $item == '\Illuminate\Support\Facades\Facade')
->map(function ($namespace) {
$r = new ReflectionClass($namespace);
$root = new ReflectionClass($namespace::getFacadeRoot());
return Str::matchAll('~@see (.*)~', $r->getDocComment())
->push("\\{$root->getName()}")
->unique()
->reject(fn($item) => str_contains($item, 'http'))
->map(function ($item) use ($namespace) {
$r = new ReflectionClass($item);
return collect($r->getMethods(ReflectionMethod::IS_PUBLIC))
->reject(fn(ReflectionMethod $m) => Str::startsWith($m->getName(), '__'))
->map(function (ReflectionMethod $m) use ($namespace, $r) {
$docBlock = $m->getDocComment();
$return = Str::of($docBlock)->match('~@return(.*)~')->trim();
$params = Str::matchAll('~@param(.*)~', $docBlock)->map(fn($param) => Str::of($param)->trim()->replace(' ', ' ')->value())->map(function ($param) use ($m) {
$p = collect($m->getParameters())->first(fn(ReflectionParameter $p) => $p->getName() == Str::match('~\$(.*)~', $param));
if ($p?->isDefaultValueAvailable()) {
$value = $p->getDefaultValue();
$param .= ' = '.match (true) {
is_string($value) && empty($value) => '\'\'',
is_array($value) && empty($value) => '[]',
$value === true => 'true',
$value === false => 'false',
$value === null => 'null',
default => $value
};
}
return $param;
})->implode(', ');
return [
'class' => $r->getName(),
'name' => $m->getName(),
'return' => $return->replace('$this', "\\{$r->getName()}")->value(),
'params' => $params ?: null,
'facade' => $namespace
];
});
});
})
->collapse()
->collapse()
->groupBy('facade')
->each(function (Collection $methods, $facade) {
$docBlocMethods = $methods
->unique('name')
->sortBy('name')
->map(function ($item) {
return str_replace(' ', ' ', "* @method static {$item['return']} {$item['name']}({$item['params']})");
})->implode(PHP_EOL);
File::append('laravel-all-facades-methods.txt', "$facade\n\n$docBlocMethods\n\n");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment