Contributte Console-extra
Nette-based console commands for latte, DIC, security, utils and many others.
# Content
# Setup
Install package
composer require contributte/console-extra
Register all commands
extensions:
console: Contributte\Console\DI\DIConsoleExtension(%consoleMode%)
# register all console bridges
console.extra: Contributte\Console\Extra\DI\ConsoleBridgesExtension(%consoleMode%)
console.extra:
# optionally disable these bridges
cache: false
caching: false
di: false
latte: false
router: false
security: false
utils: false
advancedCache: false
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
You can also register bridges one by one
extensions:
# register only bridges of your choice
console.cache: Contributte\Console\Extra\DI\CacheConsoleExtension(%consoleMode%)
console.caching: Contributte\Console\Extra\DI\CachingConsoleExtension(%consoleMode%)
console.di: Contributte\Console\Extra\DI\DIConsoleExtension(%consoleMode%)
console.latte: Contributte\Console\Extra\DI\LatteConsoleExtension(%consoleMode%)
console.router: Contributte\Console\Extra\DI\RouterConsoleExtension(%consoleMode%)
console.security: Contributte\Console\Extra\DI\SecurityConsoleExtension(%consoleMode%)
console.utils: Contributte\Console\Extra\DI\UtilsConsoleExtension(%consoleMode%)
console.advancedCache: Contributte\Console\Extra\DI\AdvancedCacheConsoleExtension(%consoleMode%)
console.database: Contributte\Console\Extra\DI\DatabaseConsoleExtension(%consoleMode%)
2
3
4
5
6
7
8
9
10
11
To use these commands you need to setup a bin/console entrypoint (opens new window).
# Extension
At this moment, these bridges are available:
- cache
- caching
- di
- latte
- router
- security
- utils
- advanced cache
# CacheConsole
console.cache:
purge:
- %tempDir%/cache
2
3
The purge
parameter expects an array of dirs.
Available commands:
nette:cache:purge
# CachingConsole
Available commands:
nette:caching:clear
This command requires to specify the cleaning strategy.
The cleaning strategy options are:
--all
or-a
shortcut--tag <tag>
or-t <tag>
shortcut--priority <priority>
or-p <priority>
shortcut
NOTE: Only one tag can be used at the time.
# DIConsole
console.di:
purge:
- %tempDir%/cache/Nette.Configurator
2
3
The purge
parameter expects an array of dirs.
Available commands:
nette:di:purge
# LatteConsole
console.latte:
warmup:
- %appDir%
warmupExclude: []
purge:
- %tempDir%/cache/latte
2
3
4
5
6
The warmup
, warmupExclude
and purge
parameters are expecting an array of dirs.
Available commands:
nette:latte:warmup
nette:latte:purge
# RouterConsole
Available commands:
nette:router:dump
# SecurityConsole
Available commands:
nette:security:password
# UtilsConsole
Available commands:
nette:utils:random
This command supports:
- count parameter (
--count <count>
or-c <count>
shortcut), to change the count of random strings. Default count is 10. - length parameter (
--length <length>
or-l <length>
shortcut), to change the length of random strings. Default count is 50.
# Database
console.database:
backupPath: %appDir%/../backups/database
2
Backup database
contributte:database:backup mysql 127.0.0.1 3306 username password database path/to file.sql
- Path could be omitted (if defined in configuration)
- Filename could be omitted (will be generated)
--no-gzip
(-g
) - disables gzip compression--bin-path
(-b
) - path to mysql/psql binary
Load database from backup
contributte:database:load mysql 127.0.0.1 3306 username password database path/to/file.sql
--bin-path
(-b
) - path to mysql/psql binary
# AdvancedCacheConsole
# Generator
Generate application cache with a single command
contributte:cache:generate
--list
show list of available generators--generator GENERATOR
use only specified generator
# Register generators you want to use:
console.advancedCache:
generators:
latte: Contributte\Console\Extra\Cache\Generators\LatteTemplatesCacheGenerator(
@Nette\Application\UI\ITemplateFactory,
[%appDir%],
[],
::realpath(%appDir%/..)
)
2
3
4
5
6
7
8
# Available generators:
- Latte templates cache generator
Contributte\Console\Extra\Cache\Generators\LatteTemplatesCacheGenerator(
[%appDir%],
@Nette\Bridges\ApplicationLatte\ILatteFactory::create()
)
2
3
4
DI containers generator
- This example is configured to generate 3 containers - 1 for production mode, 1 for debug mode and 1 for console (should be enough for every application)
- You don't need to add the
productionMode
parameter for Nette BC, it is done automatically.
Contributte\Console\Extra\Cache\Generators\DiContainersCacheGenerator(
[
debug: [debugMode: true, consoleMode: false],
production: [debugMode: false, consoleMode: false],
console: [debugMode: true, consoleMode: true]
]
)
2
3
4
5
6
7
You will also need slightly modify Bootstrap.php
and add Configurator as dynamic (imported) service to neon to get this generator work.
$configurator->addServices(['configurator' => $configurator]);
services:
configurator:
type: Nette\Configurator
imported: true
2
3
4
# Implement your own generator:
use Contributte\Console\Extra\Cache\Generators\IGenerator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class YourGenerator implements IGenerator
{
public function getDescription(): string
{
return 'description which is shown in console when you run `contributte:cache:generate --list`'
}
public function generate(InputInterface $input, OutputInterface $output): bool
{
// generate cache
// inform about it in console
// return true if generating was successful, false otherwise
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Cleaner
Clean application cache with a single command
contributte:cache:clean
--list
show list of available cleaners--cleaner CLEANER
use only specified cleaner
# Register cleaners you want to use:
console.advancedCache:
cleaners:
localFs: Contributte\Console\Extra\Cache\Cleaners\LocalFilesystemCleaner([%tempDir%])
2
3
# Available cleaners:
- APC cleaner
Contributte\Console\Extra\Cache\Cleaners\ApcCleaner()
- APCu cleaner
Contributte\Console\Extra\Cache\Cleaners\ApcuCleaner()
- Local filesystem cleaner
Contributte\Console\Extra\Cache\Cleaners\LocalFilesystemCleaner([%tempDir%], [%tempDir%/ignored/])
- Memcache(d) cleaner
Contributte\Console\Extra\Cache\Cleaners\MemcachedCleaner([@memcache1, @memcache2])
- Nette\Caching\IStorage cleaner
Contributte\Console\Extra\Cache\Cleaners\NetteCachingStorageCleaner([@storage1, @storage2])
- Opcode cleaner
Contributte\Console\Extra\Cache\Cleaners\OpcodeCleaner()
# Implement your own cleaner:
use Contributte\Console\Extra\Cache\Cleaners\ICleaner;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class YourCleaner implements ICleaner
{
public function getDescription(): string
{
return 'description which is shown in console when you run `contributte:cache:clean --list`'
}
public function clean(InputInterface $input, OutputInterface $output): bool
{
// clean cache
// inform about it in console
// return true if cleaning was successful, false otherwise
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Compatibility
How to make this extension work with other Symfony/Console implementations.
# Kdyby/Console
Kdyby
packages use the kdyby.console.command
tag to mark its Command
classes in order to find them. So it won't recognize commands from other packages which don't tag them this way.
This is where the decorator extension comes into play:
decorator:
Symfony\Component\Console\Command\Command:
tags: [kdyby.console.command]
2
3
Now kdyby.console
will be able to recognize all available commands added by this extension.
# Examples
# 1. Example projects
We've made a few skeletons with preconfigured Contributte packages.
- https://github.com/contributte/webapp-skeleton (opens new window)
- https://github.com/contributte/apitte-skeleton (opens new window)
# 2. Example playground
- https://github.com/contributte/playground (opens new window) (playground)
- https://contributte.org/examples.html (opens new window) (more examples)