Skip to content

Commit 6f4aa57

Browse files
authored
Added Helpers utility class (#39)
1 parent 669ec54 commit 6f4aa57

File tree

7 files changed

+142
-39
lines changed

7 files changed

+142
-39
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- `Fixed` for any bug fixes.
1313
- `Security` in case of vulnerabilities
1414

15+
## [5.3.0]- 2024.10.30
16+
17+
### Added
18+
19+
- Added `Helpers` utility class.
20+
1521
## [5.2.0]- 2024.09.16
1622

1723
### Added

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
"bayfrontmedia/php-http-response": "^2.0",
4242
"bayfrontmedia/php-time-helpers": "^2.0",
4343
"bayfrontmedia/route-it": "^3.1",
44-
"bayfrontmedia/simple-pdo": "^5.0",
44+
"bayfrontmedia/simple-pdo": "^5.3",
4545
"bayfrontmedia/veil": "^2.1",
46-
"filp/whoops": "^2.15",
46+
"filp/whoops": "^2.16",
4747
"symfony/console": "^7.1",
4848
"vlucas/phpdotenv": "^5.6"
4949
}

composer.lock

+35-35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,5 @@ The following services are included with Bones:
105105
### Utilities
106106

107107
- [App](utilities/app.md)
108-
- [Constants](utilities/constants.md)
108+
- [Constants](utilities/constants.md)
109+
- [Helpers](utilities/helpers.md)

docs/utilities/helpers.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Utilities: Helpers
2+
3+
The helpers utility has the namespace `Bayfront\Bones\Application\Utilities\Helpers`
4+
and contains miscellaneous helper functions.
5+
All methods are static.
6+
7+
## Methods
8+
9+
- [classUses](#classuses)
10+
- [traituses](#traituses)
11+
12+
<hr />
13+
14+
### classUses
15+
16+
**Description:**
17+
18+
Recursively return the traits used by the given class and all of its parent classes.
19+
20+
**Parameters:**
21+
22+
- `$class` (object|string)
23+
24+
**Returns:**
25+
26+
- (array)
27+
28+
<hr />
29+
30+
### traitUses
31+
32+
**Description:**
33+
34+
Recursively return all traits used by a trait and its traits.
35+
36+
**Parameters:**
37+
38+
- `$trait` (object|string)
39+
40+
**Returns:**
41+
42+
- (array)

src/Application/Utilities/Helpers.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Bayfront\Bones\Application\Utilities;
4+
5+
class Helpers
6+
{
7+
8+
/**
9+
* Recursively return the traits used by the given class and all of its parent classes.
10+
*
11+
* See: https://laravel.com/docs/11.x/helpers#method-class-uses-recursive
12+
*
13+
* @param object|string $class
14+
* @return array
15+
*/
16+
public static function classUses(object|string $class): array
17+
{
18+
19+
if (is_object($class)) {
20+
$class = get_class($class);
21+
}
22+
23+
$results = [];
24+
25+
foreach (array_reverse(class_parents($class) ?: []) + [$class => $class] as $class) {
26+
$results += self::traitUses($class);
27+
}
28+
29+
return array_unique($results);
30+
31+
}
32+
33+
/**
34+
* Recursively return all traits used by a trait and its traits.
35+
*
36+
* See: https://laravel.com/docs/11.x/helpers#method-trait-uses-recursive
37+
*
38+
* @param object|string $trait
39+
* @return array
40+
*/
41+
public static function traitUses(object|string $trait): array
42+
{
43+
44+
$traits = class_uses($trait) ?: [];
45+
46+
foreach ($traits as $trait) {
47+
$traits += self::traitUses($trait);
48+
}
49+
50+
return $traits;
51+
52+
}
53+
54+
}

src/Bones.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function start(string $interface): void
150150
Constants::define('APP_STORAGE_PATH', Constants::get('APP_BASE_PATH') . '/storage');
151151
Constants::define('BONES_BASE_PATH', rtrim(dirname(__FILE__, 2), '/'));
152152
Constants::define('BONES_RESOURCES_PATH', Constants::get('BONES_BASE_PATH') . '/resources');
153-
Constants::define('BONES_VERSION', '5.2.0');
153+
Constants::define('BONES_VERSION', '5.3.0');
154154
// ------------------------- Load environment variables -------------------------
155155

156156
if (file_exists(App::basePath('/.env'))) {

0 commit comments

Comments
 (0)