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;
use RubenMartinDev\PrestaShopModuleInstaller\InstallerInterface;

class MyModule extends Module
{
    /**
     * {@inheritDoc}
     */
    public function install()
    {
        if (!parent::install()) {
            return false;
        }

        try {
            return $this->getInstaller()->install();
        } catch (InstallerException $e) {
            $this->_errors[] = $e->getMessage();
        }

        return false;
    }

    /**
     * {@inheritDoc}
     */
    public function uninstall()
    {
        if (!parent::uninstall()) {
            return false;
        }

        try {
            return $this->getInstaller()->uninstall();
        } catch (InstallerException $e) {
            $this->_errors[] = $e->getMessage();
        }

        return false;
    }

    /**
     * @return InstallerInterface
     */
    private function getInstaller()
    {
        return Installer::createFrom(
            $this,
            [
                'configuration' => [
                    [
                        'name'        => 'is_enabled',
                        'value'       => true,
                    ],
                ],
                'database'      => [
                    [
                        'table_name'  => 'my_table',
                        'query'       => 'CREATE TABLE `{{DB_PREFIX}}my_table`;',
                    ],
                ],
                'hooks'         => [
                    [
                        'name'        => 'displayHeader',
                    ],
                ],
                'tabs'          => [
                    [
                        'class_name'  => 'AdminMyTab',
                        'name'        => 'My admin tab',
                    ],
                ],
            ],
        );
    }
}

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' => [
            [
                'table_name'  => 'my_table',
                'query'       => 'CREATE TABLE `{{DB_PREFIX}}my_table`;',
            ],
            [
                'table_name'  => 'my_another_table',
                'query_file'  => "{$myModule->getLocalPath()}/my_another_table.sql",
                'keep_data'   => true,
            ],
            // ...
        ],
        // ...
    ],
);

Installer::createFrom(
    $myModule,
    [
        'hooks' => [
            [
                'name'                => 'displayHeader',
            ],
            [
                'name'                => 'displayFooter',
                'prestashop_version'  => '>=1.7',
            ],
            [
                'name'                => 'displayAdminView',
                'prestashop_version'  => ['min' => '>=1.7'],
            ],
            [
                'name'                => 'displayAdminListBefore',
                'prestashop_version'  => ['min' => '>=1.7', 'max' => '<8.0'],
            ],
            // ...
        ],
        // ...
    ],
);

Installer::createFrom(
    $myModule,
    [
        'tabs' => [
            [
                'class_name'      => 'AdminMyTab',
                'name'            => 'My admin tab',
            ],
            [
                'class_name'      => 'AdminMyAnotherTab',
                'name'            => [1 => 'My another tab in EN', 2 => 'My another tab in ES'],
                'parent_id'       => 10,
                'position'        => 2,
                'is_active'       => false,
            ],
            [
                'class_name'      => 'AdminMyExtraTab',
                'name'            => 'My extra tab',
                'parent_id'       => 'AdminParentOrders',
                'position'        => 3,
                'route_name'      => 'admin_my_module_my_extra_tab',
                'icon'            => 'extension',
                'wording'         => 'My extra tab',
                'wording_domain'  => 'Modules.MyModule.Navigation',
            ],
            // ...
        ],
        // ...
    ],
);

namespace MyModule\Installer\Handler;

use Customer;
use RubenMartinDev\PrestaShopModuleInstaller\Handler\Exception\HandlerException;
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()
    {
        foreach ($this->items as $item) {
            $customer = new Customer();

            $customer->lastname   = $item['lastname'];
            $customer->firstname  = $item['firstname'];
            $customer->email      = $item['email'];
            $customer->passwd     = $item['passwd'];

            if (!$customer->add()) {
                throw new HandlerException('Failed to create customer ' . $item['email']);
            }
        }

        return true;
    }

    public function uninstall()
    {
        foreach ($this->items as $item) {
            $customer = Customer::getByEmail($item['email']);

            if (!$customer->delete()) {
                throw new HandlerException('Failed to delete customer ' . $item['email']);
            }
        }

        return true;
    }
}

Installer::createFrom(
    $myModule,
    [
        \MyModule\Installer\Handler\MyCustomHandler::class => [
            [
                'lastname'  => 'John',
                'firstname' => 'Doe',
                'email'     => '[email protected]',
                'passwd'    => 'e64c7d89f26bd1972efa854d13d7dd61',
            ],
        ],
    ]
);