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

Commit

Permalink
Merge 19a2405 into d2d7003
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed May 25, 2020
2 parents d2d7003 + 19a2405 commit f0c7228
Show file tree
Hide file tree
Showing 80 changed files with 2,147 additions and 2,331 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ jobs:
fail-fast: true
matrix:
php: [7.4, 7.3, 7.2.15]
laravel: [7.*, 6.*]
laravel: [7.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 7.*
testbench: 5.*
- laravel: 6.*
testbench: 4.*

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

Expand Down
107 changes: 52 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,18 @@ Laratraits is a Laravel package containing useful traits and some classes to use

## Requirements

* Laravel 6, or Laravel 7.
* Laravel 7.
* PHP 7.2.15 or later.

## [Upgrade Guide from 1.x to 2.x](UPGRADE.md)

## Installation

Fire up Composer and that's it.

composer require darkghosthunter/laratraits

## What it includes

Before installing, take a look into the list. If you're only using one, just copy and paste it in your project, no problem, as each trait and file includes a copy of the MIT License. Just remember to change the namespace.

### General

* [`DiscoverClasses`](src/DiscoverClasses.php): Discovers classes inside a directory, optionally filtered by a method name or interface.
* [`DispatchesItself`](src/DispatchesItself.php): Allows to dispatch the object instance to one of many Jobs previously set.
* [`Multitaps`](src/Multitaps.php): Makes a class infinitely tap-able. You can exit the tap using `->target` or a method name appended with `AndUntap`.
* [`RendersFromMarkdown`](src/RendersFromMarkdown.php): Takes a given class property to parse Markdown text and return HTML. Compatible with `Htmlable` interface.
* [`ValidatesItself`](src/ValidatesItself.php): Validates an incoming data using self-contained rules.
* [`SavesToSession`](src/SavesToSession.php): Saves the object (or part of it) to the session.
* [`SavesToCache`](src/SavesToCache.php): Saves the object (or part of it) to the cache..php
* [`SavesToStorage`](src/SavesToStorage.php): Saves the object (or part of it) to the storage.
* [`EnumerableStates`](src/EnumerableStates.php): Allows a class to have one of an strict list of possible states. You can also use the [`Enumerable`](src/Enumerable.php) class separately. Useful for [casting enums](https://laravel.com/docs/eloquent-mutators#custom-casts).

### Models

* [`AutoFill`](src/Models/AutoFill.php): Automatically fills the Model with values by each method name, like `fillFooAttribute()`.
* [`UsesUuid`](src/Models/UsesUuid.php): Automatically fills the UUID on the Model. Comes with an optional Eloquent Query Builder local scopes. You can override the UUID generation.
* [`DefaultColumns`](src/Models/DefaultColumns.php): Adds a `DefaultColumns` Global Scope to the Model that selects only given default columns, unless overrun manually in the query.
* [`SoftCachesAccessors`](src/Models/SoftCachesAccessors.php): Saves the result of a accessor to avoid running the accessor logic again. Overrides the `mutateAttribute()` method.
* [`DynamicallyMutates`](src/Models/DynamicallyMutates.php): Cast an attribute based on what other attribute type says. Useful for columns that hols the data type, and other the raw data value.
* [`NeighbourRecords`](src/Models/NeighbourRecords.php): Allows to easily get the "next" and "previous" record from a given model.
* [`HasSlug`](src/Models/HasSlug.php): Allows a Model to be bound to routes using the slug like `this-is-the-model`. Requires a new column in the table.
* [`ModelType`](src/Models/ModelType.php): Useful for Models that share a single table but a different "column type".
* [`HasFile`](src/Models/HasFile.php): Associates a single file to the Model. The File contents is automatically saved when model is persisted/updated. Hash checking is always done.

### Pipelines

* [`PipesThrough`](src/PipesThrough.php): Allows any class to be sent itself through a pipeline or, alternatively, queue a Job to process the pipeline later.

### Global Scopes

* [`MacrosEloquent`](src/Scopes/MacrosEloquent.php): Automatically adds selective Macros to the Eloquent Builder instance itself, instead of globally, when using a Global Scope. Append a method with "macro" and return a Closure to use as macro.

### Controllers

* [`ThrottlesRequests`](src/Controllers/ThrottlesRequests.php): An automatic and customizable request throttler, much like the default `ThrottlesLogins` trait.

### Middleware

* [`CacheStaticResponse`](src/Middleware/CacheStaticResponse.php): Caches (hopefully) static responses, avoiding running the controller logic, for a given time.
* [`ShareAuthenticatedUser`](src/Middleware/ShareAuthenticatedUser.php): Shares the authenticated user across all views.
* [`ValidateConsumableSignature`](src/Middleware/ValidateConsumableSignature.php): Makes signed routes work only one time except on client or server errors.

## Installing

Just fire up composer and that's it.

composer require darkghosthunter/laratraits
This package doesn't uses a Service Provider.

## Usage

Expand All @@ -85,7 +37,7 @@ namespace App;

use Illuminate\Database\Eloquent\Model;
use DarkGhostHunter\Laratraits\SavesToCache;
use DarkGhostHunter\Laratraits\Models\UsesUuid;
use DarkGhostHunter\Laratraits\Eloquent\UsesUuid;

class Post extends Model
{
Expand All @@ -96,9 +48,54 @@ class Post extends Model
}
```

Some traits may instance other classes for advanced logic if necessary, like the `DiscoverClasses` trait.

> There is no application overhead since there is no Service Provider registered.
## What it includes

Before installing, take a look into the list. If you're only using one, just copy and paste it in your project, no problem, as each trait and file includes a copy of the MIT License. .

Just remember to **change the namespace** if you're copy-pasting them!

### Traits for everything

* [`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.
* [`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.
* [`SavesToStorage`](src/SavesToStorage.php): Saves the object (or part of it) to the storage.
* [`SendsToHttp`](src/SendsToHttp.php): Sends the object (or part of it) through an HTTP Request.
* [`ThrottleMethods`](src/ThrottleMethods.php): Throttles a given method in a class transparently.
* [`ValidatesItself`](src/ValidatesItself.php): Validates an incoming data using self-contained rules.

### 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).

### Models

* [`FillsAttributes`](src/Eloquent/FillsAttributes.php): Automatically fills the Model with values by each method name, like `fillFooAttribute()`.
* [`DefaultColumns`](src/Eloquent/DefaultColumns.php): Adds a `DefaultColumns` Global Scope to the Model selecting only given default columns, unless overrun manually in the query.
* [`HasSlug`](src/Eloquent/HasSlug.php): Allows a Model to be bound to routes using the slug like `this-is-the-model`. Requires a new column in the table.
* [`ModelType`](src/Eloquent/ModelType.php): Useful for Models that share a single table but have different "types", like Publications: Article, Post, Note, etc.
* [`NeighbourRecords`](src/Eloquent/NeighbourRecords.php): Allows to easily get the "next" and "previous" record from a given model.
* [`UsesUuid`](src/Eloquent/UsesUuid.php): Automatically fills the UUID on the Model. Comes with an optional Eloquent Query Builder local scopes. You can override the UUID generation.

#### Casts

* [`CastEnumerable`](src/Eloquent/Casts/CastEnumerable.php): Allows a string or integer column to be [_casted_](https://laravel.com/docs/eloquent-mutators#custom-casts) as Enumerable inside a model.
* [`CastsRepository`](src/Eloquent/Casts/CastRepository.php): Allows an json column to be [_casted_](https://laravel.com/docs/eloquent-mutators#custom-casts) as a Repository (like a config tree).

### Global Scopes

* [`MacrosEloquent`](src/Scopes/MacrosEloquent.php): Automatically adds selective Macros to the Eloquent Builder instance itself, instead of globally, when using a Global Scope. Append `macro` to a public static method and that's it, done.

### Middleware

* [`CacheStaticResponse`](src/Middleware/CacheStaticResponse.php): Caches static responses, avoiding running the controller logic, for a given time.
* [`ShareAuthenticatedUser`](src/Middleware/ShareAuthenticatedUser.php): Shares the authenticated user across all views.
* [`ValidateConsumableSignature`](src/Middleware/ValidateConsumableSignature.php): Makes [signed routes](https://laravel.com/docs/urls#signed-urls) work only one time, except on client or server errors.

## Missing a trait?

Expand Down
86 changes: 86 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Upgrading from v1.x to v2.x

## Removed

The following traits and classes has been removed:

* `ClassDiscoverer`,
* `DiscoverClasses`,
* `DispatchesItself`,
* `HasFile`.
* `SoftCachesAccessors`,
* `ThrottlesRequest`,

For `HasFile`, migrate to [Spatie's Laravel Medialibrary](https://github.com/spatie/laravel-medialibrary) package instead.

For `ThrottlesRequest`, migrate to [Laravel Rate Limiting](https://github.com/laravel/framework/pull/32726) once it becomes available.

## Changed

### Namespace change

* The `Laratraits\Models` namespace has been changed to `Laratraits\Eloquent`.

## AutoFill trait changed

* The trait has been renamed to `FillsAttributes`

* The trait will now call Mutators when filling automatically a property.

## DefaultColumns trait changed

* It now incorporates the `withoutDefaultColumns()` method to disable the global scope that is applied automatically.

## DynamicallyMutates removed

* The `DynamicallyMutates` trait has been removed. Use [Custom Casts](https://laravel.com/docs/eloquent-mutators#custom-casts) instead.

### Enumerable changed

* The `Enumerable` class incorporates a new method called `assign` that assigns a given state, alternatively to calling dynamically a method call.

### HasSlug changed

* The `attributeToSlug` method has changed to `sluggableAttribute` and now returns the string `title`.

* The `setSlugAttribute` method has changed to `slugValue`, and returns the slug value.

* The `initializeHasSlug` method has been removed.

* The `setSlug` method has been added. It automatically sets the slug attribute with the slug value from a given attribute value.

* The slug will be created on saving the model only when the sluggable attribute has been modified.

* Routing now is optional. To disable routing by the slug, declare the `$routeBySlug` property as `false`.

## MacrosEloquent changed

* Now the trait will automatically re-route all Eloquent builder macros to the public static methods in the Scope. There is no longer need to return closures or what not.

### ModelType changed

* The `getQualifiedTypeColumn` method has been changed to `getModelTypeColumn`.

* The `getTypeName` method has been changed to `getModelType`, and now returns the class name as snake case by default.

### NeighbourRecords changed

* The trait now gets the latest and oldest records from the given model, using the creation timestamp of these.

* It now includes the `filterNeighbourQuery` that allows to further filter the records to retrieve.

* It also includes the `queryColumns` to filter which columns to retrieve. It defaults to only the key used for routing (which is the primary key by default)

## ValidateConsumableSignature changed

* The middleware no longer uses `terminate`, but rather, evaluates the response before sending it to the browser. This shrinks the window where a user can make two times the same request.

## UsesUuid changed

* The `getQualifiedUuidColumn()` is now `getUuidColumn()`.

* The `shouldAddGlobalScope` is now `addUuidGlobalScope()`.

## UuidScope changed

* Now all the scopes has been changed to public static methods to follow the changes on the `MacrosEloquent` trait.
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
"require": {
"php": "^7.2.15",
"ext-json": "*",
"illuminate/support": "6.*|7.*",
"symfony/finder": "4.*|5.*"
"illuminate/support": "^7.0",
"guzzlehttp/guzzle": "^6.5"
},
"require-dev": {
"orchestra/testbench": "~4.1|5.*"
"orchestra/testbench": "^5.2"
},
"autoload": {
"psr-4": {
Expand All @@ -41,7 +41,6 @@
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"

},
"config": {
"sort-packages": true
Expand Down

0 comments on commit f0c7228

Please sign in to comment.