Skip to content

Commit 4d6fdd7

Browse files
committed
Initial commit
1 parent 003cade commit 4d6fdd7

File tree

14 files changed

+293
-2
lines changed

14 files changed

+293
-2
lines changed

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/tests export-ignore
2+
/phpunit.xml export-ignore
3+
.gitattributes export-ignore
4+
.gitignore export-ignore

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.lock
3+
.idea
4+

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
# laravel-essentials
2-
Tiny extensions and helpers for Laravel projects
1+
# Laravel Essentials
2+
3+
Growing library for simple macros, facades and other helpers in the Laravel cosmos .
4+
5+
## Installation
6+
7+
### Install package
8+
9+
```shell
10+
composer require codebarista/laravel-essentials
11+
```
12+
13+
## License
14+
15+
The MIT License (MIT). Please see [License File](LICENSE) for more information.

bootstrap/helpers.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
if (! function_exists('encode_json')) {
4+
function encode_json(array $value): bool|string
5+
{
6+
return \Illuminate\Support\Arr::toJson($value);
7+
}
8+
}
9+
10+
if (! function_exists('decode_json')) {
11+
function decode_json(string $value): array
12+
{
13+
return \Illuminate\Support\Str::toJson($value);
14+
}
15+
}

composer.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"name": "codebarista/laravel-essentials",
3+
"description": "Tiny extensions and helpers for Laravel projects.",
4+
"type": "library",
5+
"keywords": [
6+
"laravel",
7+
"extensions",
8+
"helpers"
9+
],
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Ralf Langebrake",
14+
"email": "ralf@langebrake.com"
15+
}
16+
],
17+
"require": {
18+
"php": "^8.3",
19+
"laravel/framework": "^10.0",
20+
"spatie/laravel-health": "^1.20"
21+
},
22+
"require-dev": {
23+
"orchestra/testbench": "^v8.21",
24+
"pestphp/pest-plugin-laravel": "^2.0"
25+
},
26+
"suggest": {
27+
"barryvdh/laravel-ide-helper": "Enables your IDE to provide accurate autocompletion"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"Codebarista\\LaravelEssentials\\": "src/"
32+
},
33+
"files": [
34+
"bootstrap/helpers.php"
35+
]
36+
},
37+
"autoload-dev": {
38+
"psr-4": {
39+
"Codebarista\\LaravelEssentials\\Tests\\": "tests/"
40+
}
41+
},
42+
"extra": {
43+
"laravel": {
44+
"providers": [
45+
"Codebarista\\LaravelEssentials\\EssentialsServiceProvider"
46+
]
47+
}
48+
},
49+
"config": {
50+
"optimize-autoloader": true,
51+
"preferred-install": "dist",
52+
"sort-packages": true,
53+
"allow-plugins": {
54+
"pestphp/pest-plugin": true
55+
}
56+
},
57+
"minimum-stability": "dev",
58+
"prefer-stable": true
59+
}

phpunit.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
stopOnFailure="true"
6+
cacheResult="false"
7+
colors="true"
8+
>
9+
<testsuites>
10+
<testsuite name="Laravel Essentials Test Suite">
11+
<directory suffix="Test.php">./tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
<source>
15+
<include>
16+
<directory suffix=".php">./src</directory>
17+
</include>
18+
</source>
19+
</phpunit>

src/Checks/OpcodeCacheCheck.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Codebarista\LaravelEssentials\Checks;
4+
5+
use Spatie\Health\Checks\Check;
6+
use Spatie\Health\Checks\Result;
7+
8+
class OpcodeCacheCheck extends Check
9+
{
10+
public function run(): Result
11+
{
12+
if ($status = opcache_get_status(false)) {
13+
if ((bool) $status['opcache_enabled'] === true) {
14+
return Result::make()->ok();
15+
}
16+
17+
return Result::make()->warning('Disabled');
18+
}
19+
20+
return Result::make()->failed('Absent');
21+
}
22+
}

src/EssentialsServiceProvider.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Codebarista\LaravelEssentials;
4+
5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\ServiceProvider;
7+
use Illuminate\Support\Str;
8+
9+
class EssentialsServiceProvider extends ServiceProvider
10+
{
11+
public function register(): void
12+
{
13+
$this->registerStringMacros();
14+
$this->registerArrayMacros();
15+
}
16+
17+
public function boot(): void
18+
{
19+
}
20+
21+
private function registerStringMacros(): void
22+
{
23+
$macros = [
24+
'toJson' => \Codebarista\LaravelEssentials\Macros\String\Json::class,
25+
];
26+
27+
foreach ($macros as $name => $class) {
28+
if (! Str::hasMacro($name)) {
29+
Str::macro($name, app($class));
30+
}
31+
}
32+
}
33+
34+
private function registerArrayMacros(): void
35+
{
36+
$macros = [
37+
'toJson' => \Codebarista\LaravelEssentials\Macros\Array\Json::class,
38+
];
39+
40+
foreach ($macros as $name => $class) {
41+
if (! Arr::hasMacro($name)) {
42+
Arr::macro($name, app($class));
43+
}
44+
}
45+
}
46+
}

src/Facades/DB.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Codebarista\LaravelEssentials\Facades;
4+
5+
use Illuminate\Support\Collection;
6+
7+
class DB extends \Illuminate\Support\Facades\DB
8+
{
9+
public static function collect(string $query, array $bindings): Collection
10+
{
11+
return collect(self::select($query, $bindings));
12+
}
13+
}

src/Macros/Array/Json.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Codebarista\LaravelEssentials\Macros\Array;
4+
5+
class Json
6+
{
7+
public function __invoke(array $value, bool $pretty = false): bool|string
8+
{
9+
$flags = JSON_NUMERIC_CHECK
10+
| JSON_THROW_ON_ERROR
11+
| JSON_UNESCAPED_SLASHES;
12+
13+
if ($pretty === true) {
14+
$flags |= JSON_PRETTY_PRINT;
15+
}
16+
17+
return json_encode($value, $flags);
18+
}
19+
}

src/Macros/String/Json.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Codebarista\LaravelEssentials\Macros\String;
4+
5+
class Json
6+
{
7+
public function __invoke(
8+
string $value,
9+
bool $associative = true,
10+
int $depth = 512,
11+
int $flags = JSON_THROW_ON_ERROR
12+
): array|object {
13+
return json_decode($value, $associative, $depth, $flags);
14+
}
15+
}

tests/Feature/HelpersTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
it('decodes json string to array', function () {
4+
$value = <<<'JSON'
5+
{
6+
"greeting": "Gude Moin"
7+
}
8+
JSON;
9+
10+
expect(decode_json($value))
11+
->toBeArray()
12+
->toHaveKey('greeting')
13+
->toContain('Gude Moin');
14+
15+
})->covers('decode_json');
16+
17+
it('encodes array to json string', function () {
18+
$value = [
19+
'greeting' => 'Gude Moin',
20+
];
21+
22+
expect(encode_json($value))
23+
->toBeString()
24+
->toContain('greeting');
25+
26+
})->covers('encode_json');

tests/PackageTestCase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codebarista\LaravelEssentials\Tests;
6+
7+
use Codebarista\LaravelEssentials\EssentialsServiceProvider;
8+
use Orchestra\Testbench\TestCase;
9+
10+
abstract class PackageTestCase extends TestCase
11+
{
12+
protected function getPackageProviders($app): array
13+
{
14+
return [
15+
EssentialsServiceProvider::class,
16+
];
17+
}
18+
}

tests/Pest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Codebarista\LaravelEssentials\Tests\PackageTestCase;
6+
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Test Case
10+
|--------------------------------------------------------------------------
11+
|
12+
| The closure you provide to your test functions is always bound to a specific PHPUnit test
13+
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
14+
| need to change it using the "uses()" function to bind a different classes or traits.
15+
|
16+
*/
17+
18+
uses(PackageTestCase::class)->in(__DIR__);

0 commit comments

Comments
 (0)