Skip to content

Commit

Permalink
Update Nette (#3)
Browse files Browse the repository at this point in the history
- upgrade to Nette 3.x
  • Loading branch information
Tomáš Kulhánek committed Oct 27, 2020
1 parent ff76c65 commit ebb65d9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 76 deletions.
12 changes: 6 additions & 6 deletions composer.json
Expand Up @@ -11,17 +11,17 @@
],
"require": {
"php": "^7.2",
"nette/di": "^2.4.16",
"nette/utils": "^2.4.2",
"nette/caching": "^2.5",
"latte/latte": "^2.4",
"nette/di": "^3.0.3",
"nette/utils": "^3.0",
"nette/caching": "^3.0",
"latte/latte": "^2.5",
"symfony/asset": "^4.2 | ^5.0"
},
"require-dev": {
"nette/tester": "^2.2",
"nette/application": "^2.4",
"nette/application": "^3.0",
"friendsofphp/php-cs-fixer": "^2.15",
"68publishers/asset": "^1.0",
"68publishers/asset": "^2.0",
"mockery/mockery": "^1.2"
},
"suggest": {
Expand Down
124 changes: 54 additions & 70 deletions src/DI/WebpackEncoreBundleExtension.php
Expand Up @@ -7,6 +7,7 @@
use Latte;
use Nette;
use Symfony;
use RuntimeException;
use SixtyEightPublishers;

final class WebpackEncoreBundleExtension extends Nette\DI\CompilerExtension
Expand All @@ -17,52 +18,66 @@ final class WebpackEncoreBundleExtension extends Nette\DI\CompilerExtension

private const CROSSORIGIN_ALLOWED_VALUES = [NULL, 'anonymous', 'use-credentials'];

/** @var array */
private $defaults = [
'output_path' => NULL, # The path where Encore is building the assets - i.e. Encore.setOutputPath()
'builds' => [],
'crossorigin' => NULL, # crossorigin value when Encore.enableIntegrityHashes() is used, can be NULL (default), anonymous or use-credentials
'cache' => [
'enabled' => FALSE,
'storage' => '@' . Nette\Caching\IStorage::class,
],
'latte' => [
'js_assets_macro_name' => 'encore_js',
'css_assets_macro_name' => 'encore_css',
],
];
/**
* {@inheritdoc}
*/
public function getConfigSchema(): Nette\Schema\Schema
{
return Nette\Schema\Expect::structure([
'output_path' => Nette\Schema\Expect::string()->nullable(), # The path where Encore is building the assets - i.e. Encore.setOutputPath()
'builds' => Nette\Schema\Expect::array()->items('string')->assert(function (array $value): bool {
if (isset($value[self::ENTRYPOINT_DEFAULT_NAME])) {
throw new Nette\Utils\AssertionException(sprintf('Key "%s" can\'t be used as build name.', self::ENTRYPOINT_DEFAULT_NAME));
}

return TRUE;
}),
'crossorigin' => Nette\Schema\Expect::string()->nullable()->assert(function (?string $value): bool {
if (!in_array($value, self::CROSSORIGIN_ALLOWED_VALUES, TRUE)) {
throw new Nette\Utils\AssertionException(sprintf('Value "%s" for setting "crossorigin" is not allowed', $value));
}

return TRUE;
}), # crossorigin value when Encore.enableIntegrityHashes() is used, can be NULL (default), anonymous or use-credentials
'cache' => Nette\Schema\Expect::structure([
'enabled' => Nette\Schema\Expect::bool(FALSE),
'storage' => Nette\Schema\Expect::string('@' . Nette\Caching\IStorage::class)->dynamic(),
]),
'latte' => Nette\Schema\Expect::structure([
'js_assets_macro_name' => Nette\Schema\Expect::string('encore_js'),
'css_assets_macro_name' => Nette\Schema\Expect::string('encore_css'),
]),
]);
}

/**
* {@inheritdoc}
*
* @throws \Nette\Utils\AssertionException
*/
public function loadConfiguration(): void
{
$config = $this->getValidConfig();
$builder = $this->getContainerBuilder();
$cache = $this->registerCache($config['cache']['enabled'], $config['cache']['storage']);
$cache = $this->registerCache($this->getConfig()->cache->enabled, $this->getConfig()->cache->storage);
$lookups = [];

if (NULL !== $config['output_path']) {
$lookups[] = $this->createEntryPointLookupStatement(self::ENTRYPOINT_DEFAULT_NAME, $config['output_path'], $cache);
if (NULL !== $this->getConfig()->output_path) {
$lookups[] = $this->createEntryPointLookupStatement(self::ENTRYPOINT_DEFAULT_NAME, $this->getConfig()->output_path, $cache);
}

foreach ($config['builds'] as $name => $path) {
foreach ($this->getConfig()->builds as $name => $path) {
$lookups[] = $this->createEntryPointLookupStatement($name, $path, $cache);
}

$builder->addDefinition($this->prefix('entryPointLookupProvider'))
->setType(SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IEntryPointLookupProvider::class)
->setFactory(SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\EntryPointLookupProvider::class, [
'lookups' => $lookups,
'defaultName' => NULL !== $config['output_path'] ? self::ENTRYPOINT_DEFAULT_NAME : NULL,
'defaultName' => NULL !== $this->getConfig()->output_path ? self::ENTRYPOINT_DEFAULT_NAME : NULL,
]);

$defaultAttributes = [];

if (NULL !== $config['crossorigin']) {
$defaultAttributes['crossorigin'] = $config['crossorigin'];
if (NULL !== $this->getConfig()->crossorigin) {
$defaultAttributes['crossorigin'] = $this->getConfig()->crossorigin;
}

$builder->addDefinition($this->prefix('tagRenderer'))
Expand All @@ -75,43 +90,40 @@ public function loadConfiguration(): void

/**
* {@inheritdoc}
*
* @throws \Nette\Utils\AssertionException
*/
public function beforeCompile(): void
{
$config = $this->getValidConfig();
$builder = $this->getContainerBuilder();

if (NULL === $builder->getByType(Symfony\Component\Asset\Packages::class, FALSE)) {
throw new \RuntimeException('Missing service of type Symfony\Component\Asset\Packages that is required by this package. You can configure and register it manually or you can use package 68publishers/asset (recommended way).');
throw new RuntimeException('Missing service of type Symfony\Component\Asset\Packages that is required by this package. You can configure and register it manually or you can use package 68publishers/asset (recommended way).');
}

$latteFactory = $builder->getDefinition($builder->getByType(Latte\Engine::class) ?? 'nette.latteFactory');

$latteFactory->addSetup('addProvider', [
$latteFactory->getResultDefinition()->addSetup('addProvider', [
'name' => 'webpackEncoreTagRenderer',
'value' => $this->prefix('@tagRenderer'),
]);

$latteFactory->addSetup('?->onCompile[] = function ($engine) { ?::install(?, ?, $engine->getCompiler()); }', [
$latteFactory->getResultDefinition()->addSetup('?->onCompile[] = function ($engine) { ?::install(?, ?, $engine->getCompiler()); }', [
'@self',
new Nette\PhpGenerator\PhpLiteral(SixtyEightPublishers\WebpackEncoreBundle\Latte\WebpackEncoreMacros::class),
$config['latte']['js_assets_macro_name'],
$config['latte']['css_assets_macro_name'],
$this->getConfig()->latte->js_assets_macro_name,
$this->getConfig()->latte->css_assets_macro_name,
]);
}

/**
* @param string $name
* @param string $path
* @param \Nette\DI\ServiceDefinition|NULL $cache
* @param string $name
* @param string $path
* @param Nette\DI\Definitions\ServiceDefinition|NULL $cache
*
* @return \Nette\DI\Statement
* @return Nette\DI\Definitions\Statement
*/
private function createEntryPointLookupStatement(string $name, string $path, ?Nette\DI\ServiceDefinition $cache): Nette\DI\Statement
private function createEntryPointLookupStatement(string $name, string $path, ?Nette\DI\Definitions\ServiceDefinition $cache): Nette\DI\Definitions\Statement
{
return new Nette\DI\Statement(SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\EntryPointLookup::class, [
return new Nette\DI\Definitions\Statement(SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\EntryPointLookup::class, [
'buildName' => $name,
'entryPointJsonPath' => $path . '/' . self::ENTRYPOINTS_FILE_NAME,
'cache' => $cache,
Expand All @@ -122,9 +134,9 @@ private function createEntryPointLookupStatement(string $name, string $path, ?Ne
* @param bool $enabled
* @param mixed $storage
*
* @return \Nette\DI\ServiceDefinition|NULL
* @return Nette\DI\Definitions\ServiceDefinition|NULL
*/
private function registerCache(bool $enabled, $storage): ?Nette\DI\ServiceDefinition
private function registerCache(bool $enabled, $storage): ?Nette\DI\Definitions\ServiceDefinition
{
if (FALSE === $enabled) {
return NULL;
Expand All @@ -136,7 +148,7 @@ private function registerCache(bool $enabled, $storage): ?Nette\DI\ServiceDefini
$storage = $builder->addDefinition($this->prefix('cache.storage'))
->setType(Nette\Caching\IStorage::class)
->setFactory($storage)
->setInject(FALSE);
->addTag(Nette\DI\Extensions\InjectExtension::TAG_INJECT);
}

return $builder->addDefinition($this->prefix('cache.cache'))
Expand All @@ -145,34 +157,6 @@ private function registerCache(bool $enabled, $storage): ?Nette\DI\ServiceDefini
'storage' => $storage,
'namespace' => str_replace('\\', '.', SixtyEightPublishers\WebpackEncoreBundle\EntryPoint\IEntryPointLookup::class),
])
->setInject(FALSE);
}

/**
* @return array
* @throws \Nette\Utils\AssertionException
*/
private function getValidConfig(): array
{
/** @noinspection PhpInternalEntityUsedInspection */
$config = $this->validateConfig(Nette\DI\Helpers::expand($this->defaults, $this->getContainerBuilder()->parameters));

Nette\Utils\Validators::assertField($config, 'output_path', 'null|string');
Nette\Utils\Validators::assertField($config['cache'], 'enabled', 'bool');
Nette\Utils\Validators::assertField($config['cache'], 'storage', 'string|' . Nette\DI\Statement::class);
Nette\Utils\Validators::assertField($config, 'builds', 'string[]');
Nette\Utils\Validators::assertField($config['latte'], 'js_assets_macro_name', 'string');
Nette\Utils\Validators::assertField($config['latte'], 'css_assets_macro_name', 'string');
Nette\Utils\Validators::assertField($config, 'crossorigin', 'null|string');

if (isset($config['builds'][self::ENTRYPOINT_DEFAULT_NAME])) {
throw new Nette\Utils\AssertionException(sprintf('Key "%s" can\'t be used as build name.', self::ENTRYPOINT_DEFAULT_NAME));
}

if (!in_array($config['crossorigin'], self::CROSSORIGIN_ALLOWED_VALUES, TRUE)) {
throw new Nette\Utils\AssertionException(sprintf('Value "%s" for setting "crossorigin" is not allowed', $config['crossorigin']));
}

return $config;
->addTag(Nette\DI\Extensions\InjectExtension::TAG_INJECT);
}
}

0 comments on commit ebb65d9

Please sign in to comment.