Skip to content

Commit

Permalink
Nette 3.0 support, composer updates, fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gappa committed May 3, 2019
1 parent 35d436e commit 9426ee2
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 96 deletions.
143 changes: 82 additions & 61 deletions WebLoader/Nette/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,88 @@
use Nette;
use Nette\Configurator;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\DI\Config\Helpers;
use Nette\DI\ContainerBuilder;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nette\Utils\Finder;

/**
* @author Jan Marek
*/
class Extension extends \Nette\DI\CompilerExtension
class Extension extends CompilerExtension
{
/** @var string */
public const DEFAULT_TEMP_PATH = 'webtemp';

/** @var string */
public const EXTENSION_NAME = 'webloader';

/** @var bool */
private $debugMode;

/** @var string */
private $wwwDir;


public function __construct(string $wwwDir, bool $debugMode = false)
{
$this->wwwDir = $wwwDir;
$this->debugMode = $debugMode;
}


public function getDefaultConfig()
public function getConfigSchema(): Schema
{
return [
'jsDefaults' => [
'checkLastModified' => true,
'debug' => false,
'sourceDir' => '%wwwDir%/js',
'tempDir' => '%wwwDir%/' . self::DEFAULT_TEMP_PATH,
'tempPath' => self::DEFAULT_TEMP_PATH,
'files' => [],
'watchFiles' => [],
'remoteFiles' => [],
'filters' => [],
'fileFilters' => [],
'joinFiles' => true,
'async' => false,
'defer' => false,
'nonce' => null,
'absoluteUrl' => false,
'namingConvention' => '@' . $this->prefix('jsNamingConvention'),
],
'cssDefaults' => [
'checkLastModified' => true,
'debug' => false,
'sourceDir' => '%wwwDir%/css',
'tempDir' => '%wwwDir%/' . self::DEFAULT_TEMP_PATH,
'tempPath' => self::DEFAULT_TEMP_PATH,
'files' => [],
'watchFiles' => [],
'remoteFiles' => [],
'filters' => [],
'fileFilters' => [],
'joinFiles' => true,
'async' => false,
'defer' => false,
'nonce' => null,
'absoluteUrl' => false,
'namingConvention' => '@' . $this->prefix('cssNamingConvention'),
],
'js' => [],
'css' => [],
'debugger' => '%debugMode%',
];
return Expect::structure([
'jsDefaults' => Expect::structure([
'checkLastModified' => Expect::bool(true),
'debug' => Expect::bool(false),
'sourceDir' => Expect::string($this->wwwDir . '/js'),
'tempDir' => Expect::string($this->wwwDir . '/' . self::DEFAULT_TEMP_PATH),
'tempPath' => Expect::string(self::DEFAULT_TEMP_PATH),
'files' => Expect::array(),
'watchFiles' => Expect::array(),
'remoteFiles' => Expect::array(),
'filters' => Expect::array(),
'fileFilters' => Expect::array(),
'joinFiles' => Expect::bool(true),
'async' => Expect::bool(false),
'defer' => Expect::bool(false),
'nonce' => Expect::string()->nullable(),
'absoluteUrl' => Expect::bool(false),
'namingConvention' => Expect::string('@' . $this->prefix('jsNamingConvention')),
]),
'cssDefaults' => Expect::structure([
'checkLastModified' => Expect::bool(true),
'debug' => Expect::bool(false),
'sourceDir' => Expect::string($this->wwwDir . '/css')->dynamic(),
'tempDir' => Expect::string($this->wwwDir . '/' . self::DEFAULT_TEMP_PATH),
'tempPath' => Expect::string(self::DEFAULT_TEMP_PATH),
'files' => Expect::array(),
'watchFiles' => Expect::array(),
'remoteFiles' => Expect::array(),
'filters' => Expect::array(),
'fileFilters' => Expect::array(),
'joinFiles' => Expect::bool(true),
'async' => Expect::bool(false),
'defer' => Expect::bool(false),
'nonce' => Expect::string()->nullable(),
'absoluteUrl' => Expect::bool(false),
'namingConvention' => Expect::string('@' . $this->prefix('cssNamingConvention')),
]),
'js' => Expect::array(),
'css' => Expect::array(),
'debugger' => Expect::bool($this->debugMode),
]);
}


public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$config = $this->getConfig($this->getDefaultConfig());
$config = json_decode(json_encode($this->getConfig()), true);

$builder->addDefinition($this->prefix('cssNamingConvention'))
->setFactory('WebLoader\DefaultOutputNamingConvention::createCssConvention');
Expand All @@ -79,8 +98,8 @@ public function loadConfiguration(): void

if ($config['debugger']) {
$builder->addDefinition($this->prefix('tracyPanel'))
->setClass('WebLoader\Nette\Diagnostics\Panel')
->setArguments([$builder->expand('%appDir%')]);
->setClass('WebLoader\Nette\Diagnostics\Panel');
// ->setArguments([$builder->expand('%appDir%')]);
}

$builder->parameters['webloader'] = $config;
Expand All @@ -94,13 +113,13 @@ public function loadConfiguration(): void
$loaderFactoryTempPaths[strtolower($name)] = $wlConfig['tempPath'];

if (!is_dir($wlConfig['tempDir']) || !is_writable($wlConfig['tempDir'])) {
throw new \WebLoader\Nette\CompilationException(sprintf("You must create a writable directory '%s'", $wlConfig['tempDir']));
throw new CompilationException(sprintf("You must create a writable directory '%s'", $wlConfig['tempDir']));
}
}
}

$builder->addDefinition($this->prefix('factory'))
->setClass('WebLoader\Nette\LoaderFactory', [$loaderFactoryTempPaths, $this->name]);
->setFactory('WebLoader\Nette\LoaderFactory', [$loaderFactoryTempPaths, $this->name]);

if (class_exists('Symfony\Component\Console\Command\Command')) {
$builder->addDefinition($this->prefix('generateCommand'))
Expand Down Expand Up @@ -168,23 +187,25 @@ private function addWebLoader(ContainerBuilder $builder, string $name, array $co
}


public function afterCompile(Nette\PhpGenerator\ClassType $class): void
{
$meta = $class->getProperty('meta');
if (array_key_exists('webloader\\nette\\loaderfactory', $meta->value['types'])) {
$meta->value['types']['webloader\\loaderfactory'] = $meta->value['types']['webloader\\nette\\loaderfactory'];
}
if (array_key_exists('WebLoader\\Nette\\LoaderFactory', $meta->value['types'])) {
$meta->value['types']['WebLoader\\LoaderFactory'] = $meta->value['types']['WebLoader\\Nette\\LoaderFactory'];
}

$init = $class->methods['initialize'];
$init->addBody('if (!class_exists(?, ?)) class_alias(?, ?);', ['WebLoader\\LoaderFactory', false, 'WebLoader\\Nette\\LoaderFactory', 'WebLoader\\LoaderFactory']);
}
// public function afterCompile(Nette\PhpGenerator\ClassType $class): void
// {
// $meta = $class->getProperty('meta');
// if (array_key_exists('webloader\\nette\\loaderfactory', $meta->value['types'])) {
// $meta->value['types']['webloader\\loaderfactory'] = $meta->value['types']['webloader\\nette\\loaderfactory'];
// }
// if (array_key_exists('WebLoader\\Nette\\LoaderFactory', $meta->value['types'])) {
// $meta->value['types']['WebLoader\\LoaderFactory'] = $meta->value['types']['WebLoader\\Nette\\LoaderFactory'];
// }
//
// $init = $class->methods['initialize'];
// $init->addBody('if (!class_exists(?, ?)) class_alias(?, ?);', ['WebLoader\\LoaderFactory', false, 'WebLoader\\Nette\\LoaderFactory', 'WebLoader\\LoaderFactory']);
// }


public function install(Configurator $configurator): void
{

bdump('x');
$self = $this;
$configurator->onCompile[] = function ($configurator, Compiler $compiler) use ($self): void {
$compiler->addExtension($self::EXTENSION_NAME, $self);
Expand Down
6 changes: 3 additions & 3 deletions WebLoader/Nette/WebLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace WebLoader\Nette;

use Nette\Application\UI\Control;
use Nette\Utils\Html;
use WebLoader\Compiler;
use WebLoader\File;
Expand All @@ -15,10 +16,10 @@
* @author Jan Marek
* @license MIT
*/
abstract class WebLoader extends \Nette\Application\UI\Control
abstract class WebLoader extends Control
{

/** @var \WebLoader\Compiler */
/** @var Compiler */
private $compiler;

/** @var string */
Expand All @@ -30,7 +31,6 @@ abstract class WebLoader extends \Nette\Application\UI\Control

public function __construct(Compiler $compiler, string $tempPath, bool $appendLastModified)
{
parent::__construct();
$this->compiler = $compiler;
$this->tempPath = $tempPath;
$this->appendLastModified = $appendLastModified;
Expand Down
47 changes: 24 additions & 23 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,39 @@
},
"require": {
"php": ">= 7.1",
"nette/application": "^2.4",
"nette/di": "^2.4",
"nette/utils": "^2.4"
},
"nette/application": "^3.0",
"nette/di": "^3.0",
"nette/utils": "^3.0",
"ext-json": "*"
},
"suggest": {
"oyejorge/less.php": "LESS compiler written in PHP.",
"leafo/scssphp": "SCSS compiler written in PHP.",
"joseki/webloader-filters": "CSSMin & JSMin filters written in PHP.",
"coffeescript/coffeescript": "CoffeeScript compiler written in PHP."
},
"require-dev": {
"nette/application": "^2.4",
"nette/bootstrap": "^2.4",
"nette/caching": "^2.5",
"nette/component-model": "^2.3",
"nette/database": "^2.4",
"nette/application": "^3.0",
"nette/bootstrap": "^3.0",
"nette/caching": "^3.0",
"nette/component-model": "^3.0",
"nette/database": "^3.0",
"nette/deprecated": "^2.3",
"nette/di": "^2.4",
"nette/finder": "^2.4",
"nette/forms": "^2.4",
"nette/http": "^2.4",
"nette/mail": "^2.4",
"nette/neon": "^2.4",
"nette/php-generator": "^2.4",
"nette/di": "^3.0",
"nette/finder": "^2.5",
"nette/forms": "^3.0",
"nette/http": "^3.0",
"nette/mail": "^3.0",
"nette/neon": "^3.0",
"nette/php-generator": "^3.0",
"nette/reflection": "^2.4",
"nette/robot-loader": "^2.4",
"nette/safe-stream": "^2.3",
"nette/security": "^2.4",
"nette/tokenizer": "^2.2",
"nette/utils": "^2.4",
"latte/latte": "^2.4",
"tracy/tracy": "^2.4",
"nette/robot-loader": "^3.0",
"nette/safe-stream": "^2.4",
"nette/security": "^3.0",
"nette/tokenizer": "^3.0",
"nette/utils": "^3.0",
"latte/latte": "^2.5",
"tracy/tracy": "^2.6",
"oyejorge/less.php": "^1.7",
"leafo/scssphp": "^0.7",
"kylekatarnls/coffeescript": "1.3.*",
Expand Down
17 changes: 11 additions & 6 deletions tests/Nette/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@

namespace WebLoader\Test\Nette;

use Nette\Configurator;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\Utils\Finder;
use PHPUnit\Framework\TestCase;
use WebLoader\Nette\Extension;
use WebLoader\Path;


class ExtensionTest extends TestCase
{

/** @var \Nette\DI\Container */
/** @var Container */
private $container;


Expand All @@ -22,7 +26,7 @@ private function prepareContainer($configFiles): void
unlink((string) $file);
}

$configurator = new \Nette\Configurator();
$configurator = new Configurator();
$configurator->setTempDirectory($tempDir);

foreach ($configFiles as $file) {
Expand All @@ -35,7 +39,7 @@ private function prepareContainer($configFiles): void
'tempDir' => $tempDir,
]);

$extension = new \WebLoader\Nette\Extension();
$extension = new Extension(__DIR__ . '/..', $configurator->isDebugMode());
$extension->install($configurator);

$this->container = @$configurator->createContainer(); // sends header X-Powered-By, ...
Expand Down Expand Up @@ -172,11 +176,12 @@ public function testExtensionName(): void
$tempDir = __DIR__ . '/../temp';
$class = 'ExtensionNameServiceContainer';

$configurator = new \Nette\Configurator();
$configurator = new Configurator();
$configurator->setTempDirectory($tempDir);
$configurator->addParameters(['container' => ['class' => $class]]);
$configurator->onCompile[] = function ($configurator, \Nette\DI\Compiler $compiler) {
$compiler->addExtension('Foo', new \WebLoader\Nette\Extension());
$configurator->onCompile[] = function (Configurator $configurator, Compiler $compiler) {
$extension = new Extension(__DIR__ . '/..', $configurator->isDebugMode());
$compiler->addExtension('Foo', $extension);
};
$configurator->addConfig(__DIR__ . '/../fixtures/extensionName.neon');
$container = $configurator->createContainer();
Expand Down
5 changes: 2 additions & 3 deletions tests/fixtures/extension.neon
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
parameters:
cssJoinFiles: false

nette:
security:
frames: yes
http:
frames: yes

services:
variablesFilter: WebLoader\Filter\VariablesFilter({foo: bar})
Expand Down

0 comments on commit 9426ee2

Please sign in to comment.