Skip to content

Commit

Permalink
Fix #4, fix #6: Make objects immutable, add AssetManager::getAssetUrl…
Browse files Browse the repository at this point in the history
…() method (#67)

Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
  • Loading branch information
devanych and samdark committed Apr 20, 2021
1 parent 5061528 commit 7c24f3d
Show file tree
Hide file tree
Showing 21 changed files with 673 additions and 491 deletions.
6 changes: 2 additions & 4 deletions config/params.php
Expand Up @@ -5,10 +5,8 @@
return [
'yiisoft/assets' => [
'assetConverter' => [
'command' => [
'from' => 'scss',
'to' => 'css',
'command' => '@npm/.bin/sass {options} {from} {to}',
'commands' => [
'scss' => ['css', '@npm/.bin/sass {options} {from} {to}'],
],
'forceConvert' => false,
],
Expand Down
54 changes: 33 additions & 21 deletions config/web.php
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Assets\AssetConverter;
use Yiisoft\Assets\AssetConverterInterface;
Expand All @@ -17,38 +19,48 @@
return [
AssetConverterInterface::class => [
'class' => AssetConverter::class,
'setCommand()' => [
$params['yiisoft/assets']['assetConverter']['command']['from'],
$params['yiisoft/assets']['assetConverter']['command']['to'],
$params['yiisoft/assets']['assetConverter']['command']['command'],
'__construct()' => [
Reference::to(Aliases::class),
Reference::to(LoggerInterface::class),
$params['yiisoft/assets']['assetConverter']['commands'],
$params['yiisoft/assets']['assetConverter']['forceConvert'],
],
'setForceConvert()' => [$params['yiisoft/assets']['assetConverter']['forceConvert']],
],

AssetLoaderInterface::class => [
'class' => AssetLoader::class,
'setAppendTimestamp()' => [$params['yiisoft/assets']['assetLoader']['appendTimestamp']],
'setAssetMap()' => [$params['yiisoft/assets']['assetLoader']['assetMap']],
'setBasePath()' => [$params['yiisoft/assets']['assetLoader']['basePath']],
'setBaseUrl()' => [$params['yiisoft/assets']['assetLoader']['baseUrl']],
'__construct()' => [
Reference::to(Aliases::class),
$params['yiisoft/assets']['assetLoader']['appendTimestamp'],
$params['yiisoft/assets']['assetLoader']['assetMap'],
$params['yiisoft/assets']['assetLoader']['basePath'],
$params['yiisoft/assets']['assetLoader']['baseUrl'],
],
],

AssetPublisherInterface::class => [
'class' => AssetPublisher::class,
'setForceCopy()' => [$params['yiisoft/assets']['assetPublisher']['forceCopy']],
'setLinkAssets()' => [$params['yiisoft/assets']['assetPublisher']['linkAssets']],
],

AssetManager::class => [
'class' => AssetManager::class,
'__construct()' => [
Reference::to(Aliases::class),
Reference::to(AssetLoaderInterface::class),
$params['yiisoft/assets']['assetManager']['allowedBundleNames'],
$params['yiisoft/assets']['assetManager']['customizedBundles'],
$params['yiisoft/assets']['assetPublisher']['forceCopy'],
$params['yiisoft/assets']['assetPublisher']['linkAssets'],
],
'setPublisher()' => [Reference::to(AssetPublisherInterface::class)],
'setConverter()' => [Reference::to(AssetConverterInterface::class)],
'register()' => [$params['yiisoft/assets']['assetManager']['register']],
],

AssetManager::class => static function (ContainerInterface $container) use ($params): AssetManager {
$assetManager = new AssetManager(
$container->get(Aliases::class),
$container->get(AssetLoaderInterface::class),
$params['yiisoft/assets']['assetManager']['allowedBundleNames'],
$params['yiisoft/assets']['assetManager']['customizedBundles'],
);

$assetManager = $assetManager
->withConverter($container->get(AssetConverterInterface::class))
->withPublisher($container->get(AssetPublisherInterface::class))
;

$assetManager->register($params['yiisoft/assets']['assetManager']['register']);
return $assetManager;
},
];
4 changes: 2 additions & 2 deletions docs/asset-bundles.md
Expand Up @@ -248,8 +248,8 @@ resolving files stays in the layout.

### Override file paths for export

By default, all CSS and JavaScript file paths are exported from the asset bundle, but you can specify the list of exported
file paths explicitly in the `$export` property.
By default, all CSS and JavaScript file paths are exported from the asset bundle, but
you can specify the list of exported file paths explicitly in the `$export` property.

```php
namespace App\Assets;
Expand Down
23 changes: 15 additions & 8 deletions docs/asset-converter.md
Expand Up @@ -64,7 +64,16 @@ to CSS. By default, asset converter has command definitions for less, scss, sass
are meant to be installed globally, and we have it as local dependency, we need to redefine a command:

```php
$assetManager->getConverter()->setCommand('scss', ['css', '@npm/.bin/sass {options} {from} {to}']);
/**
* @var \Psr\Log\LoggerInterface $logger
* @var \Yiisoft\Aliases\Aliases $aliases
* @var \Yiisoft\Assets\AssetManager $assetManager
*/

$converter = new \Yiisoft\Assets\AssetConverter($aliases, $logger, [
'scss' => ['css', '@npm/.bin/sass {options} {from} {to}'],
]);
$assetManager = $assetManager->withConverter($converter);
```

or, if done via `yiisoft/di` container:
Expand All @@ -73,9 +82,9 @@ or, if done via `yiisoft/di` container:
AssetConverterInterface::class => static function (\Psr\Container\ContainerInterface $container) {
$aliases = $container->get(\Yiisoft\Aliases\Aliases::class);
$logger = $container->get(\Psr\Log\LoggerInterface::class);
$converter = new \Yiisoft\Assets\AssetConverter($aliases, $logger);
$converter->setCommand('scss', 'css', '@npm/.bin/sass {options} {from} {to}');
return $converter;
return new \Yiisoft\Assets\AssetConverter($aliases, $logger, [
'scss' => ['css', '@npm/.bin/sass {options} {from} {to}'],
]);
}
```

Expand All @@ -84,10 +93,8 @@ or, if done via params.php:
```php
'yiisoft/assets' => [
'assetConverter' => [
'command' => [
'from' => 'scss',
'to' => 'css',
'command' => '@npm/.bin/sass {options} {from} {to}',
'commands' => [
'scss' => ['css', '@npm/.bin/sass {options} {from} {to}'],
],
'forceConvert' => false,
],
Expand Down
78 changes: 28 additions & 50 deletions docs/asset-manager.md
Expand Up @@ -41,12 +41,12 @@ return [
/**
* Example settings options AssetLoader:
*
* $loader->setAppendTimestamp(true);
* $loader->setAssetMap(['jquery.js' => 'https://code.jquery.com/jquery-3.4.1.js']);
* $loader->setBasePath('@assets');
* $loader->setBaseUrl('@assetsUrl');
* $loader->setCssDefaultOptions(['media' => 'screen', 'hreflang' => 'en');
* $loader->setJsDefaultOptions(['async' => true, 'defer' => true);
* $loader = $loader->withAppendTimestamp(true);
* $loader = $loader->withAssetMap(['jquery.js' => 'https://code.jquery.com/jquery-3.4.1.js']);
* $loader = $loader->withBasePath('@assets');
* $loader = $loader->withBaseUrl('@assetsUrl');
* $loader = $loader->withCssDefaultOptions(['media' => 'screen', 'hreflang' => 'en');
* $loader = $loader->withJsDefaultOptions(['async' => true, 'defer' => true);
*/

return $loader;
Expand All @@ -58,11 +58,11 @@ return [
/**
* Example settings options AssetPublisher:
*
* $publisher->setDirMode(0775);
* $publisher->setFileMode(0755);
* $publisher->setForceCopy(true);
* $publisher->setHashCallback(static fn () => 'hash');
* $publisher->setLinkAssets(true);
* $publisher = $publisher->withDirMode(0775);
* $publisher = $publisher->withFileMode(0755);
* $publisher = $publisher->withForceCopy(true);
* $publisher = $publisher->withHashCallback(static fn () => 'hash');
* $publisher = $publisher->withLinkAssets(true);
*/

return $publisher;
Expand All @@ -73,24 +73,9 @@ return [
$container->get(Aliases::class),
$container->get(AssetLoaderInterface::class),
);

/**
* Setting AsssetConverter::class in view/layout use $assetManager->getConverter()
*
* In view/layout command example:
*
* $assetManager->getConverter()->setCommand('php', 'txt', 'php {from} > {to}');
*/
$assetManager->setConverter($container->get(AssetConverterInterface::class));

/**
* Setting AsssetPublisher::class in view/layout use $assetManager->getPublisher()
*
* In view/layout command example:
*
* $assetManager->getPublisher()->setForceCopy(true);
*/
$assetManager->setPublisher($container->get(AssetPublisherInterface::class));

$assetManager = $assetManager->withConverter($container->get(AssetConverterInterface::class));
$assetManager = $assetManager->withPublisher($container->get(AssetPublisherInterface::class));

return $assetManager;
},
Expand All @@ -115,10 +100,10 @@ $loader = new AssetLoader($aliases);
$publisher = new AssetPublisher($aliases);


$assetManager = new AssetManager($aliases, $loader);

$assetManager->setConverter($converter);
$assetManager->setPublisher($publisher);
$assetManager = (new AssetManager($aliases, $loader))
->withConverter($converter)
->withPublisher($publisher)
;
```

### Specifying additional settings
Expand Down Expand Up @@ -214,9 +199,9 @@ This mode is used by default in the [yiisoft/app](https://github.com/yiisoft/app
* @var \Yiisoft\Assets\AssetPublisherInterface $publisher
*/

$assetManager = new \Yiisoft\Assets\AssetManager($aliases, $loader);

$assetManager->setPublisher($publisher);
$assetManager = (new \Yiisoft\Assets\AssetManager($aliases, $loader))
->withPublisher($publisher)
;

$assetManager->register([
\App\Assets\BootstrapAsset::class,
Expand Down Expand Up @@ -249,19 +234,15 @@ class PublishCommand extends Command
protected static $defaultName = 'assets/publish';

private AssetManager $assetManager;
private Aliases $aliases;

public function __construct(AssetManager $assetManager, Aliases $aliases)
{
$this->assetManager = $assetManager;
$this->aliases = $aliases;
$this->assetManager = $assetManager->withPublisher(new AssetPublisher($aliases));
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->assetManager->setPublisher(new AssetPublisher($this->aliases));

{
$this->assetManager->register([/* asset bundle names */]);
// To register all bundles if the allowed asset bundle names are used.
//$this->assetManager->registerAllAllowed();
Expand Down Expand Up @@ -292,21 +273,18 @@ class PublishCommand extends Command
protected static $defaultName = 'assets/publish';

private AssetManager $assetManager;
private Aliases $aliases;

public function __construct(AssetManager $assetManager, Aliases $aliases)
{
$this->assetManager = $assetManager;
$this->aliases = $aliases;
$publisher = (new AssetPublisher($aliases))
->withHashCallback(static fn (string $path): string => hash('md4', $path))
;
$this->assetManager = $assetManager->withPublisher($publisher);
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$publisher = new AssetPublisher($this->aliases);
$publisher->setHashCallback(static fn (string $path):string => hash('md4', $path));
$this->assetManager->setPublisher(new AssetPublisher($this->aliases));

{
$this->assetManager->register([/* asset bundle names */]);
// To register all bundles if the allowed asset bundle names are used.
//$this->assetManager->registerAllAllowed();
Expand Down
4 changes: 2 additions & 2 deletions src/AssetBundle.php
Expand Up @@ -154,8 +154,8 @@ class AssetBundle
* A source asset file is a file that is part of your source code repository of your Web application.
* You must set this property if the directory containing the source asset files is not Web accessible.
*
* If a publisher is set via {@see AssetManager::setPublisher()}, {@see AssetManager} will publish
* the source asset files to a Web-accessible directory automatically when the asset bundle is registered on a page.
* If a publisher is set via {@see AssetManager::withPublisher()}, {@see AssetManager} will publish the source
* asset files to a Web-accessible directory automatically when the asset bundle is registered on a page.
*
* If you do not set this property, it means the source asset files are located under {@see $basePath}.
*
Expand Down

0 comments on commit 7c24f3d

Please sign in to comment.