Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Nette Framework container extension (experimental feature)

  • Loading branch information...
commit 26a498772aa914c700c7884ae61424ed7a6bfdb5 1 parent b89084a
Jan Marek janmarek authored
Showing with 178 additions and 1 deletion.
  1. +122 −0 WebLoader/Nette/Extension.php
  2. +56 −1 readme.txt
122 WebLoader/Nette/Extension.php
View
@@ -0,0 +1,122 @@
+<?php
+
+namespace WebLoader\Nette;
+
+/**
+ * @author Jan Marek
+ */
+class Extension extends \Nette\Config\CompilerExtension
+{
+
+ const EXTENSION_NAME = 'webloader';
+
+ public function getDefaultConfig()
+ {
+ return array(
+ 'jsDefaults' => array(
+ 'sourceDir' => '%wwwDir%/js',
+ 'tempDir' => '%wwwDir%/webtemp',
+ 'tempPath' => './webtemp',
+ 'files' => array(),
+ 'remoteFiles' => array(),
+ 'filters' => array(),
+ 'fileFilters' => array(),
+ 'namingConvention' => '@' . $this->prefix('jsNamingConvention'),
+ ),
+ 'cssDefaults' => array(
+ 'sourceDir' => '%wwwDir%/css',
+ 'tempDir' => '%wwwDir%/webtemp',
+ 'tempPath' => './webtemp',
+ 'files' => array(),
+ 'remoteFiles' => array(),
+ 'filters' => array(),
+ 'fileFilters' => array(),
+ 'namingConvention' => '@' . $this->prefix('cssNamingConvention'),
+ ),
+ 'js' => array(
+
+ ),
+ 'css' => array(
+
+ ),
+ );
+ }
+
+ public function loadConfiguration()
+ {
+ $builder = $this->getContainerBuilder();
+ $config = $this->getConfig($this->getDefaultConfig());
+
+ $builder->addDefinition($this->prefix('cssNamingConvention'))
+ ->setFactory('WebLoader\DefaultOutputNamingConvention::createCssConvention');
+
+ $builder->addDefinition($this->prefix('jsNamingConvention'))
+ ->setFactory('WebLoader\DefaultOutputNamingConvention::createJsConvention');
+
+ $builder->parameters['webloader'] = $config;
+
+ foreach (array('css', 'js') as $type) {
+ foreach ($config[$type] as $name => $wlConfig) {
+ $configDefault = $config[$type . 'Defaults'];
+ $this->addWebLoader($builder, $type . '_' . $name, array_merge($configDefault, $wlConfig));
+ }
+ }
+ }
+
+ private function addWebLoader(\Nette\DI\ContainerBuilder $builder, $name, $config)
+ {
+ $filesServiceName = $this->prefix($name . '_files');
+
+ $files = $builder->addDefinition($filesServiceName)
+ ->setClass('WebLoader\FileCollection')
+ ->setArguments(array($config['sourceDir']));
+
+ foreach ($config['files'] as $file) {
+ // finder support
+ if (is_array($file) && isset($file['files']) && (isset($file['in']) || isset($file['from']))) {
+ $finder = \Nette\Utils\Finder::findFiles($file['files']);
+
+ if (isset($file['in'])) {
+ $finder->in($file['in']);
+ } else {
+ $finder->from($file['from']);
+ }
+
+ foreach ($finder as $foundFile) {
+ $files->addSetup('addFile', array((string) $foundFile));
+ }
+ } else {
+ $files->addSetup('addFile', array($file));
+ }
+ }
+
+ $files->addSetup('addRemoteFiles', array($config['remoteFiles']));
+
+ $compiler = $builder->addDefinition($this->prefix($name . '_compiler'))
+ ->setClass('WebLoader\Compiler')
+ ->setArguments(array(
+ '@' . $filesServiceName,
+ $config['namingConvention'],
+ $config['tempDir'],
+ ));
+
+ foreach ($config['filters'] as $filter) {
+ $compiler->addSetup('addFilter', $filter);
+ }
+
+ foreach ($config['fileFilters'] as $filter) {
+ $compiler->addSetup('addFileFilter', $filter);
+ }
+
+ // todo css media
+ }
+
+ public function install(\Nette\Config\Configurator $configurator)
+ {
+ $self = $this;
+ $configurator->onCompile[] = function ($configurator, $compiler) use ($self) {
+ $compiler->addExtension($self::EXTENSION_NAME, $self);
+ };
+ }
+
+}
57 readme.txt
View
@@ -35,4 +35,59 @@ Example:
Template:
- {control css}
+ {control css}
+
+
+
+Example with Nette Framework extension used
+-------------------------------------------
+
+
+
+Extension is registered in bootstrap.php:
+
+ $webloaderExtension = new \WebLoader\Nette\Extension();
+ $webloaderExtension->install($configurator);
+
+
+Configuration in config.neon:
+
+ services:
+ wlCssFilter: WebLoader\Filter\CssUrlsFilter(%wwwDir%)
+
+ webloader:
+ css:
+ default:
+ files:
+ - style.css
+ - {files: ["*.css", "*.less"], from: %appDir%/presenters} # Nette\Utils\Finder support
+ filters:
+ - @wlCssFilter
+ js:
+ default:
+ remoteFiles:
+ - http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js
+ - http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js
+ files:
+ - %appDir%/../libs/nette/nette/client-side/forms/netteForms.js
+ - web.js
+
+
+
+BasePresenter.php (will be better):
+
+ public function createComponentCss()
+ {
+ return new \WebLoader\Nette\CssLoader(
+ $this->context->webloader->css_default_compiler, // service generated by extension
+ $this->template->basePath . '/webtemp'
+ );
+ }
+
+ public function createComponentJs()
+ {
+ return new \WebLoader\Nette\JavaScriptLoader(
+ $this->context->webloader->js_default_compiler, // service generated by extension
+ $this->template->basePath . '/webtemp'
+ );
+ }
Please sign in to comment.
Something went wrong with that request. Please try again.