Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #36 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Multiple updates
  • Loading branch information
DarkGhostHunter committed Aug 4, 2021
2 parents dba2ef2 + bb29c39 commit bff4564
Show file tree
Hide file tree
Showing 50 changed files with 828 additions and 469 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/php.yml
Expand Up @@ -11,14 +11,12 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [8.0, 7.4, 7.3]
laravel: [8.*, 7.*]
php: [8.0, 7.4]
laravel: [8.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 8.*
testbench: 6.*
- laravel: 7.*
testbench: 5.*
testbench: ^6.19

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - ${{ matrix.dependency-version }}

Expand Down
20 changes: 11 additions & 9 deletions README.md
Expand Up @@ -8,10 +8,8 @@ Laratraits is a Laravel package containing useful traits and some classes to use

## Requirements

* Laravel 7.x, Laravel 8.0.
* PHP 7.3, PHP 8.0 or later.

## [Upgrade Guide from 1.x to 2.x](UPGRADE.md)
* Laravel 8.0.
* PHP 7.4, PHP 8.0 or later.

## Installation

Expand Down Expand Up @@ -54,11 +52,12 @@ Just remember to **change the namespace** if you're copy-pasting them!
### Traits for everything

* [`Comparable`](src/Comparable.php): Allows a class or its properties to be compared to a list of values.
* [`ConditionCalls`](src/ConditionCalls.php): Allows an object instance to execute `when` and `unless` logic.
* [`EnumerableStates`](src/EnumerableStates.php): Allows a class instance to have a single allowed state.
* ~~[`ConditionCalls`](src/ConditionCalls.php): Allows an object instance to execute `when` and `unless` logic.~~
* ~~[`EnumerableStates`](src/EnumerableStates.php): Allows a class instance to have a single allowed state.~~
* [`FiresItself`](src/FiresItself.php): Allows an Event to be fired conveniently.
* [`Multitaps`](src/Multitaps.php): Makes all class methods _chainable_, like using `tap()` but forever. You can exit the tap using `->target` or a method name appended with `AndUntap`.
* [`PipesThrough`](src/PipesThrough.php): Allows a class to be piped through a pipeline immediately or to a queue.
* [`RegeneratesCache`](src/RegeneratesCache.php): Stores a copy of an object (or part of it) avoiding data-races among other processes.
* [`RendersFromMarkdown`](src/RendersFromMarkdown.php): Takes a given class property to parse Markdown text and return HTML. Compatible with `Htmlable` interface.
* [`SavesToCache`](src/SavesToCache.php): Saves the object (or part of it) to the cache.
* [`SavesToSession`](src/SavesToSession.php): Saves the object (or part of it) to the session.
Expand All @@ -70,13 +69,12 @@ Just remember to **change the namespace** if you're copy-pasting them!

### Useful classes

* [`Enumerable`](src/Enumerable.php): Lists and controls a state from a list. Useful for [casting enums](https://laravel.com/docs/eloquent-mutators#custom-casts).
* ~~[`Enumerable`](src/Enumerable.php): Lists and controls a state from a list. Useful for [casting enums](https://laravel.com/docs/eloquent-mutators#custom-casts).~~

### Models

* [`ConditionFill`](src/Eloquent/ConditionFill.php): Fills an attribute if a given condition is truthy or falsy.
* [`DefaultColumns`](src/Eloquent/DefaultColumns.php): Adds a `DefaultColumns` Global Scope to the Model selecting only given default columns, unless overrun manually in the query.
* ~~[`EncryptsJson`](src/Eloquent/EncryptsJson.php): Encrypts and decrypts the JSON representation of a Model.~~
* [`FillsAttributes`](src/Eloquent/FillsAttributes.php): Automatically fills the Model with values by each method name, like `fillFooAttribute()`.
* [`FromRequest`](src/Eloquent/FromRequest.php): One-liners to make, create, or update models from the HTTP Request.
* [`HasSlug`](src/Eloquent/HasSlug.php): Allows a Model to be bound to routes using the slug like `this-is-the-model`. Must use an exclusive slug column in the model table.
Expand All @@ -86,7 +84,7 @@ Just remember to **change the namespace** if you're copy-pasting them!

#### Casts

* [`CastEnumerable`](src/Eloquent/Casts/CastEnumerable.php): Allows an [`Enumerable`](src/Enumerable.php) to be saved as an integer o string, and retrieved back to an `Enumerable` instance.
* ~~[`CastEnumerable`](src/Eloquent/Casts/CastEnumerable.php): Allows an [`Enumerable`](src/Enumerable.php) to be saved as an integer o string, and retrieved back to an `Enumerable` instance.~~
* [`CastsRepository`](src/Eloquent/Casts/CastRepository.php): Allows a Config Repository string to be saved a JSON, and retrieved back as Config Repository.
* [`CastsBase64`](src/Eloquent/Casts/CastsBase64.php): Allows a binary value to be saved as BASE64 in the database, and retrieved back as binary.

Expand All @@ -104,6 +102,10 @@ Just remember to **change the namespace** if you're copy-pasting them!

* [`RegistersFileDirective`](src/Blade/RegistersFileDirective.php): Easily register a directive using a PHP file contents.

### Migrations:

* [`PublishesMigrations`](src/ServiceProviders/PublishesMigrations.php): Allows a package to register migrations automatically as publishable assets.

## Missing a trait?

You can make an issue with your proposal. Consider the logic must be contained inside a trait, or use an auxiliar class to avoid polluting the class with multiple methods. PRs are preferred with tests.
Expand Down
86 changes: 0 additions & 86 deletions UPGRADE.md

This file was deleted.

12 changes: 6 additions & 6 deletions composer.json
Expand Up @@ -20,15 +20,15 @@
}
],
"require": {
"php": "^7.3||^7.4||^8.0",
"php": "^7.4||^8.0",
"ext-json": "*",
"illuminate/support": "^7.0||^8.0",
"guzzlehttp/guzzle": "^6.5||^7.0"
"illuminate/support": "^8.0",
"guzzlehttp/guzzle": "^7.0"
},
"require-dev": {
"orchestra/testbench": "^5.2||^6.0",
"phpunit/phpunit": "^9.3",
"mockery/mockery":"^1.4"
"orchestra/testbench": "^6.19",
"phpunit/phpunit": "^9.5",
"mockery/mockery":"^1.4.3"
},
"autoload": {
"psr-4": {
Expand Down
11 changes: 6 additions & 5 deletions src/Blade/RegistersFileDirective.php
Expand Up @@ -65,6 +65,7 @@
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use ReflectionClass;

use const DIRECTORY_SEPARATOR as DS;

Expand All @@ -73,12 +74,12 @@ trait RegistersFileDirective
/**
* Registers the directive.
*
* @param mixed $expression
* @param string|null $expression
* @param string|null $path
* @return string
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public static function register($expression = null, string $path = null)
public static function register(string $expression = null, string $path = null): string
{
$contents = Str::finish(stripcslashes(static::getDirectiveContents($path)), ' ?>');

Expand All @@ -96,7 +97,7 @@ public static function register($expression = null, string $path = null)
* @return string
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected static function getDirectiveContents(?string $path = null)
protected static function getDirectiveContents(string $path = null): string
{
// If the developer has returned a path for the directive, we will use just that.
// Otherwise, we will just cycle between a list of predefined paths where the
Expand All @@ -105,9 +106,9 @@ protected static function getDirectiveContents(?string $path = null)
return File::get($path);
}

$file = Str::finish($path ?? Str::snake(class_basename(static::class)), '.php');
$file = Str::finish(Str::snake(class_basename(static::class)), '.php');

$dir = dirname((new \ReflectionClass(static::class))->getFileName());
$dir = dirname((new ReflectionClass(static::class))->getFileName());

$paths = [
resource_path('views' . DS . 'directives' . DS . $file),
Expand Down
147 changes: 147 additions & 0 deletions src/CacheRegenerator.php
@@ -0,0 +1,147 @@
<?php

namespace DarkGhostHunter\Laratraits;

use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Carbon;
use Serializable;

class CacheRegenerator implements Serializable
{
/**
* The time suffix to check the storing time.
*
* @var string
*/
public static string $timeSuffix = ':time';

/**
* Cache repository to store and retrieve.
*
* @var \Illuminate\Contracts\Cache\Repository
*/
protected Repository $store;

/**
* The object to cache.
*
* @var object
*/
protected object $object;

/**
* The time the object became not equal to the source.
*
* @var \Illuminate\Support\Carbon|null
*/
protected ?Carbon $invalidAt = null;

/**
* The cache key to use to operate.
*
* @var string
*/
protected string $key;

/**
* CacheRegenerator constructor.
*
* @param object $object
* @param string|\Illuminate\Contracts\Cache\Repository $store
* @param string $key
*/
public function __construct(object $object, $store, string $key)
{
$this->store = $store;
$this->object = $object;
$this->key = $key;
}

/**
* Sets the object as no longer equal to the source.
*
* @param bool $forget
*/
public function invalidate(bool $forget = false): void
{
$this->invalidAt = now();

if ($forget) {
$this->forget();
}
}

/**
* Forgets the cached data.
*
* @return void
*/
public function forget(): void
{
$this->store->forget($this->key);
$this->store->forget($this->key.':time');
}

/**
* Regenerates the data in the cache.
*
* @param int|\DateTimeInterface $ttl
* @param bool $force
*
* @return bool Return "true" if stored, "false" if not.
*/
public function regenerate($ttl = 60, bool $force = false): bool
{
if (!$force && !$this->shouldRegenerate()) {
return false;
}

$this->store->setMultiple([
$this->key.static::$timeSuffix => $now = now(),
$this->key => method_exists($this->object,
'toCache') ? $this->object->toCache() : $this->object,
], $ttl);

$this->invalidAt = $now;

return true;
}

/**
* Check if the current data is fresher than the stored one.
*
* @return bool
*/
protected function shouldRegenerate(): bool
{
if (!$this->invalidAt) {
return true;
}

return $this->invalidAt->isAfter($this->store->get($this->key.static::$timeSuffix));
}

/**
* String representation of object.
*
* @return string|null
*/
public function serialize(): ?string
{
return null; // Don't serialize
}

/**
* Constructs the object.
*
* @param string $data The string representation of the object.
*
* @return void
*
* @codeCoverageIgnore
*/
public function unserialize($data): void
{
// Don't unserialize
}
}

0 comments on commit bff4564

Please sign in to comment.