Skip to content

Commit

Permalink
Merge pull request #19 from bedita/feat/3.0.0
Browse files Browse the repository at this point in the history
Refactor structure using Plugin object and add new TwigView
  • Loading branch information
stefanorosanelli authored May 6, 2020
2 parents 53d2c5c + 3d1238c commit 4ffa37f
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 222 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,50 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
return $service;
}
```

## Load assets with AssetRevisions

`AssetRevisions` with the help of an asset strategy can easily resolve the common issue
of loading built versioned assets as `js` and `css`.

Through `\BEdita\WebTools\View\Helper\HtmlHelper` you can transparently link built assets placed in a custom folder or raw assets living in `webroot/js` or `webroot/css`.

### Define which strategy to use

The best place to define which strategy your app will use is the `Application::bootstrap()`

```php
use BEdita\WebTools\Utility\AssetRevisions;
use BEdita\WebTools\Utility\Asset\Strategy\EntrypointsStrategy;

public function bootstrap(): void
{
parent::bootstrap();

AssetsRevisions::setStrategy(new EntrypointsStrategy());

// you can also set the path where to find the manifest (default is webroot/build/entrypoints.json)
// AssetsRevisions::setStrategy(
// new EntrypointsStrategy(['manifestPath' => '/custom/path/entrypoints.json']);
// );
}
```

There are two assets strategies out of the box:

* `EntrypointsStrategy` based on the `entrypoints.json` file generated by [Webpack Encore](https://github.com/symfony/webpack-encore)
* `RevManifestStrategy` based on `rev-manifest.json` file generated by [gulp-rev](https://github.com/sindresorhus/gulp-rev)

Anyway you are free to define your own strategy implementing `AssetStrategyInterface`.

### Use HtmlHelper to load assets

Once a strategy is set you can link assets using `\BEdita\WebTools\View\Helper\HtmlHelper` and its methods `script()`, `css()` and `assets()`, for example:

```php
<?= $this->Html->script('app') ?>
```

The javascript `app` asset will be searched first from your asset strategy falling back to CakePHP `HtmlHelper` if strategy doesn't resolve the asset.

In this way you can continue to load assets as it was placed in common `webroot/js` or `webroot/css` and delegate to `\BEdita\WebTools\View\Helper\HtmlHelper` the task of resolve the link to them.
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
"bedita/php-sdk": "^1.0.3",
"cakephp/cakephp": "4.0.*",
"josegonzalez/dotenv": "^3.2",
"wyrihaximus/twig-view": "^5.0",
"cakephp/authentication": "^2.1.0"
"cakephp/authentication": "^2.1.0",
"cakephp/twig-view": "^1.0.1"
},
"require-dev": {
"cakephp/bake": "^2.0.3",
"cakephp/cakephp-codesniffer": "~4.0.0",
"cakephp/debug_kit": "^4.0",
"phpunit/phpunit": "^8.0"
},
"autoload": {
Expand Down
110 changes: 0 additions & 110 deletions src/BaseApplication.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Command/CacheClearallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CacheClearallCommand extends BaseCommand
*/
public function execute(Arguments $args, ConsoleIo $io): ?int
{
$twigCachePath = CACHE . 'twigView';
$twigCachePath = CACHE . 'twig_view';
$folder = new Folder($twigCachePath);
if (file_exists($twigCachePath) && !$folder->delete()) {
$io->error("Error removing Twig cache files in {$twigCachePath}");
Expand Down
60 changes: 60 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2020 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
*/
namespace BEdita\WebTools;

use BEdita\WebTools\Command\CacheClearallCommand;
use Cake\Console\CommandCollection;
use Cake\Core\BasePlugin;
use Cake\Core\PluginApplicationInterface;

/**
* Plugin class for BEdita\WebTools.
*/
class Plugin extends BasePlugin
{
/**
* Load routes or not
*
* @var bool
*/
protected $routesEnabled = false;

/**
* Use `cache clear_all` from BEdita\WebTools\Command\CacheClearallCommand
*
* @param \Cake\Console\CommandCollection $commands Console commands.
* @return \Cake\Console\CommandCollection
*/
public function console(CommandCollection $commands): CommandCollection
{
parent::console($commands);
$commands->remove('cache clear_all');
$commands->add('cache clear_all', CacheClearallCommand::class);

return $commands;
}

/**
* {@inheritDoc}
*/
public function bootstrap(PluginApplicationInterface $app): void
{
// Call parent to load bootstrap from files.
parent::bootstrap($app);

// Load more plugins here
$app->addPlugin('Cake/TwigView');
}
}
6 changes: 3 additions & 3 deletions src/View/TwigView.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace BEdita\WebTools\View;

use BEdita\WebTools\View\Twig\BeditaTwigExtension;
use WyriHaximus\TwigView\View\TwigView as BaseTwigView;
use Cake\TwigView\View\TwigView as BaseTwigView;

/**
* View class that uses TwigView and adds Twig extensions
Expand All @@ -25,9 +25,9 @@ class TwigView extends BaseTwigView
/**
* {@inheritDoc}
*/
public function initialize(): void
public function initializeExtensions(): void
{
parent::initialize();
parent::initializeExtensions();

$this->getTwig()
->addExtension(new BeditaTwigExtension());
Expand Down
73 changes: 0 additions & 73 deletions tests/TestCase/BaseApplicationTest.php

This file was deleted.

45 changes: 45 additions & 0 deletions tests/TestCase/PluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2018 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
*/
namespace BEdita\WebTools\Test\TestCase;

use BEdita\WebTools\Command\CacheClearallCommand;
use Cake\Console\CommandCollection;
use Cake\TestSuite\TestCase;
use TestApp\Application;

/**
* {@see BEdita\WebTools\Plugin} Test Case
*
* @coversDefaultClass \BEdita\WebTools\Plugin
*/
class PluginTest extends TestCase
{
/**
* Test `console` method
*
* @return void
*
* @covers ::console
*/
public function testConsole(): void
{
$app = new Application(CONFIG);
$commands = $app->console(new CommandCollection([]));
$commands = $app->pluginConsole($commands);
$cacheClearAll = $commands->get('cache clear_all');
static::assertNotEmpty($cacheClearAll);
static::assertEquals(CacheClearallCommand::class, $cacheClearAll);
}
}
Loading

0 comments on commit 4ffa37f

Please sign in to comment.