-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0ab5c95
Showing
7 changed files
with
484 additions
and
0 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,7 @@ | ||
/vendor | ||
build | ||
composer.phar | ||
composer.lock | ||
.DS_Store | ||
.phpunit.result.cache | ||
.vscode |
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,5 @@ | ||
# CHANGELOG | ||
|
||
## [1.0.0] | ||
|
||
- Package created from sirmathays/convenient-laravel-helpers |
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,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Matti Suoraniemi | ||
|
||
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. |
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,84 @@ | ||
<p> | ||
<img src="https://matti.suoraniemi.com/storage/lyhty-support.png" width="400"> | ||
</p> | ||
|
||
<p> | ||
<a href="https://packagist.org/packages/lyhty/support"> | ||
<img src="https://img.shields.io/packagist/dt/lyhty/support" alt="Total Downloads"> | ||
</a> | ||
<a href="https://packagist.org/packages/lyhty/support"> | ||
<img src="https://img.shields.io/packagist/v/lyhty/support" alt="Latest Stable Version"> | ||
</a> | ||
<a href="https://packagist.org/packages/lyhty/support"> | ||
<img src="https://img.shields.io/packagist/l/lyhty/support" alt="License"> | ||
</a> | ||
</p> | ||
|
||
This package provides some additional, convenient helpers for you to use with your Laravel project. | ||
|
||
## Installation | ||
|
||
Install the package with Composer: | ||
|
||
composer require lyhty/support | ||
|
||
## Features | ||
|
||
### Helpers | ||
|
||
- `class_uses_trait($class, $trait, bool $recursive = true): bool` | ||
- Return boolean value whether the given class uses given trait. | ||
- `array_depth(array $array)` | ||
- Return integer describing the max depth of the given array. | ||
- `class_implements_interface($class, $interface): bool` | ||
- Return boolean value whether the given class implements given interface. | ||
- `class_extends($class, $parent): bool` | ||
- Return boolean value whether the given class extends given parent class. | ||
- `set_type($value, $type)` | ||
- Alias for 'settype' which allows non-variables as arguments. | ||
- `trim_spaces(string $string): string` | ||
- Trim spaces from string. | ||
- `not_null($var): bool` | ||
- !is_null | ||
- `get_bool($value): bool` | ||
- Get boolean value from given value. Accepts string true/false. | ||
- `class_namespace(string $className): string` | ||
- Get namespace of given class. | ||
- `___(array $keys, array $replace = [], array $numbers = [], string $locale = null, string $glue = ' '): string` | ||
- Translate given messages and glue them together. | ||
|
||
### Discovery class | ||
|
||
This is pretty much copied from `Illuminate\Foundation\Events\DiscoverEvents` from, just made more | ||
generic. | ||
|
||
**Examples** | ||
|
||
```php | ||
use Lyhty\Support\Discovery; | ||
|
||
$all = Discovery::within('app\Models')->toArray(); | ||
// ["App\Models\User", "App\Models\BlogPost", "App\Models\Concerns\Taggable", "App\Models\Contracts\BlogWriter"] | ||
|
||
$classes = Discovery::classesWithin('app\Models')->toArray(); | ||
// ["App\Models\User", "App\Models\BlogPost"] | ||
|
||
$traits = Discovery::traitsWithin('app\Models')->toArray(); | ||
// ["App\Models\Concerns\Taggable"] | ||
|
||
$interfaces = Discovery::interfacesWithin('app\Models')->toArray(); | ||
// ["App\Models\Contracts\BlogWriter"] | ||
|
||
$usingClasses = Discovery::usesWithin('app\Models', 'App\Models\Concerns\Taggable')->toArray(); | ||
// ["App\Models\BlogPost"] | ||
|
||
$implementingClasses = Discovery::implementsWithin('app\Models', 'App\Models\Contracts\BlogWriter')->toArray(); | ||
// ["App\Models\User"] | ||
|
||
$extendingClasses = Discovery::extendsWithin('app\Models', 'Illuminate\Database\Eloquent\Model')->toArray(); | ||
// ["App\Models\User", "App\Models\BlogPost"] | ||
``` | ||
|
||
## License | ||
|
||
Convenient Laravel Helpers is open-sourced software licensed under the [MIT license](LICENSE.md). |
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,39 @@ | ||
{ | ||
"name": "lyhty/support", | ||
"description": "Adds some very convenient helpers to your Laravel project", | ||
"keywords": [ | ||
"laravel", | ||
"support", | ||
"helpers" | ||
], | ||
"homepage": "https://matti.suoraniemi.com/creations/lyhty/support", | ||
"require": { | ||
"php": ">=7.1.3", | ||
"laravel/framework": ">=5.6" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~7.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Lyhty\\Support\\": "src" | ||
}, | ||
"files": [ | ||
"src/helpers.php" | ||
] | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Tests\\": "tests/" | ||
} | ||
}, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Matti Suoraniemi", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"prefer-stable": true | ||
} |
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,170 @@ | ||
<?php | ||
|
||
namespace Lyhty\Support; | ||
|
||
use Closure; | ||
use Illuminate\Support\Str; | ||
use ReflectionClass; | ||
use ReflectionException; | ||
use SplFileInfo; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
class Discovery | ||
{ | ||
/** | ||
* Get all of the instantiable classes by searching the given directory. | ||
* | ||
* @param string $path | ||
* @param string|null $basePath | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
public static function within( | ||
string $path, | ||
string $basePath = null, | ||
?Closure $filter = null | ||
) { | ||
$basePath = $basePath ?: base_path(); | ||
|
||
$files = (new Finder)->files()->in( | ||
Str::finish($basePath, '/') . ltrim($path, DIRECTORY_SEPARATOR) | ||
); | ||
|
||
$classes = collect(); | ||
|
||
foreach ($files as $file) { | ||
try { | ||
$class = new ReflectionClass(static::classFromFile($file, $basePath)); | ||
} catch (ReflectionException $e) { | ||
continue; | ||
} | ||
|
||
if ($filter && !$filter($class)) { | ||
continue; | ||
} | ||
|
||
$classes->push($class->getName()); | ||
} | ||
|
||
return $classes; | ||
} | ||
|
||
/** | ||
* Get all of the abstract classes by searching the given directory. | ||
* | ||
* @param string $path | ||
* @param string|null $basePath | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
public static function abstractsWithin(string $path, string $basePath = null) | ||
{ | ||
return static::within($path, $basePath, function (ReflectionClass $class) { | ||
return $class->isAbstract() && !$class->isInterface() && !$class->isTrait(); | ||
}); | ||
} | ||
|
||
/** | ||
* Get all of the classes by searching the given directory. | ||
* | ||
* @param string $path | ||
* @param string|null $basePath | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
public static function classesWithin(string $path, string $basePath = null) | ||
{ | ||
return static::within($path, $basePath, function (ReflectionClass $class) { | ||
return $class->isInstantiable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Get all of the interfaces by searching the given directory. | ||
* | ||
* @param string $path | ||
* @param string|null $basePath | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
public static function interfacesWithin(string $path, string $basePath = null) | ||
{ | ||
return static::within($path, $basePath, function (ReflectionClass $class) { | ||
return $class->isInterface(); | ||
}); | ||
} | ||
|
||
/** | ||
* Get all of the traits by searching the given directory. | ||
* | ||
* @param string $path | ||
* @param string|null $basePath | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
public static function traitsWithin(string $path, string $basePath = null) | ||
{ | ||
return static::within($path, $basePath, function (ReflectionClass $class) { | ||
return $class->isTrait(); | ||
}); | ||
} | ||
|
||
/** | ||
* Get all of the classes that use the given trait by searching the given directory. | ||
* | ||
* @param string $path | ||
* @param string $trait | ||
* @param bool $recursive | ||
* @param string|null $basePath | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
public static function usesWithin(string $path, string $trait, bool $recursive = true, string $basePath = null) | ||
{ | ||
return static::within($path, $basePath, function (ReflectionClass $class) use ($trait, $recursive) { | ||
return class_uses_trait($class->getName(), $trait, $recursive); | ||
}); | ||
} | ||
|
||
/** | ||
* Get all of the classes that implement the given interface by searching the given directory. | ||
* | ||
* @param string $path | ||
* @param string $interface | ||
* @param string|null $basePath | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
public static function implementsWithin(string $path, string $interface, string $basePath = null) | ||
{ | ||
return static::within($path, $basePath, function (ReflectionClass $class) use ($interface) { | ||
return class_implements_interface($class->getName(), $interface); | ||
}); | ||
} | ||
|
||
/** | ||
* Get all of the classes that extend the given class by searching the given directory. | ||
* | ||
* @param string $path | ||
* @param string $interface | ||
* @param string|null $basePath | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
public static function extendsWithin(string $path, string $parent, string $basePath = null) | ||
{ | ||
return static::within($path, $basePath, function (ReflectionClass $class) use ($parent) { | ||
return class_extends($class->getName(), $parent); | ||
}); | ||
} | ||
|
||
/** | ||
* Extract the class name from the given file path. | ||
* | ||
* @param \SplFileInfo $file | ||
* @param string $basePath | ||
* @return string | ||
*/ | ||
protected static function classFromFile(SplFileInfo $file, $basePath) | ||
{ | ||
$class = trim(Str::replaceFirst($basePath, '', $file->getRealPath()), DIRECTORY_SEPARATOR); | ||
|
||
return str_replace( | ||
[DIRECTORY_SEPARATOR, ucfirst(basename(app()->path())) . '\\'], | ||
['\\', app()->getNamespace()], | ||
ucfirst(Str::replaceLast('.php', '', $class)) | ||
); | ||
} | ||
} |
Oops, something went wrong.