Skip to content

Commit dd0bcfe

Browse files
This will fulfill the release
1 parent 08501b4 commit dd0bcfe

File tree

10 files changed

+154
-117
lines changed

10 files changed

+154
-117
lines changed

helpers.php

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/Commands/ClassGenerators/MakeEnumClass.php renamed to src/Commands/ClassGenerators/MakeEnum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Bensondevs\LaravelBoilerPlate\Services\Utility\ClassGeneratorService;
66
use Illuminate\Console\Command;
77

8-
class MakeEnumClass extends Command
8+
class MakeEnum extends Command
99
{
1010
/**
1111
* The name and signature of the console command.
@@ -19,7 +19,7 @@ class MakeEnumClass extends Command
1919
*
2020
* @var string
2121
*/
22-
protected $description = 'Create enum class';
22+
protected $description = 'Create an enum class';
2323

2424
/**
2525
* Create a new command instance.

src/Enum/Enum.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace Bensondevs\LaravelBoilerPlate\Enum;
4+
5+
use ReflectionClass;
6+
use ReflectionException;
7+
8+
class Enum
9+
{
10+
/**
11+
* Get array that contains list of constants
12+
*
13+
* @return array
14+
*/
15+
public static function getConstants(): array
16+
{
17+
$reflectionClass = new ReflectionClass(static::class);
18+
return $reflectionClass->getConstants();
19+
}
20+
21+
/**
22+
* Get value of enum
23+
*
24+
* @param string $value
25+
* @return string
26+
* @throws ReflectionException
27+
*/
28+
public static function getValue(string $value): string
29+
{
30+
$constants = static::collectAsArray();
31+
return $constants[$value];
32+
}
33+
34+
/**
35+
* Get description of enum value
36+
*
37+
* @param int|string $value
38+
* @return string
39+
* @throws ReflectionException
40+
*/
41+
public static function getDescription(int|string $value): string
42+
{
43+
$constants = static::getConstants();
44+
45+
return array_search($value, $constants);
46+
}
47+
48+
/**
49+
* Collect all enum values as array
50+
*
51+
* @param bool $withDescription
52+
* @return array<int, int>|array<int, string>
53+
* @throws ReflectionException
54+
*/
55+
public static function collectAsArray(bool $withDescription = false): array
56+
{
57+
$refClass = new ReflectionClass(static::class);
58+
$enums = $refClass->getConstants();
59+
60+
if ($withDescription === true) {
61+
$tempEnums = [];
62+
foreach ($enums as $enum) {
63+
$tempEnums[$enum] = self::getDescription($enum);
64+
}
65+
66+
$enums = $tempEnums;
67+
}
68+
69+
return $enums;
70+
}
71+
72+
/**
73+
* Get values of the enum
74+
*
75+
* @return array
76+
* @throws ReflectionException
77+
*/
78+
public static function collectValues(): array
79+
{
80+
return array_values(self::getConstants());
81+
}
82+
83+
/**
84+
* Get keys of the enum
85+
*
86+
* @return array
87+
* @throws ReflectionException
88+
*/
89+
public static function collectKeys(): array
90+
{
91+
return array_keys(self::getConstants());
92+
}
93+
94+
/**
95+
* Get values from the enum without excepted items in parameter.
96+
*
97+
* @param array|int|string $excludedValues
98+
* @return array
99+
* @throws ReflectionException
100+
*/
101+
public static function collectValuesWithout(array|int|string $excludedValues): array
102+
{
103+
$excludedValues = is_array($excludedValues) ?
104+
$excludedValues :
105+
[$excludedValues];
106+
107+
$values = self::collectValues();
108+
return array_filter($values, function ($value) use ($excludedValues) {
109+
return !in_array($value, $excludedValues);
110+
});
111+
}
112+
113+
/**
114+
* Check whether a subscription status existed.
115+
*
116+
* @param int|string $value
117+
* @return bool
118+
* @throws ReflectionException
119+
*/
120+
public static function hasValue(int|string $value): bool
121+
{
122+
$values = self::collectValues();
123+
124+
return in_array($value, $values);
125+
}
126+
}

src/Helpers/FileHelpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ function stub_path(string $stubName): string
350350
*/
351351
function package_stub_path(string $stubName): string
352352
{
353-
return base_path(concat_paths([
354-
'../Templates/ClassGenerator',
353+
return (concat_paths([
354+
__DIR__ . '/../Templates/ClassGenerator',
355355
$stubName,
356356
]));
357357
}

src/LaravelBoilerPlateServiceProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public function boot(): void
2424
*/
2525
private function registerHelpers(): void
2626
{
27-
foreach (scandir('Helpers') as $helperFile) {
27+
foreach (scandir(__DIR__ . '/Helpers') as $helperFile) {
2828
if (in_array($helperFile, ['.', '..'])) {
2929
continue;
3030
}
3131

32-
require '../Helpers/'. $helperFile;
32+
require __DIR__ . '/Helpers/' . $helperFile;
3333
}
3434
}
3535

@@ -42,11 +42,11 @@ private function registerConsoleCommands(): void
4242
{
4343
if ($this->app->runningInConsole()) {
4444
$this->commands([
45-
\Bensondevs\LaravelBoilerPlate\Commands\ClassGenerators\MakeEnumClass::class,
45+
\Bensondevs\LaravelBoilerPlate\Commands\ClassGenerators\MakeEnum::class,
4646
\Bensondevs\LaravelBoilerPlate\Commands\ClassGenerators\MakeHelper::class,
4747
\Bensondevs\LaravelBoilerPlate\Commands\ClassGenerators\MakeIntegrationTest::class,
48-
\Bensondevs\LaravelBoilerPlate\Commands\ClassGenerators\MakeRepositoryClass::class,
49-
\Bensondevs\LaravelBoilerPlate\Commands\ClassGenerators\MakeServiceClass::class,
48+
\Bensondevs\LaravelBoilerPlate\Commands\ClassGenerators\MakeRepository::class,
49+
\Bensondevs\LaravelBoilerPlate\Commands\ClassGenerators\MakeService::class,
5050
]);
5151
}
5252
}

src/Repositories/BaseRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace RafCom\Repositories;
3+
namespace Bensondevs\LaravelBoilerPlate\Repositories;
44

55
use Illuminate\Database\Eloquent\{Builder, Collection, Model};
66
use Illuminate\Pagination\LengthAwarePaginator;

src/Services/Utility/ClassGeneratorService.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,18 @@ private function prepareVariables(): void
7171
'scope' => app_path('Scopes'),
7272
'enum' => app_path('Enums'),
7373
'integration_test' => test_path('Integration'),
74+
'helper' => app_path('Helpers'),
7475
];
7576

7677
$this->stubPaths = [
77-
'service' => stub_path('Service.stub'),
78-
'repository' => stub_path('Repository.stub'),
79-
'contract' => stub_path('Contract.stub'),
80-
'trait' => stub_path('Trait.stub'),
81-
'scope' => stub_path('Scope.stub'),
82-
'enum' => stub_path('Enum.stub'),
83-
'integration_test' => stub_path('IntegrationTest.stub'),
78+
'service' => package_stub_path('Service.stub'),
79+
'repository' => package_stub_path('Repository.stub'),
80+
'contract' => package_stub_path('Contract.stub'),
81+
'trait' => package_stub_path('Trait.stub'),
82+
'scope' => package_stub_path('Scope.stub'),
83+
'enum' => package_stub_path('Enum.stub'),
84+
'integration_test' => package_stub_path('IntegrationTest.stub'),
85+
'helper' => package_stub_path('Helper.stub'),
8486
];
8587

8688
$this->stubCompacts = [
@@ -155,7 +157,12 @@ public function getDirectory(): string
155157
{
156158
$type = $this->getType();
157159

158-
return $this->basePaths[$type];
160+
$instancePath = $this->basePaths[$type];
161+
if (!file_exists($instancePath)) {
162+
mkdir($instancePath);
163+
}
164+
165+
return $instancePath;
159166
}
160167

161168
/**

src/Stubs/Enum.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Enums{{extendedPath}};
44

5-
use App\Enums\Enum;
5+
use Bensondevs\LaravelBoilerPlate\Enums\Enum;
66

77
class {{enumName}} extends Enum
88
{

src/Stubs/Repository.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Repositories{{extendedPath}};
44

5-
use App\Repositories\BaseRepository;
5+
use Bensondevs\LaravelBoilerPlate\Repositories\BaseRepository;
66

77
class {{repositoryName}} extends BaseRepository
88
{

src/Templates/ClassGenerator/Repository.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Repositories{{extendedPath}};
44

5-
use RafCom\Repositories\BaseRepository;
5+
use Bensondevs\LaravelBoilerPlate\Repositories\BaseRepository;
66

77
class {{repositoryName}} extends BaseRepository
88
{

0 commit comments

Comments
 (0)