From 522f06162e692f3923820c1ca25277f38fbbba82 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Tue, 13 Jun 2017 19:17:55 +0100 Subject: [PATCH] Change sluggable implementation --- app/Article.php | 23 +++++-- app/Place.php | 23 +++++-- changelog.md | 4 ++ composer.json | 2 +- composer.lock | 160 +++++++++++++++++++++++++++++++------------ config/app.php | 5 ++ config/sluggable.php | 126 ++++++++++++++++++++++++++++++++++ 7 files changed, 285 insertions(+), 58 deletions(-) create mode 100644 config/sluggable.php diff --git a/app/Article.php b/app/Article.php index 84324892..b7f89ee8 100644 --- a/app/Article.php +++ b/app/Article.php @@ -4,18 +4,13 @@ namespace App; use Illuminate\Database\Eloquent\Model; use League\CommonMark\CommonMarkConverter; -use MartinBean\Database\Eloquent\Sluggable; +use Cviebrock\EloquentSluggable\Sluggable; use Illuminate\Database\Eloquent\SoftDeletes; class Article extends Model { - use SoftDeletes; - /* - * We want to turn the titles into slugs - */ use Sluggable; - const DISPLAY_NAME = 'title'; - const SLUG = 'titleurl'; + use SoftDeletes; /** * The attributes that should be mutated to dates. @@ -31,6 +26,20 @@ class Article extends Model */ protected $table = 'articles'; + /** + * Return the sluggable configuration array for this model. + * + * @return array + */ + public function sluggable() + { + return [ + 'titleurl' => [ + 'source' => 'title' + ] + ]; + } + /** * Define the relationship with webmentions. * diff --git a/app/Place.php b/app/Place.php index 466a345f..0b2dcb73 100644 --- a/app/Place.php +++ b/app/Place.php @@ -6,18 +6,13 @@ use DB; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Phaza\LaravelPostgis\Geometries\Point; -use MartinBean\Database\Eloquent\Sluggable; +use Cviebrock\EloquentSluggable\Sluggable; use Phaza\LaravelPostgis\Eloquent\PostgisTrait; class Place extends Model { - use PostgisTrait; - /* - * We want to turn the names into slugs. - */ use Sluggable; - const DISPLAY_NAME = 'name'; - const SLUG = 'slug'; + use PostgisTrait; /** * The attributes that are mass assignable. @@ -36,6 +31,20 @@ class Place extends Model 'polygon', ]; + /** + * Return the sluggable configuration array for this model. + * + * @return array + */ + public function sluggable() + { + return [ + 'slug' => [ + 'source' => 'name' + ] + ]; + } + /** * Define the relationship with Notes. * diff --git a/changelog.md b/changelog.md index ab9ddb36..6ad7daa6 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # Changelog +## Version {next} + - Add support for ownyourgram.com sending h-card locations + - change sluggable implementation + ## Version 0.5.14 (2017-06-11) - Remove some Log statements in-appropriate for porduction diff --git a/composer.json b/composer.json index d1ff9e16..a29172c5 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "type": "project", "require": { "php": ">=7.1.0", + "cviebrock/eloquent-sluggable": "^4.2", "ezyang/htmlpurifier": "~4.6", "guzzlehttp/guzzle": "~6.0", "indieauth/client": "~0.1", @@ -18,7 +19,6 @@ "lcobucci/jwt": "^3.1", "league/commonmark": "^0.15.0", "league/flysystem-aws-s3-v3": "^1.0", - "martinbean/laravel-sluggable-trait": "0.2.*", "mf2/mf2": "~0.3", "phaza/laravel-postgis": "~3.1", "pmatseykanets/laravel-scout-postgres": "^0.5.0", diff --git a/composer.lock b/composer.lock index 5a02219e..db90587c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "7cfa8f05a76283f45a0f1a30d00ecbad", + "content-hash": "af5dc6e36b50396acecb6c17d3392645", "packages": [ { "name": "aws/aws-sdk-php", @@ -180,6 +180,70 @@ ], "time": "2016-08-19T16:43:44+00:00" }, + { + "name": "cocur/slugify", + "version": "v2.5", + "source": { + "type": "git", + "url": "https://github.com/cocur/slugify.git", + "reference": "e8167e9a3236044afebd6e8ab13ebeb3ec9ca145" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/cocur/slugify/zipball/e8167e9a3236044afebd6e8ab13ebeb3ec9ca145", + "reference": "e8167e9a3236044afebd6e8ab13ebeb3ec9ca145", + "shasum": "" + }, + "require": { + "php": ">=5.5.9" + }, + "require-dev": { + "laravel/framework": "~5.1", + "latte/latte": "~2.2", + "league/container": "^2.2.0", + "mikey179/vfsstream": "~1.6", + "mockery/mockery": "~0.9", + "nette/di": "~2.2", + "phpunit/phpunit": "~4.8|~5.2", + "pimple/pimple": "~1.1", + "plumphp/plum": "~0.1", + "silex/silex": "~1.3", + "symfony/config": "~2.4|~3.0", + "symfony/dependency-injection": "~2.4|~3.0", + "symfony/http-kernel": "~2.4|~3.0", + "twig/twig": "~1.26|~2.0", + "zendframework/zend-modulemanager": "~2.2", + "zendframework/zend-servicemanager": "~2.2", + "zendframework/zend-view": "~2.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "Cocur\\Slugify\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ivo Bathke", + "email": "ivo.bathke@gmail.com" + }, + { + "name": "Florian Eckerstorfer", + "email": "florian@eckerstorfer.co", + "homepage": "https://florian.ec" + } + ], + "description": "Converts a string into a slug.", + "keywords": [ + "slug", + "slugify" + ], + "time": "2017-03-23T21:52:55+00:00" + }, { "name": "composer/ca-bundle", "version": "1.0.7", @@ -239,6 +303,58 @@ ], "time": "2017-03-06T11:59:08+00:00" }, + { + "name": "cviebrock/eloquent-sluggable", + "version": "4.2.3", + "source": { + "type": "git", + "url": "https://github.com/cviebrock/eloquent-sluggable.git", + "reference": "cbeb6e16b783173dbb95e00f78237d349e73c07d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/cviebrock/eloquent-sluggable/zipball/cbeb6e16b783173dbb95e00f78237d349e73c07d", + "reference": "cbeb6e16b783173dbb95e00f78237d349e73c07d", + "shasum": "" + }, + "require": { + "cocur/slugify": "^2.3", + "illuminate/config": "^5.4", + "illuminate/database": "^5.4", + "illuminate/support": "^5.4", + "php": ">=5.6.4 || ^7.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "orchestra/database": "~3.4", + "orchestra/testbench": "^3.4", + "phpunit/phpunit": "~5.7" + }, + "type": "library", + "autoload": { + "psr-4": { + "Cviebrock\\EloquentSluggable\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Colin Viebrock", + "email": "colin@viebrock.ca" + } + ], + "description": "Easy creation of slugs for your Eloquent models in Laravel 5.", + "homepage": "https://github.com/cviebrock/eloquent-sluggable", + "keywords": [ + "eloquent", + "laravel", + "slug" + ], + "time": "2017-04-18T15:03:20+00:00" + }, { "name": "dnoegel/php-xdg-base-dir", "version": "0.1", @@ -1971,48 +2087,6 @@ "description": "Flysystem adapter for the AWS S3 SDK v3.x", "time": "2017-06-08T13:37:03+00:00" }, - { - "name": "martinbean/laravel-sluggable-trait", - "version": "0.2.0", - "source": { - "type": "git", - "url": "https://github.com/martinbean/laravel-sluggable-trait.git", - "reference": "8984dc9bc2596814f79baf44aeb9a39c9c07b149" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/martinbean/laravel-sluggable-trait/zipball/8984dc9bc2596814f79baf44aeb9a39c9c07b149", - "reference": "8984dc9bc2596814f79baf44aeb9a39c9c07b149", - "shasum": "" - }, - "require": { - "illuminate/database": ">=4.0", - "illuminate/support": ">=4.0", - "php": ">=5.4.0" - }, - "type": "library", - "autoload": { - "psr-4": { - "MartinBean\\Database\\Eloquent\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Martin Bean", - "email": "martin@martinbean.co.uk" - } - ], - "description": "A trait you can apply to Eloquent models to have slugs automatically generated on save.", - "keywords": [ - "eloquent", - "laravel" - ], - "time": "2015-02-17T22:47:44+00:00" - }, { "name": "mf2/mf2", "version": "v0.3.2", diff --git a/config/app.php b/config/app.php index 1b96380e..9a315d60 100644 --- a/config/app.php +++ b/config/app.php @@ -235,6 +235,11 @@ return [ */ Laravel\Tinker\TinkerServiceProvider::class, + /* + * Eolquent Sluggable + */ + Cviebrock\EloquentSluggable\ServiceProvider::class, + ], /* diff --git a/config/sluggable.php b/config/sluggable.php new file mode 100644 index 00000000..6c5cb085 --- /dev/null +++ b/config/sluggable.php @@ -0,0 +1,126 @@ +name; + * + * Or it can be an array of fields, like ("name", "company"), which builds a slug from: + * + * $model->name . ' ' . $model->company; + * + * If you've defined custom getters in your model, you can use those too, + * since Eloquent will call them when you request a custom attribute. + * + * Defaults to null, which uses the toString() method on your model. + */ + + 'source' => null, + + /** + * The maximum length of a generated slug. Defaults to "null", which means + * no length restrictions are enforced. Set it to a positive integer if you + * want to make sure your slugs aren't too long. + */ + + 'maxLength' => null, + + /** + * If left to "null", then use the cocur/slugify package to generate the slug + * (with the separator defined below). + * + * Set this to a closure that accepts two parameters (string and separator) + * to define a custom slugger. e.g.: + * + * 'method' => function( $string, $sep ) { + * return preg_replace('/[^a-z]+/i', $sep, $string); + * }, + * + * Otherwise, this will be treated as a callable to be used. e.g.: + * + * 'method' => array('Str','slug'), + */ + + 'method' => null, + + /** + * Separator to use when generating slugs. Defaults to a hyphen. + */ + + 'separator' => '-', + + /** + * Enforce uniqueness of slugs? Defaults to true. + * If a generated slug already exists, an incremental numeric + * value will be appended to the end until a unique slug is found. e.g.: + * + * my-slug + * my-slug-1 + * my-slug-2 + */ + + 'unique' => true, + + /** + * If you are enforcing unique slugs, the default is to add an + * incremental value to the end of the base slug. Alternatively, you + * can change this value to a closure that accepts three parameters: + * the base slug, the separator, and a Collection of the other + * "similar" slugs. The closure should return the new unique + * suffix to append to the slug. + */ + + 'uniqueSuffix' => null, + + /** + * Should we include the trashed items when generating a unique slug? + * This only applies if the softDelete property is set for the Eloquent model. + * If set to "false", then a new slug could duplicate one that exists on a trashed model. + * If set to "true", then uniqueness is enforced across trashed and existing models. + */ + + 'includeTrashed' => false, + + /** + * An array of slug names that can never be used for this model, + * e.g. to prevent collisions with existing routes or controller methods, etc.. + * Defaults to null (i.e. no reserved names). + * Can be a static array, e.g.: + * + * 'reserved' => array('add', 'delete'), + * + * or a closure that returns an array of reserved names. + * If using a closure, it will accept one parameter: the model itself, and should + * return an array of reserved names, or null. e.g. + * + * 'reserved' => function( Model $model) { + * return $model->some_method_that_returns_an_array(); + * } + * + * In the case of a slug that gets generated with one of these reserved names, + * we will do: + * + * $slug .= $separator + "1" + * + * and continue from there. + */ + + 'reserved' => null, + + /** + * Whether to update the slug value when a model is being + * re-saved (i.e. already exists). Defaults to false, which + * means slugs are not updated. + * + * Be careful! If you are using slugs to generate URLs, then + * updating your slug automatically might change your URLs which + * is probably not a good idea from an SEO point of view. + * Only set this to true if you understand the possible consequences. + */ + + 'onUpdate' => false, + +];