PHP code example of rubenmartindev / prestashop-module-installer
1. Go to this page and download the library: Download rubenmartindev/prestashop-module-installer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rubenmartindev / prestashop-module-installer example snippets
use RubenMartinDev\PrestaShopModuleInstaller\Exception\InstallerException;
use RubenMartinDev\PrestaShopModuleInstaller\Installer;
class MyModule extends Module
{
/** @var Installer */
private $installer;
public function __construct()
{
// ...
$this->installer = Installer::createFrom(
$this,
[
'configuration' => [
[
'name' => 'is_enabled',
'value' => true,
],
],
'database' => [
[
'tableName' => 'my_table',
'query' => 'CREATE TABLE `{{DB_PREFIX}}my_table`;',
],
],
'hooks' => [
[
'name' => 'displayHeader',
],
],
'tabs' => [
[
'className' => 'AdminMyTab',
'name' => 'My admin tab',
],
],
],
);
}
/**
* {@inheritDoc}
*/
public function install()
{
if (!parent::install()) {
return false;
}
try {
return $this->installer->install();
} catch (InstallerException $e) {
$this->_errors[] = $e->getMessage();
}
return false;
}
/**
* {@inheritDoc}
*/
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
try {
return $this->installer->uninstall();
} catch (InstallerException $e) {
$this->_errors[] = $e->getMessage();
}
return false;
}
}
Installer::createFrom(
$myModule,
[
'configuration' => [
// ...
],
'hooks' => [
// ...
],
'database' => [
// ...
],
'tabs' => [
// ...
],
]
);
Installer::createFrom(
$myModule,
[
'configuration' => [
[
'name' => 'my_boolean', // --> MY_MODULE_MY_BOOLEAN
'value' => true, // --> 1
],
[
'name' => 'my_string', // --> MY_PREFIX_MY_STRING
'value' => 'abcdef', // --> abcdef
'prefix' => 'my_prefix'
],
[
'name' => 'my_array', // --> MY_MODULE_MY_ARRAY
'value' => [ // --> {"email": "[email protected]"}
'email' => '[email protected]',
],
'prefix' => null,
],
[
'name' => 'my_object', // --> MY_MODULE_MY_OBJECT
'value' => new stdClass(), // --> O:8:"stdClass":0:{}
],
[
'name' => 'my_callback', // --> OTHER_PREFIX_MY_CALLBACK
'value' => function () { // --> my_value
return 'my_value';
},
'prefix' => 'other_prefix',
],
// ...
],
// ...
],
);
Installer::createFrom(
$myModule,
[
'database' => [
[
'tableName' => 'my_table',
'query' => 'CREATE TABLE `{{DB_PREFIX}}my_table`;',
],
[
'tableName' => 'my_another_table',
'queryFile' => "{$myModule->getLocalPath()}/my_another_table.sql",
'keepData' => true,
],
// ...
],
// ...
],
);
Installer::createFrom(
$myModule,
[
'hooks' => [
[
'name' => 'displayHeader',
],
[
'name' => 'displayFooter',
'prestashopVersion' => '>=1.7',
],
[
'name' => 'displayAdminView',
'prestashopVersion' => ['min' => '>=1.7'],
],
[
'name' => 'displayAdminListBefore',
'prestashopVersion' => ['min' => '>=1.7', 'max' => '<8.0'],
],
// ...
],
// ...
],
);
Installer::createFrom(
$myModule,
[
'tabs' => [
[
'className' => 'AdminMyTab',
'name' => 'My admin tab',
],
[
'className' => 'AdminMyAnotherTab',
'name' => [1 => 'My another tab in EN', 2 => 'My another tab in ES'],
'parentId' => 10,
'position' => 2,
'isActive' => false,
],
[
'className' => 'AdminMyExtraTab',
'name' => 'My extra tab',
'parentId' => 'AdminParentOrders',
'position' => 3,
'routeName' => 'admin_my_module_my_extra_tab',
'icon' => 'extension',
'wording' => 'My extra tab',
'wordingDomain' => 'Modules.MyModule.Navigation',
],
// ...
],
// ...
],
);
namespace MyModule\Installer\Handler;
use Customer;
use RubenMartinDev\PrestaShopModuleInstaller\Handler\HandlerInterface;
final class MyCustomHandler implements HandlerInterface
{
private $items;
public function __construct(array $items)
{
$this->items = $items;
}
public static function createFrom(Module $module, array $items)
{
return new static($items);
}
public function install()
{
$isSuccessful = true;
foreach ($this->items as $item) {
$customer = new Customer();
$customer->lastname = $item['lastname'];
$customer->firstname = $item['firstname'];
$customer->email = $item['email'];
$customer->passwd = $item['passwd'];
$isSuccessful &= $customer->add();
}
return $isSuccessful;
}
public function uninstall()
{
$isSuccessful = true;
foreach ($this->items as $item) {
$customer = Customer::getByEmail($item['email']);
$isSuccessful &= $customer->delete();
}
return $isSuccessful;
}
}
Installer::createFrom(
$myModule,
[
\MyModule\Installer\Handler\MyCustomHandler::class => [
[
'lastname' => 'John',
'firstname' => 'Doe',
'email' => '[email protected]',
'passwd' => 'e64c7d89f26bd1972efa854d13d7dd61',
],
],
]
);