Skip to content

Commit

Permalink
Merge pull request #2 from grape-fluid/feature/rework
Browse files Browse the repository at this point in the history
Feature/rework
  • Loading branch information
sparkcz committed Feb 14, 2019
2 parents 72ea6c3 + 24fe42a commit 56ab95a
Show file tree
Hide file tree
Showing 18 changed files with 568 additions and 108 deletions.
250 changes: 248 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,255 @@
[![Downloads total](https://img.shields.io/packagist/dt/grape-fluid/asset-loader.svg?style=flat-square)](https://packagist.org/packages/grape-fluid/asset-loader)
[![Latest stable](https://img.shields.io/packagist/v/grape-fluid/asset-loader.svg?style=flat-square)](https://packagist.org/packages/grape-fluid/asset-loader)

Asset Loader for your Nette projects

## Install
## Dependencies

| Require | Version |
|-------------------|--------------|
| PHP | `>= 5.6` |
| nette/application | `^2.4` |
| nette/di | `^2.4` |

| Require-dev | Version |
|-------------------|--------------|
| nette/tester | `^2.0` |
| tracy/tracy | `^2.4` |

## Quickstart

1. Install **Asset Loader** with composer

```
composer require grape-fluid/asset-loader
```

2. Register **extension** (in neon)

```
extensions:
assets: Grapesc\GrapeFluid\AssetLoaderExtension
```

3. Set your **public directory** (mostly www) (in neon)

```
assets:
config:
wwwDir: %appDir%/../www
```

4. Set-up your first package (see all options / features below)

```
assets:
yourPackage:
js:
# will create file in wwwDir/assets/yourPackage/js/main.js
- %appDir%/modules/WebsiteModule/assets/js/main.js
css:
# will create file in wwwDir/assets/yourPackage/css/styles.css
- %appDir%/modules/StyleModule/assets/css/style.css
# you can also use external / remote assets
- 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.min.css'
copy:
# will copy all images from folder into wwwDir/assets/yourPackage/copy/
- %appDir%/modules/StyleModule/assets/images/*
```

5. Inject *AssetsControl* and create component in your **BasePresenter**

```
/** @var \Grapesc\GrapeFluid\AssetsControl\AssetsControl @inject */
public $assets;


protected function createComponentAssets()
{
return $this->assets;
}
```

6. Render your assets in **template**

```
{control assets:css}
{control assets:js}
```

## Multiple files

If you have too many files (for ex. scripts) that you want to include in your templates,
or just want to copy whole directory (for ex. with images),
you can use asterisk `*` character.

```
assets:
yourPackage:
js:
- %appDir%/modules/WebsiteModule/assets/js/scripts/*
```

## Custom destination

If you need, for any reason, copy file into public directory with custom name or custom directory,
just specify asset as array. You can use `&` character that will be replaced with public assets directory folder.

```
assets:
yourPackage:
js:
# [ source, destination ]
- [ %appDir%/modules/WebsiteModule/assets/js/main.js, &/my/folder/test.js ]
```

You can also do the same with whole **directories**:

```
assets:
yourPackage:
js:
# [ source, destination ]
- [ %appDir%/modules/StyleModule/assets/images/*, &/images/ ]
```

## Options

Of course, quickstart doesn't do anything special but copy files and use them in your template files.
But it's getting more interesting with these options.

### Limits

You can limit **assets** by Nette-like links.
By default all defined assets are enabled everywhere.
**You can combine multiple limits.** By using limits Asset Loader will load only assets that matches your limit.
You can easily create assets for backend / frontend, but also for sub-frontend modules, etc.

#### How to limit asset for **module**?

```
assets:
yourPackage:
limit:
- ":Module:.*"
```

#### How to limit asset for multiple **presenters**?

```
assets:
yourPackage:
limit:
- ":Module:Presenter:.*"
- ":Module:AnotherPresenter:.*"
```

#### Limiting by access rights ($user->isAllowed())
```
assets:
yourPackage:
# Will call $user->isAllowed('homepage')
- ['link' = ':Module:Presenter.*', 'auth' = 'homepage']
```

#### Limiting by custom options

Create and define custom service that implements `\Grapesc\GrapeFluid\Options\IAssetOptions` interface as written below.
**We are automatically registering these as services** sou you don't have to.
**Inject through constructor** in these services also works.
Now you are able to handle limits in any way you want.
For example enable / disable assets from your back-office.

```
assets:
config:
options:
# Must implement \Grapesc\GrapeFluid\Options\IAssetOptions
- \Your\Class\That\Handles\SomeOptions
- \Your\Class\That\Handles\AnotherOptions
yourPackage:
# Will call $class->getOption('inline.enabled') on every service
- ['link' = ':Module:Presenter.*', 'option' = 'inline.enabled']
```

#### Using negation limit

Use `!` in front of limit

```
assets:
yourPackage:
limit:
- "!:Module:Presenter:.*"
```

### Ordering

Sometimes, especially when using modules, it comes handy to sort / order your packages.

#### Start / End

```
assets:
yourPackage:
order: [start|end]
```

#### Before / After

```
assets:
yourPackage:
order:
[before|after]: anotherPackageName
```

or

```
assets:
yourPackage:
order:
type: [before|after]
position: anotherPackageName
```

### Disabling

```
assets:
yourPackage:
[disable|disabled]: true
```


## Debug

By default Asset Loader operates in production mode, you can override this with following:

```
assets:
config:
debug: true
```

When debug mode is one, Asset Loader watch files for changes and deploy them automatically.
It also works if you add new files into your packages.

## Other

### Custom asset folder

```
assets:
config:
assetsDir: customDirectoryName
```

### Directory permissions

```
composer require grape-fluid/asset-loader
assets:
config:
dirPerm: 0511
```
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
],
"require": {
"php": ">= 5.6",
"nette/application": "^2.4"
"nette/application": "^2.4",
"nette/di": "^2.4"
},
"require-dev": {
"nette/tester": "^2.0",
Expand Down
12 changes: 12 additions & 0 deletions src/AssetLoaderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Grapesc\GrapeFluid;


/**
* @author Kulíšek Patrik <kulisek@grapesc.cz>
*/
class AssetLoaderException extends \Exception
{

}
82 changes: 66 additions & 16 deletions src/AssetLoaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,87 @@

namespace Grapesc\GrapeFluid;

use Grapesc\GrapeFluid\Options\EnableAllAssetOptions;
use Grapesc\GrapeFluid\Options\IAssetOptions;
use Nette\DI\CompilerExtension;
use Nette\DI\Config\Helpers;
use Nette\DI\Statement;


/**
* @author Kulíšek Patrik <kulisek@grapesc.cz>
*/
class AssetLoaderExtension extends CompilerExtension
{

/** @var array */
private $params;


public function __construct($appDir, $assetsDir, $dirPerm = 0777, $debug = false)
protected $defaults = [
"wwwDir" => null,
"assetsDir" => "assets",
"dirPerm" => 0511,
"debug" => false,
"options" => []
];

/** @var array */
private $defaultConfig = [];

/** @var array */
private $options = [];


public function __construct(array $defaultConfig = [])
{
$this->params = [
"appDir" => $appDir,
"assetsDir" => $assetsDir,
"dirPerm" => $dirPerm,
"debug" => $debug
];
$this->defaultConfig = $defaultConfig;
}


public function loadConfiguration()
{
$config = $this->getConfig();
$config = $this->validateConfig($this->defaults, Helpers::merge($this->defaultConfig, $this->config['config'] ?? []));
$builder = $this->getContainerBuilder();
$optionClasses = $config['options'];
unset($config['options']);

if (empty($optionClasses)) {
$optionClasses[] = EnableAllAssetOptions::class;
}

foreach ($optionClasses as $key => $class) {
if (!class_exists($class)) {
throw new AssetOptionException($class . " not found");
} else {
$reflection = new \ReflectionClass($class);
if (!$reflection->implementsInterface(IAssetOptions::class)) {
throw new AssetOptionException("Class $class must implement " . IAssetOptions::class);
}

$definition = $this->prefix('option.' . strtolower($reflection->getShortName()));
$builder->addDefinition($definition)
->setFactory($class);

$this->options[] = "@" . $definition;
}
}

$packages = $this->config;
unset($packages['config']);

$builder->addDefinition($this->prefix('assets'))
->setFactory('Grapesc\\GrapeFluid\\AssetRepository', [$this->params, $config]);
->setFactory('Grapesc\\GrapeFluid\\AssetRepository', [$config, $packages]);

$builder->addDefinition($this->prefix("collector"))
->setFactory('Grapesc\\GrapeFluid\\ScriptCollector');

$builder->addDefinition($this->prefix('control'))
->setFactory('Grapesc\\GrapeFluid\\AssetsControl\\AssetsControl');
}


public function beforeCompile()
{
$builder = $this->getContainerBuilder();
$builder->getDefinition("assets.control")->addSetup(new Statement('$service->setOptionClasses(?)', [$this->options]));
}

}
}
12 changes: 12 additions & 0 deletions src/AssetOptionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Grapesc\GrapeFluid;


/**
* @author Kulíšek Patrik <kulisek@grapesc.cz>
*/
class AssetOptionException extends \Exception
{

}

0 comments on commit 56ab95a

Please sign in to comment.