⚠️ Warning ⚠️
This article was written 7 years and 1 month ago. The informations it contains may be outdated or no longer relevant.
Note : This is a guide for PS 1.6.x because 1.7 provide a composer.json
How to use composer in PrestaShop
The PHP World is full of talentued developers. They create very usefull and powerfull libraries. In PrestaShop, developers often have to reimplement them. Unless use composer !
Init a composer
First, you have to install composer to your system. I don't teach you how to do this, read the doc. Then, in your project root, launch
composer init
Inject autoload in Composer
The main file for PS is the famous config/config.inc.php
wich define all PS need to work.
So we will be tempted to modify it to add require ../vendor/autoload.php;
. But we don't want to edit this file directly, and add it in our VCS.
And guess what, PrestaShop devs have thought of everything! In config.inc.php, we can found this line:
define('_PS_CUSTOM_CONFIG_FILE_', _PS_CONFIG_DIR_.'settings_custom.inc.php');
...
/* Custom config made by users */
if (is_file(_PS_CUSTOM_CONFIG_FILE_)) {
include_once(_PS_CUSTOM_CONFIG_FILE_);
}
There is a proper way to include a config file.
So create a config/settings_custom.inc.php
file with this inside :
<?php
# Require Autoload from composer
require_once _PS_ROOT_DIR_ . '/vendor/autoload.php';
And you are able to use external libraries. Go on packagist
And after ?
Now, you can create modules, and use your libraries inside
But the must is to separate your modules in external repos to require them in your PrestaShop Installation.
How to add a command application ?
Most of framework have it now : Magento, Silex, Laravel, Typo3 ,Symfony... The Symfony Console Component !
We'll add it into our composerised PrestaShop 1.6.
Require
To add it, in root directory of application, launch
composer require symfony/console
Create an application
Create a file console
where you want. I prefer personnaly create a bin
directory and put it in.
#!/usr/bin/env php
<?php
require_once ('path/to/your/config/config.inc.php');
use Symfony\Component\Console\Application;
$application = new Application('My awesome console application', '1.0');
$application->run();
Add the execution right on this file :
chmod +x bin/console
Now, in root application, launch :
php bin/console
and Tada :tada: !
Add a Command
Create a file named MyAwesomeCommand.php
inside your module, or in a Command
directory.
<?php
namespace MyModule\Command
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyAwesomeCommand extends Command
{
/**
* Configure Command
*/
protected function configure()
{
$this
->setName('mymodule:mycommand')
->setDescription('My awesome Command in PrestaShop');
}
/**
* Execute Command
*
* @param InputInterface $input The input
* @param OutputInterface $oOutput The output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info> I\'m ready to rock</info>');
}
Register the namespace of your Command in the composer.json
:
{
...
"autoload": {
"psr-4": {
"MyModule\\Command\\": "modules/mymodule/Command/"
}
}
}
In your console
file, register your command :
$application = new Application('My awesome command application', '1.0');
// Here : register Command
$application->add(new \MyModule\Command\MyAwesomeCommand());
$application->run();
Relaunch the console
:
php bin/console
Now, your command is in the list. You can call it like that :
php bin/console mymodule:mycommand
In your Command, you have access to ALL PrestaShop classes but don't forget the \
before calling them, they have no namespace :sweat:
Feel free to read the awesome doc to learn more about command.
I have to write all commands ??
Hell, no ! Some good guys write some usefull command for PrestaShop already. Look here or here.
For efipeek/prestaconsole, you can even require it in composer, and register command in your console
file.