Skip to content

Commit

Permalink
Update pint config (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
rawilk authored Sep 27, 2023
1 parent f1babf5 commit e7247fb
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 121 deletions.
54 changes: 29 additions & 25 deletions .github/workflows/pint.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
name: PHP Linting (Pint)

on:
workflow_dispatch:
push:
branches-ignore:
- 'dependabot/npm_and_yarn/*'
workflow_dispatch:
push:
branches-ignore:
- 'dependabot/npm_and_yarn/*'

permissions:
contents: write

jobs:
phplint:
runs-on: ubuntu-latest
phplint:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2
ref: ${{ github.head_ref }}

- name: Laravel pint
uses: aglipanci/[email protected]
with:
preset: laravel
- name: Laravel pint
uses: aglipanci/[email protected]
with:
preset: laravel

- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch
- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch

- name: Commit Changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: PHP Linting (Pint)
branch: ${{ steps.extract_branch.outputs.branch }}
skip_fetch: true
- name: Commit Changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: PHP Linting (Pint)
branch: ${{ steps.extract_branch.outputs.branch }}
skip_fetch: true
2 changes: 2 additions & 0 deletions config/settings.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

return [
/*
|--------------------------------------------------------------------------
Expand Down
32 changes: 31 additions & 1 deletion pint.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@
"types_spaces": {
"space": "none"
},
"single_trait_insert_per_statement": true
"combine_consecutive_issets": true,
"combine_consecutive_unsets": true,
"declare_parentheses": true,
"declare_strict_types": true,
"explicit_string_variable": true,
"single_trait_insert_per_statement": true,
"ordered_class_elements": {
"order": [
"use_trait",
"case",
"constant",
"constant_public",
"constant_protected",
"constant_private",
"property_public",
"property_protected",
"property_private",
"construct",
"destruct",
"magic",
"phpunit",
"method_abstract",
"method_public_static",
"method_public",
"method_protected_static",
"method_protected",
"method_private_static",
"method_private"
],
"sort_algorithm": "none"
}
}
}
10 changes: 5 additions & 5 deletions src/Drivers/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function extend(string $driver, Closure $callback): self
return $this;
}

public function setDefaultDriver(string $driver): void
{
$this->app['config']['settings.driver'] = $driver;
}

protected function createDatabaseDriver(array $config): DatabaseDriver
{
return new DatabaseDriver(
Expand All @@ -54,11 +59,6 @@ protected function getDefaultDriver(): string
return $this->app['config']['settings.driver'];
}

public function setDefaultDriver(string $driver): void
{
$this->app['config']['settings.driver'] = $driver;
}

protected function getDriverConfig(string $driver): ?array
{
return $this->app['config']["settings.drivers.{$driver}"];
Expand Down
8 changes: 4 additions & 4 deletions src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

class Setting extends Model implements SettingContract
{
public $timestamps = false;

protected ?string $teamForeignKey = null;

protected $guarded = ['id'];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
Expand All @@ -20,10 +24,6 @@ public function __construct(array $attributes = [])
$this->teamForeignKey = config('settings.team_foreign_key');
}

protected $guarded = ['id'];

public $timestamps = false;

public static function getValue(string $key, $default = null, $teamId = null)
{
$value = static::query()
Expand Down
140 changes: 70 additions & 70 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,62 +213,6 @@ public function isTrue(string $key, $default = true): bool
return $value === true || $value === '1' || $value === 1;
}

protected function normalizeKey(string $key): string
{
if (Str::startsWith(haystack: $key, needles: 'file_')) {
return str_replace(search: 'file_', replace: 'file.', subject: $key);
}

return $key;
}

protected function getCacheKey(string $key): string
{
$cacheKey = $this->cacheKeyPrefix . $key;

if ($this->teams) {
$teamId = $this->teamId ?? 'null';

$cacheKey .= "::team:{$teamId}";
}

return $cacheKey;
}

protected function getKeyForStorage(string $key): string
{
return $this->keyGenerator->generate(key: $key, context: $this->context);
}

protected function serializeValue($value): string
{
return $this->valueSerializer->serialize($value);
}

protected function unserializeValue($serialized)
{
if (! is_string($serialized)) {
return $serialized;
}

// Attempt to unserialize the value, but return the original value if that fails.
return rescue(fn () => $this->valueSerializer->unserialize($serialized), fn () => $serialized);
}

protected function shouldSetNewValue(string $key, $newValue): bool
{
if (! $this->cacheIsEnabled()) {
return true;
}

// To prevent decryption errors, we will check if we have a setting set for the current context and key.
if (! $this->doNotResetContext()->has($key)) {
return true;
}

return $newValue !== $this->doNotResetContext()->get($key);
}

public function disableCache(): self
{
$this->cacheEnabled = false;
Expand Down Expand Up @@ -297,15 +241,6 @@ public function setCache(Cache $cache): self
return $this;
}

protected function cacheIsEnabled(): bool
{
if ($this->temporarilyDisableCache) {
return false;
}

return $this->cacheEnabled && $this->cache !== null;
}

public function disableEncryption(): self
{
$this->encryptionEnabled = false;
Expand All @@ -327,11 +262,6 @@ public function setEncrypter(Encrypter $encrypter): self
return $this;
}

protected function encryptionIsEnabled(): bool
{
return $this->encryptionEnabled && $this->encrypter !== null;
}

public function enableTeams(): self
{
$this->teams = true;
Expand All @@ -358,6 +288,76 @@ public function useCacheKeyPrefix(string $prefix): self
return $this;
}

protected function normalizeKey(string $key): string
{
if (Str::startsWith(haystack: $key, needles: 'file_')) {
return str_replace(search: 'file_', replace: 'file.', subject: $key);
}

return $key;
}

protected function getCacheKey(string $key): string
{
$cacheKey = $this->cacheKeyPrefix . $key;

if ($this->teams) {
$teamId = $this->teamId ?? 'null';

$cacheKey .= "::team:{$teamId}";
}

return $cacheKey;
}

protected function getKeyForStorage(string $key): string
{
return $this->keyGenerator->generate(key: $key, context: $this->context);
}

protected function serializeValue($value): string
{
return $this->valueSerializer->serialize($value);
}

protected function unserializeValue($serialized)
{
if (! is_string($serialized)) {
return $serialized;
}

// Attempt to unserialize the value, but return the original value if that fails.
return rescue(fn () => $this->valueSerializer->unserialize($serialized), fn () => $serialized);
}

protected function shouldSetNewValue(string $key, $newValue): bool
{
if (! $this->cacheIsEnabled()) {
return true;
}

// To prevent decryption errors, we will check if we have a setting set for the current context and key.
if (! $this->doNotResetContext()->has($key)) {
return true;
}

return $newValue !== $this->doNotResetContext()->get($key);
}

protected function cacheIsEnabled(): bool
{
if ($this->temporarilyDisableCache) {
return false;
}

return $this->cacheEnabled && $this->cache !== null;
}

protected function encryptionIsEnabled(): bool
{
return $this->encryptionEnabled && $this->encrypter !== null;
}

protected function decryptValue($value)
{
if (! $this->encryptionIsEnabled()) {
Expand Down
16 changes: 8 additions & 8 deletions src/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public function packageRegistered(): void
$this->registerSettings();
}

public function provides(): array
{
return [
Settings::class,
'SettingsFactory',
];
}

protected function bootModelBindings(): void
{
$config = $this->app['config']['settings.drivers.eloquent'];
Expand Down Expand Up @@ -70,12 +78,4 @@ protected function registerSettings(): void
return $settings;
});
}

public function provides(): array
{
return [
Settings::class,
'SettingsFactory',
];
}
}
2 changes: 1 addition & 1 deletion tests/Feature/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ function isSerialized(mixed $data): bool
case 'd':
$end = '';

return (bool) preg_match("/^{$token}:[0-9.E+-]+;$end/", $data);
return (bool) preg_match("/^{$token}:[0-9.E+-]+;{$end}/", $data);
}

return false;
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\DB;
use Rawilk\Settings\Facades\Settings;
use Rawilk\Settings\Tests\TestCase;
Expand Down
14 changes: 7 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@

class TestCase extends Orchestra
{
protected function getPackageProviders($app): array
{
return [
SettingsServiceProvider::class,
];
}

public function getEnvironmentSetUp($app): void
{
$migrations = [
Expand All @@ -27,4 +20,11 @@ public function getEnvironmentSetUp($app): void
$migration->up();
}
}

protected function getPackageProviders($app): array
{
return [
SettingsServiceProvider::class,
];
}
}

0 comments on commit e7247fb

Please sign in to comment.