Skip to content

Commit

Permalink
Merge branch 'master' into publishable
Browse files Browse the repository at this point in the history
  • Loading branch information
AfzalSabbir committed Sep 17, 2022
2 parents 4a89c52 + b1d81f2 commit 74f7ab5
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 65 deletions.
78 changes: 67 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,80 @@
# slug-generator
# SlugGenerator - Laravel Slug Generator
> A Laravel package to generate slugs for your models.
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Travis](https://img.shields.io/travis/afzalsabbir/slug-generator.svg?style=flat-square)]()
[![Build Status](https://img.shields.io/travis/afzalsabbir/slug-generator/master.svg?style=flat-square)](https://travis-ci.org/afzalsabbir/slug-generator)
[![Quality Score](https://img.shields.io/scrutinizer/g/afzalsabbir/slug-generator.svg?style=flat-square)](https://scrutinizer-ci.com/g/afzalsabbir/slug-generator)

[![Latest Stable Version](https://img.shields.io/packagist/v/afzalsabbir/slug-generator.svg?style=flat-square)](https://packagist.org/packages/afzalsabbir/slug-generator)
[![Latest Unstable Version](https://img.shields.io/packagist/vpre/afzalsabbir/slug-generator.svg?style=flat-square)](https://packagist.org/packages/afzalsabbir/slug-generator)
[![Total Downloads](https://img.shields.io/packagist/dt/afzalsabbir/slug-generator.svg?style=flat-square)](https://packagist.org/packages/afzalsabbir/slug-generator)

## Installation

## Install
You can install the package via composer:

```bash
```php
composer require afzalsabbir/slug-generator
```


## Usage

Write a few lines about the usage of this package.

- Using this package is very simple. Just use the `SlugGenerator` trait in your model and it'll automatically generate a
slug for you. It'll use default configuration.
```php
// ...
use AfzalSabbir\SlugGenerator\SlugGenerator;
use Illuminate\Database\Eloquent\Model;
// ...

class Post extends Model
{
// ...
use SlugGenerator;
// ...
}
```
- Publish the config file to customize the slug generation. \
<sup>_[**Note:** if facing any issue run `php artisan vendor:publish` and find `AfzalSabbir\SlugGenerator\SlugGeneratorServiceProvider` to publish]_</sup>
```php
php artisan vendor:publish --provider="AfzalSabbir\SlugGenerator\SlugGeneratorServiceProvider" --tag="config"
```
- You can also configure the slug generation in your model.
```php
namespace App\Models;

use AfzalSabbir\SlugGenerator\SlugGenerator;
use Illuminate\Database\Eloquent\Model;
// ...

class Post extends Model
{
// ...
use SlugGenerator;

// ...
protected $slugGenerator = [
"set-on-create" => true, // Whether to set the slug when the model is created
"set-on-update" => false, // Whether to update the slug when the target field is updated
"target-field" => "title", // The field that will be used to generate the slug
"separator" => "-", // The separator that will be used to separate the words
"slug-field" => "slug", // The field that will be used to store the slug
];

// ...
}
```
- You can also use the `generateSlug(Model $model)` helper method to generate a slug.
```php
use AfzalSabbir\SlugGenerator\SlugGenerator;
use App\Models\Post;
// ...

$post = Post::find(1);
$post->title = "Hello World";
$post->slug = generateSlug($post);
$post->save();
```

## Testing

Expand All @@ -25,17 +84,14 @@ Run the tests with:
vendor/bin/phpunit
```


## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.


## Security

If you discover any security-related issues, please email [email protected] instead of using the issue tracker.


## License

The MIT License (MIT). Please see [License File](/LICENSE.md) for more information.
The MIT License (MIT). Please see [License File](/LICENSE.md) for more information.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "afzalsabbir/slug-generator",
"description": "Your Package Description here",
"description": "A simple package to generate slug from string for Laravel",
"type": "library",
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -31,15 +31,15 @@
},
"autoload": {
"psr-4": {
"Afzalsabbir\\SlugGenerator\\": "src/"
"AfzalSabbir\\SlugGenerator\\": "src/"
},
"files": [
"src/helpers.php"
"src/slug-generator-helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Afzalsabbir\\SlugGenerator\\Tests\\":"tests/"
"AfzalSabbir\\SlugGenerator\\Tests\\":"tests/"
}
},
"config": {
Expand All @@ -53,10 +53,10 @@
"extra": {
"laravel": {
"providers": [
"Afzalsabbir\\SlugGenerator\\SlugGeneratorServiceProvider"
"AfzalSabbir\\SlugGenerator\\SlugGeneratorServiceProvider"
],
"aliases": {
"Afzalsabbir": "Afzalsabbir\\SlugGenerator\\SlugGeneratorFacade"
"SlugGenerator": "AfzalSabbir\\SlugGenerator\\SlugGeneratorFacade"
}
}
}
Expand Down
9 changes: 0 additions & 9 deletions config/slug-generator.php

This file was deleted.

9 changes: 9 additions & 0 deletions config/sluggenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [
"set-on-create" => true, // Whether to set the slug when the model is created
"set-on-update" => false, // Whether to update the slug when the target field is updated
"target-field" => "title", // The field that will be used to generate the slug
"separator" => "-", // The separator that will be used to separate the words
"slug-field" => "slug", // The field that will be used to store the slug
];
2 changes: 1 addition & 1 deletion src/SlugGeneratorFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class SlugGeneratorFacade extends Facade
*/
protected static function getFacadeAccessor()
{
return 'slug-generator';
return 'SlugGenerator';
}
}
36 changes: 3 additions & 33 deletions src/SlugGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,10 @@ class SlugGeneratorServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->mergeConfigFrom(__DIR__ . '/../config/slug-generator.php', 'sluggenerator');
$this->mergeConfigFrom(__DIR__ . '/../config/sluggenerator.php', 'sluggenerator');

$this->publishConfig();
//$this->publishTraits();

// $this->loadViewsFrom(__DIR__.'/resources/views', 'slug-generator');
// $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
// $this->registerRoutes();
}

/**
* Register the package routes.
*
* @return void
*/
private function registerRoutes()
{
Route::group($this->routeConfiguration(), function () {
$this->loadRoutesFrom(__DIR__ . '/Http/routes.php');
});
}

/**
* Get route group configuration array.
*
* @return array
*/
private function routeConfiguration()
{
return [
'namespace' => "Afzalsabbir\SlugGenerator\Http\Controllers",
'middleware' => 'api',
'prefix' => 'api'
];
}

/**
Expand All @@ -58,7 +28,7 @@ private function routeConfiguration()
public function register()
{
// Register facade
$this->app->singleton('slug-generator', function () {
$this->app->singleton('sluggenerator', function () {
return new SlugGenerator;
});
}
Expand All @@ -72,7 +42,7 @@ public function publishConfig()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/slug-generator.php' => config_path('sluggenerator.php'),
__DIR__ . '/../config/sluggenerator.php' => config_path('sluggenerator.php'),
], 'config');
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Traits/SlugGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ trait SlugGenerator
public static function bootSlugGenerator(): void
{
static::creating(function ($model) {
if ($model->slug['set-on-create'] ?? config("sluggenerator.set-on-create")) {
if ($model->slugGenerator['set-on-create'] ?? config("sluggenerator.set-on-create")) {
$model->handle($model);
}
});

static::updating(function ($model) {
if ($model->slug['set-on-update'] ?? config("sluggenerator.set-on-update")) {
if ($model->slugGenerator['set-on-update'] ?? config("sluggenerator.set-on-update")) {
$model->handle($model);
}
});
Expand All @@ -41,9 +41,9 @@ public static function bootSlugGenerator(): void
public function handle(Model $model): string
{
try {
$targetField = $model->slug['target-field'] ?? config("sluggenerator.target-field");
$separator = $model->slug['separator'] ?? config("sluggenerator.separator");
$slugField = $model->slug['slug-field'] ?? config("sluggenerator.slug-field");
$targetField = $model->slugGenerator['target-field'] ?? config("sluggenerator.target-field");
$separator = $model->slugGenerator['separator'] ?? config("sluggenerator.separator");
$slugField = $model->slugGenerator['slug-field'] ?? config("sluggenerator.slug-field");

return $model->generateSlug(null, $slugField, $targetField, $separator);
}
Expand Down
File renamed without changes.

0 comments on commit 74f7ab5

Please sign in to comment.