Skip to content

Commit

Permalink
Added command to generate shortcode class, multiple other edtis
Browse files Browse the repository at this point in the history
  • Loading branch information
vedmant committed Apr 20, 2019
1 parent ee27bc1 commit b74a7a8
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 14 deletions.
61 changes: 59 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,31 @@ Via Composer
$ composer require vedmant/laravel-shortcodes
```


## Usage


### Shortcode class

Shortcode class should extend abstract \Vedmant\LaravelShortcodes\Shortcode class.

This packages adds `make:shortcode` artisan command:
```bash
php artisan make:shortcode PostsListShortcode
```
It will generate a shortcode class in the `app/Shortcodes` folder by default.


### Register shortcodes

You can use AppServiceProvider boot method to register all needed shortcodes.

Using shortcode class:
```php
Shortcodes::add('b', BShortcode::class);
```

Using shortcode classes in array:
Using shortcode classes in array, preferable for lots of shortcodes:
```php
Shortcodes::add([
'a' => AShortcode::class,
Expand All @@ -39,14 +54,53 @@ Shortcodes::add('test', function ($atts, $content, $tag, $manager) {
});
```

### Rendering shortcodes

By default this packages extends View to parse all shortcodes during views rendering.
This feature can be disabled in the config file.

Also to enable / disable rendering shortcodes for specific view you can use:

```php
view('some-view')->withShortcodes();
// Or
view('some-view')->withoutShortcodes();
```

To render shortcodes manually use:
```blade
{{ Shortcodes::render('[b]bold[/b]') }}
```


### Global attributes

You can set global attributes that will be available in each shortcode
```php
Shortcodes::global('post', $post);
```

Then you can get global attributes in the shortcode class:

```php
$post = $this->manager->global('post');
```


### Comma separated values (array attributes)

If you need to pass an array to a shortcode, you can pass values separated by comma:

```blade
[posts_list ids="1,2,3"]
```

Then in render function you can parse this attribute using build in method:
```php
$ids = $this->parseCommaSeparated($atts['ids']);
```


## Configuraton

Publish configuration.
Expand All @@ -56,21 +110,24 @@ php artisan vendor:publish --tag=shortcodes

Edit configuration file as needed.


## Testing

``` bash
$ composer test
```


## TODO

1. Add commands to generate a shortcode class
1. Add commands to generate a shortcode view, generate view by default with make:shortcode
1. Update readme
1. Create styles attributes trait
1. Integrate into debug bar
1. Fix styleci
1. Add unit tests
1. Integrate travis ci
1. Create performance profile tests, optimize performance

## Contributing

Expand Down
71 changes: 71 additions & 0 deletions src/Commands/MakeShortcodeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Vedmant\LaravelShortcodes\Commands;

use Illuminate\Console\GeneratorCommand;

class MakeShortcodeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:shortcode';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new shortcode class';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Shortcode';

/**
* Execute the console command.
*/
public function handle()
{
if (parent::handle() === false && ! $this->option('force')) {
return false;
}
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/../../stubs/shortcode.stub';
}

/**
* Determine if the class already exists.
*
* @param string $rawName
* @return bool
*/
protected function alreadyExists($rawName)
{
return class_exists($rawName);
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Shortcodes';
}
}
5 changes: 4 additions & 1 deletion src/LaravelShortcodesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Vedmant\LaravelShortcodes;

use Illuminate\Support\ServiceProvider;
use Vedmant\LaravelShortcodes\Commands\MakeShortcodeCommand;
use Vedmant\LaravelShortcodes\View\Factory;

class LaravelShortcodesServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -108,6 +109,8 @@ protected function bootForConsole()
], 'shortcodes.views');*/

// Registering package commands.
// $this->commands([]);
$this->commands([
MakeShortcodeCommand::class,
]);
}
}
1 change: 1 addition & 0 deletions src/Shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ abstract class Shortcode implements ShortcodeContract
/**
* AbstractShortcode constructor.
*
* @param Application $app
* @param ShortcodesManager $manager
*/
public function __construct(Application $app, ShortcodesManager $manager)
Expand Down
5 changes: 4 additions & 1 deletion src/ShortcodesRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@ private function doShortcodeTag($m)
/** @var Shortcode $shortcode */
if (is_callable($this->shortcodes[$tag])) {
return $m[1] . $this->shortcodes[$tag]($atts, isset($m[5]) ? $m[5] : null, $tag, $this->manager) . $m[6];
} else if (class_exists($this->shortcodes[$tag])) {
} else if (class_exists($this->shortcodes[$tag]) ) {
$shortcode = new $this->shortcodes[$tag]($this->app, $this->manager);
if (! $shortcode instanceof Shortcode) {
return "Class {$this->shortcodes[$tag]} is not an instance of abstract " . Shortcode::class;
}
// Fill all attributes and apply defaults
$atts = $this->shortcodeAtts($shortcode->attributes(), $atts);

Expand Down
5 changes: 0 additions & 5 deletions src/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

namespace Vedmant\LaravelShortcodes\View;

use Illuminate\Events\Dispatcher;
use Illuminate\View\ViewFinderInterface;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Factory as IlluminateViewFactory;
use Vedmant\LaravelShortcodes\ShortcodesManager;
use Vedmant\LaravelShortcodes\View\View;

class Factory extends IlluminateViewFactory
{
Expand Down
33 changes: 28 additions & 5 deletions src/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

namespace Vedmant\LaravelShortcodes\View;

use Illuminate\Contracts\Support\Renderable;
use Illuminate\Contracts\View\Engine;
use Illuminate\View\Factory;
use Illuminate\View\View as IlluminateView;
use Vedmant\LaravelShortcodes\ShortcodesManager;
use Webwizo\Shortcodes\Compilers\ShortcodeCompiler;
use Illuminate\Contracts\View\Engine as EngineInterface;

class View extends IlluminateView
{
/**
* @var ShortcodesManager Shortcode manager
*/
public $shortcodes;
private $shortcodes;

/**
* @var bool If should render shortcodes
*/
private $renderShortcodes = false;

/**
* Create a new view instance.
Expand All @@ -33,6 +35,27 @@ public function __construct(Factory $factory, Engine $engine, $view, $path, $dat
parent::__construct($factory, $engine, $view, $path, $data);

$this->shortcodes = $shortcodes;
$this->renderShortcodes = $this->shortcodes->config['render_views'];
}

/**
* Should render shortcodes
*/
public function withShortcodes()
{
$this->renderShortcodes = true;

return $this;
}

/**
* Should not render shortcodes
*/
public function withoutShortcodes()
{
$this->renderShortcodes = false;

return $this;
}

/**
Expand All @@ -54,7 +77,7 @@ protected function getContents()
{
$contents = $this->engine->get($this->path, $this->gatherData());

if ($this->shortcodes->config['render_views']) {
if ($this->renderShortcodes) {
return $this->shortcodes->render($contents);
}

Expand Down
41 changes: 41 additions & 0 deletions stubs/shortcode.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace DummyNamespace;

use Vedmant\LaravelShortcodes\Shortcode;

class DummyClass extends Shortcode
{
/**
* @var string Shortcode description
*/
public $description = 'Shortcode description';

/**
* Get attributes config
*
* @return mixed
*/
public function attributes()
{
return [
'example' => [
'default' => 'default',
'description' => 'Example attribute',
'sample' => 'some-sample-value',
],
];
}

/**
* Render shortcode
*
* @param array $atts
* @param string $content
* @return string
*/
public function render(array $atts, $content, $tag)
{
return $this->view('shortcodes.some-shortcode', compact('atts', 'content'));
}
}

0 comments on commit b74a7a8

Please sign in to comment.