Skip to content

Commit 6980b63

Browse files
authored
Shortcode artisan command make:shortcode (#83)
1 parent 6c1b7df commit 6980b63

File tree

6 files changed

+181
-9
lines changed

6 files changed

+181
-9
lines changed

README.md

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ If you are looking for Laravel 4.2, see: https://github.com/patrickbrouwers/Lara
2727

2828
Via Composer
2929

30-
``` bash
30+
```bash
3131
$ composer require "webwizo/laravel-shortcodes:1.0.*"
3232
```
3333

@@ -107,7 +107,7 @@ Shortcode::strip($contents);
107107

108108
Create a new ServiceProvider where you can register all the shortcodes.
109109

110-
``` bash
110+
```bash
111111
php artisan make:provider ShortcodesServiceProvider
112112
```
113113

@@ -128,6 +128,7 @@ php artisan make:provider ShortcodesServiceProvider
128128
```
129129

130130
ShortcodesServiceProvider.php Class File
131+
131132
```php
132133
<?php namespace App\Providers;
133134

@@ -163,6 +164,7 @@ class ShortcodesServiceProvider extends ServiceProvider
163164
### Default class for BoldShortcode
164165

165166
You can store each shortcode within their class `app/Shortcodes/BoldShortcode.php`
167+
166168
```php
167169
namespace App\Shortcodes;
168170

@@ -172,13 +174,14 @@ class BoldShortcode {
172174
{
173175
return sprintf('<strong class="%s">%s</strong>', $shortcode->class, $content);
174176
}
175-
177+
176178
}
177179
```
178180

179181
### Class with custom method
180182

181183
You can store each shortcode within their class `app/Shortcodes/ItalicShortcode.php`
184+
182185
```php
183186
namespace App\Shortcodes;
184187

@@ -188,7 +191,7 @@ class ItalicShortcode {
188191
{
189192
return sprintf('<i class="%s">%s</i>', $shortcode->class, $content);
190193
}
191-
194+
192195
}
193196
```
194197

@@ -203,10 +206,43 @@ class BoldShortcode {
203206
{
204207
return '<strong '. $shortcode->get('class', 'default') .'>' . $content . '</strong>';
205208
}
206-
209+
207210
}
208211
```
209212

213+
## Shortcode Artisan Generator Command
214+
215+
This package provides an Artisan command to quickly generate shortcode classes:
216+
217+
```php
218+
php artisan make:shortcode YourShortcodeName
219+
```
220+
221+
- By default, this creates a new class in `app/Shortcodes/YourShortcodeNameShortcode.php`.
222+
- If the file already exists, use the `--force` option to overwrite:
223+
224+
```bash
225+
php artisan make:shortcode YourShortcodeName --force
226+
```
227+
228+
### Customizing the Stub
229+
230+
You can publish the stub file to customize the generated class:
231+
232+
```bash
233+
php artisan vendor:publish --tag=shortcode-stubs
234+
```
235+
236+
This will copy the stub to `resources/stubs/shortcode.stub` in your Laravel app. Edit this file to change the template for new shortcode classes.
237+
238+
## Testing
239+
240+
To run the tests for the shortcode generator command:
241+
242+
```bash
243+
composer test
244+
```
245+
210246
## Change log
211247

212248
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
@@ -221,8 +257,8 @@ If you discover any security related issues, please email [email protected] inst
221257

222258
## Credits
223259

224-
- [Asif Iqbal][link-author]
225-
- [All Contributors][link-contributors]
260+
- [Asif Iqbal][link-author]
261+
- [All Contributors][link-contributors]
226262

227263
## Support me
228264

@@ -238,7 +274,6 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
238274
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/webwizo/laravel-shortcodes.svg?style=flat-square
239275
[ico-code-quality]: https://img.shields.io/scrutinizer/g/webwizo/laravel-shortcodes.svg?style=flat-square
240276
[ico-downloads]: https://img.shields.io/packagist/dt/webwizo/laravel-shortcodes.svg?style=flat-square
241-
242277
[link-packagist]: https://packagist.org/packages/webwizo/laravel-shortcodes
243278
[link-travis]: https://travis-ci.org/webwizo/laravel-shortcodes
244279
[link-scrutinizer]: https://scrutinizer-ci.com/g/webwizo/laravel-shortcodes/code-structure

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "webwizo/laravel-shortcodes",
33
"type": "library",
4-
"description": "Wordpress like shortcodes for Laravel 5, 6, 7, 8, 9, 10 and 11",
4+
"description": "Wordpress like shortcodes for Laravel 5, 6, 7, 8, 9, 10, 11 and 12",
55
"keywords": [
66
"laravel",
77
"wordpress",

resources/stubs/shortcode.stub

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Shortcodes;
4+
5+
class {{class}}
6+
{
7+
public function register($shortcode, $content, $compiler, $name, $viewData)
8+
{
9+
// Implement shortcode logic here
10+
return $content;
11+
}
12+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Webwizo\Shortcodes\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
8+
class MakeShortcodeCommand extends Command
9+
{
10+
protected $signature = 'make:shortcode {name} {--force}';
11+
protected $description = 'Create a new shortcode class';
12+
13+
protected $files;
14+
15+
public function __construct(Filesystem $files)
16+
{
17+
parent::__construct();
18+
$this->files = $files;
19+
}
20+
21+
public function handle()
22+
{
23+
$name = $this->argument('name');
24+
$className = ucfirst($name) . 'Shortcode';
25+
$path = $this->getShortcodePath($className);
26+
$force = $this->option('force');
27+
28+
if ($this->files->exists($path) && !$force) {
29+
$this->error("Shortcode class already exists: {$className}. Use --force to overwrite.");
30+
return 1;
31+
}
32+
33+
$stub = $this->getStub($className);
34+
$this->files->ensureDirectoryExists(dirname($path));
35+
$this->files->put($path, $stub);
36+
37+
$this->info("Shortcode class created: {$path}");
38+
return 0;
39+
}
40+
41+
protected function getShortcodePath($className)
42+
{
43+
// Use Laravel's app_path helper if available, fallback to relative path
44+
if (function_exists('app_path')) {
45+
return app_path("Shortcodes/{$className}.php");
46+
}
47+
return getcwd() . "/app/Shortcodes/{$className}.php";
48+
}
49+
50+
protected function getStub($className)
51+
{
52+
$stubPath = base_path('resources/stubs/shortcode.stub');
53+
if (!$this->files->exists($stubPath)) {
54+
// fallback to package stub if not published
55+
$stubPath = __DIR__ . '/../../resources/stubs/shortcode.stub';
56+
}
57+
$stub = $this->files->get($stubPath);
58+
return str_replace('{{class}}', $className, $stub);
59+
}
60+
}

src/ShortcodesServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ class ShortcodesServiceProvider extends ServiceProvider
1616
public function boot()
1717
{
1818
$this->enableCompiler();
19+
if ($this->app->runningInConsole()) {
20+
$this->commands([
21+
\Webwizo\Shortcodes\Console\MakeShortcodeCommand::class,
22+
]);
23+
// Publish stub file for customization
24+
$this->publishes([
25+
__DIR__ . '/../resources/stubs/shortcode.stub' => resource_path('stubs/shortcode.stub'),
26+
], 'shortcode-stubs');
27+
}
1928
}
2029

2130
/**

tests/MakeShortcodeCommandTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Webwizo\Shortcodes\Tests;
4+
5+
use Illuminate\Filesystem\Filesystem;
6+
use Webwizo\Shortcodes\Console\MakeShortcodeCommand;
7+
use Webwizo\Shortcodes\ShortcodesServiceProvider;
8+
use Orchestra\Testbench\TestCase;
9+
10+
class MakeShortcodeCommandTest extends TestCase
11+
{
12+
protected function getPackageProviders($app)
13+
{
14+
return [ShortcodesServiceProvider::class];
15+
}
16+
17+
public function test_it_creates_shortcode_class()
18+
{
19+
$filesystem = new Filesystem();
20+
$name = 'Demo';
21+
$className = 'DemoShortcode';
22+
$path = app_path("Shortcodes/{$className}.php");
23+
24+
// Remove file if it exists
25+
if ($filesystem->exists($path)) {
26+
$filesystem->delete($path);
27+
}
28+
29+
$this->artisan('make:shortcode', ['name' => $name])
30+
->expectsOutput("Shortcode class created: {$path}")
31+
->assertExitCode(0);
32+
33+
$this->assertTrue($filesystem->exists($path));
34+
$contents = $filesystem->get($path);
35+
$this->assertStringContainsString('class DemoShortcode', $contents);
36+
}
37+
38+
public function test_force_overwrites_existing_class()
39+
{
40+
$filesystem = new Filesystem();
41+
$name = 'Demo';
42+
$className = 'DemoShortcode';
43+
$path = app_path("Shortcodes/{$className}.php");
44+
45+
// Create file with dummy content
46+
$filesystem->ensureDirectoryExists(dirname($path));
47+
$filesystem->put($path, '<?php // dummy');
48+
49+
$this->artisan('make:shortcode', ['name' => $name, '--force' => true])
50+
->expectsOutput("Shortcode class created: {$path}")
51+
->assertExitCode(0);
52+
53+
$contents = $filesystem->get($path);
54+
$this->assertStringContainsString('class DemoShortcode', $contents);
55+
}
56+
}

0 commit comments

Comments
 (0)