This plugins allow Atomik to be used in a terminal. It allows other plugins to provide custom commands and to create scripts to better administer your application.
To call your application from the command line, use the following command
php index.php [command] [args]
Where index.php is Atomik's core file.
Scripts are simple php files. You can do anything you want with them. They are located
in the app/scripts folder. This can be changed using the “scripts_dir”
conguration key.
To call your script use the previous command line by replacing [command] with the filename of your script without the extension.
Example 3.1. Calling custom scripts
Let's create a script in app/scripts/cleanup-my-db.php
<?php // do some stuff to cleanup my database
To call this script use the following command:
php index.php cleanup-my-db
It is possible for the script to access the command line arguments. A variable named $arguments
is available in script files. It is an array containing all the arguments after the command name (like $_SERVER[argv]).
Example 3.2. Calling custom scripts with arguments
Create a script in app/scripts/cleanup-my-db.php
<?php echo 'cleaning ' . $arguments[0];
Call the following script using
php index.php cleanup-my-db dbname
Will print “cleaning dbname”.
A command is like a script but in the form of a function. Commands needs to be registered. Their useful because they provide a way for any plugins to add its own command for administration.
To register a command use the ConsolePlugin::register() method. It takes
as first argument the command name (the one that will be used in the command line) and a php
callback to the command's associated function or method.
Example 3.3. Creating a command
function myCommand()
{
// ...
}
ConsolePlugin::register('my-command', 'myCommand');
Can be called with:
php index.php my-command
Like scripts, commands can retreive the arguments from the command line. They will be passed as the first parameter of the callback
Example 3.4. Creating a command with arguments
function sayHelloTo($arguments)
{
echo 'hello ' . $arguments[0];
}
ConsolePlugin::register('say-hello-to', 'sayHelloTo');
Can be called with:
php index.php say-hello-to peter
Will output “hello peter”.
Other plugins can listen for the Console::Start event and register their commands from the listener callback.
The plugin provides two built-in methods: init and generate.
The first one will initialize a project by creating all the needed directories. If the “--htaccess”
option is used, the .htaccess file will also be generated.
The second command allows you to create new actions and views. Just specify a name and the action file and the view file will be generated. You can generate multiple pages by separating them by a space
Example 3.6. Using the generate command
php index.php generate home php index.php generate photos about
The Console plugin also provides some utility methods.
ConsolePlugin::println() can be used to print a message on a single line.
You can also control the indentation using the second argument (which is an int).
ConsolePlugin::success() and ConsolePlugin::fail() are
two methods to print a “[SUCCESS]” or “[FAIL]” string. A message can also be
printed. They can work in conjonction with ConsolePlugin::println(). A new line
is added after a success or fail call.
Example 3.7. Printing a message
ConsolePlugin::println('trying to connect to the remote server...');
if ($connectionSuccess) {
ConsolePlugin::success();
} else {
ConsolePlugin::fail();
}
This will print something like (if success): “trying to connect to the remote server... [SUCCESS]”
You can create directories using ConsolePlugin::mkdir().
Example 3.8. Creating directories
ConsolePlugin::mkdir('/my/dir');
ConsolePlugin::mkdir('/my/dir', $indent, "My message to announce i'm creating a directory");
Finally, you can create files using ConsolePlugin::touch().
Example 3.9. Creating files
ConsolePlugin::touch('/path/to/my/file.txt');
ConsolePlugin::touch('/path/to/my/file.txt', $fileContent);
ConsolePlugin::touch('/path/to/my/file.txt', '', $indent, "My message to announce i'm creating a file");