Falconize
Falconize is simple yet powerful tool that helps you to improve you development experience during PrestaShop modules development.
Installation
composer require oksydan/falconize
Usage
Step 1 - Create FalconizeConfiguration
First you need to creat FalconizeConfiguration class that implements FalconizeConfigurationInterface. This class will be used to configure Falconize. Example FalconizeConfiguration class:
<?php
namespace MyModule\NamespaceName;
use Doctrine\DBAL\Connection; use Oksydan\Falconize\FalconizeConfigurationInterface; use Oksydan\Falconize\PrestaShop\Module\PrestaShopModuleInterface;
final class FalconizeConfiguration implements FalconizeConfigurationInterface { protected PrestaShopModuleInterface $module;
protected Connection $connection;
protected string $databasePrefix;
protected string $prestashopVersion;
public function __construct( PrestaShopModuleInterface $module, Connection $connection, string $databasePrefix, string $prestashopVersion ) { $this->module = $module; $this->connection = $connection; $this->databasePrefix = $databasePrefix; $this->prestashopVersion = $prestashopVersion; }
public function getConnection(): Connection { return $this->connection; }
public function getModule(): PrestaShopModuleInterface { return $this->module; }
public function getConfigurationFile(): \SplFileInfo { // /../../config/configuration.yml is just an example path to configuration file return new \SplFileInfo(DIR . '/../../config/configuration.yml'); }
public function getDatabasePrefix(): string { return $this->databasePrefix; }
public function getPrestashopVersion(): string { return $this->prestashopVersion; } }
Step 2 - Create configuration file
Then create have to create configration file. Configuration file is a yaml file that contains information about database tables and hooks that you want to register/unregister. Good practice is to place you configuration file inside <code>/modules/my_module/config</code> directory. Example configuration file:
database_tables: my_table_name: columns:
name: id_my_table_name
type: integer length: 11 notnull: true autoincrement: true
name: active
type: boolean notnull: true default: 0 primary:
id_my_table_name
indexes:
name: my_table_name_active_index
columns:
active
my_table_name_lang: columns:
name: id_my_table_name
type: integer length: 11 notnull: true
name: id_lang
type: integer length: 11 notnull: true
name: name
type: string length: 255 notnull: true primary:
id_my_table_name
id_lang
constraint_keys:
name: my_table_name_lang_constraint_lang
foreign_table: lang update: NO ACTION delete: CASCADE local_columns:
id_lang
foreign_columns:
id_lang
hooks: register:
name: displayHome
name: displayModernHook
version: '8.0.0' compare_operator: '>='
name: displayLegacyHook
version: '8.0.0' compare_operator: '<' unregister:
name: displayMyOldHook
Step 3 - Create Falconize instance
Then you need to create <code>Falconize</code> instance passing <code>FalconizeConfiguration</code> instance to constructor. Your main module class have to implement <code>Oksydan\Falconize\PrestaShop\Module\PrestaShopModuleInterface</code>
<?php
use Oksydan\Falconize\Falconize; use MyModule\NamespaceName\FalconizeConfiguration; use PrestaShop\PrestaShop\Adapter\SymfonyContainer; use Oksydan\Falconize\PrestaShop\Module\PrestaShopModuleInterface;
class MyModule extends Module implements PrestaShopModuleInterface { protected Falconize $falconize;
public function __construct() { ... }
private function getFalconize() { if (!isset($this->falconize)) { $falconizeConfiguration = new FalconizeConfiguration( $this, SymfonyContainer::getInstance()->get('doctrine.dbal.default_connection'), _DB_PREFIX_, _PS_VERSION_ ); $this->falconize = new Falconize($falconizeConfiguration); }
return $this->falconize; } }
Step 4 - Use Falconize
When step 3 is done you are able to use Falconize to handle your installation and uninstallation process. Falconize will automatically register/unregister hooks and create/delete database tables. In case of an installation/uninstallation error, Falconize will throw an exception providing information about error.
``<code>php <?php
...
class MyModule extends Module implements PrestaShopModuleInterface { ...
public function install() { return parent::install() && $this->getFalconize()->install(); }
public function uninstall() { return parent::uninstall() && $this->getFalconize()->uninstall(); } } <pre><code>
Usage during upgrade process
When you would like to upgrade your module you are able to use Falconize to handle your upgrade process. Falconize will automatically register/unregister hooks and upgrade your database schema based on your updated configuration file. If you change column type, add new column or remove column, add index etc. in your configuration file, Falconize will automatically upgrade your database schema on </code>Falconize::install()<code> call.
</code></pre>php <?php
function upgrade_module_1_1_0($module) // 1.1.0 is example version { return $module->getFalconize()->install(); } <pre><code>
Configuration yaml file
Configuration yaml file contains two main sections: </code>database_tables<code> and </code>hooks<code>.
Database tables: </code>database_tables<code> (</code>array<code>)
</code>database_tables<code> section contains information about database tables that you want to create. Schema is based on Doctrine DBAL schema.
Table name (</code>string<code>)
Table name is a key of </code>database_tables<code> section. It has to be unique. It only accepts alphanumeric characters and underscore.
Example: </code></pre>yaml database_tables: my_table_name: ... <pre><code> &gt; [!IMPORTANT] &gt; Order of tables is important. It&#039;s the same as order of execution.
Columns: </code>columns<code> (</code>array<code>)
Columns section contains information about table columns. It is an array of columns. Each column has to have </code>name<code> and </code>type<code> keys. Other keys are optional.
Example:
</code></pre>yaml database_tables: my_table_name: columns:
...
... <pre><code> ##### Column name: </code>name<code> (</code>string<code>)
Column name is a key of column section. It has to be unique. It only accepts alphanumeric characters and underscore.
Example:
</code></pre>yaml
database_tables: my_table_name: columns:
... <pre><code> ##### Column type: </code>type<code> (</code>string<code>)
Column type is a key of column section. It has to be valid database column type. You can find all available types here.
Example:
</code></pre>yaml database_tables: my_table_name: columns:
type: integer ... <pre><code> ##### Column length </code>length<code> (</code>integer<code>) - optional
Column length is a key of column section. It has to be valid column length.
Example:
</code></pre>yaml database_tables: my_table_name: columns:
... length: 11 ... <pre><code> ##### Column notnull: </code>notnull<code> (</code>boolean<code>) - optional
Column notnull is a key of column section. It has to be boolean.
Example:
</code></pre>yaml database_tables: my_table_name: columns:
... notnull: true ... <pre><code> ##### Column autoincrement: </code>autoincrement<code> (</code>boolean<code>) - optional
Column autoincrement is a key of column section. It has to be boolean.
Example:
</code></pre>yaml database_tables: my_table_name: columns:
... autoincrement: true ... <pre><code> ##### Column default: </code>default<code> (</code>mixed<code>) - optional
Column default is a key of column section. It has to be valid column default value.
Example:
</code></pre>yaml database_tables: my_table_name: columns:
... default: 0 ... <pre><code>
Primary: </code>primary<code> (</code>array<code>) - optional
Primary section contains information about primary key. It is an array of column names.
Example:
</code></pre>yaml database_tables: my_table_name: columns:
... primary:
<pre><code>
Indexes: </code>indexes<code> (</code>array<code>) - optional
Indexes section contains information about indexes. It is an array of indexes. Each index has to have </code>name<code> and </code>columns<code> keys.
Index name: </code>name<code> (</code>string<code>)
Index name is a key of index section. It has to be unique. It only accepts alphanumeric characters and underscore.
Index columns: </code>columns<code> (</code>array<code>)
Index columns is a key of index section. It has to be an array of column names.
Example:
</code></pre>yaml database_tables: my_table_name: columns:
... indexes:
- name: my_table_name_active_index
columns:
<pre><code>
Constraint keys: </code>constraint_keys<code> (</code>array<code>) - optional
Constraint keys section contains information about constraint keys. It is an array of constraint keys. Each constraint key has to have </code>name<code>, </code>foreign_table<code>, </code>update<code>, </code>delete<code>, </code>local_columns<code> and </code>foreign_columns<code> keys.
Constraint key name: </code>name<code> (</code>string<code>)
Constraint key name is a key of constraint key section. It has to be unique. It only accepts alphanumeric characters and underscore.
Constraint key foreign table: </code>foreign_table<code> (</code>string<code>)
Constraint key foreign table is a key of constraint key section. It has to be valid table name.
Constraint key update: </code>update<code> (</code>string<code>)
Constraint key update is a key of constraint key section. It has to be valid constraint key update value. You can find all available values </code>'NO ACTION'<code>, </code>'CASCADE'<code>, </code>'SET NULL'<code>, </code>'RESTRICT'<code>, </code>'SET DEFAULT'<code>.
Constraint key delete: </code>delete<code> (</code>string<code>)
Constraint key delete is a key of constraint key section. It has to be valid constraint key delete value. You can find all available values </code>'NO ACTION'<code>, </code>'CASCADE'<code>, </code>'SET NULL'<code>, </code>'RESTRICT'<code>, </code>'SET DEFAULT'<code>.
Constraint key local columns: </code>local_columns<code> (</code>array<code>)
Constraint key local columns is a key of constraint key section. It has to be an array of column names.
Constraint key foreign columns: </code>foreign_columns<code> (</code>array<code>)
Constraint key foreign columns is a key of constraint key section. It has to be an array of column names.
Example:
</code></pre>yaml database_tables: my_table_name_lang: columns:
... constraint_keys:
- name: my_table_name_lang_constraint_lang
foreign_table: lang update: NO ACTION delete: CASCADE local_columns:
foreign_columns:
<pre><code>
Hooks: </code>hooks<code> (</code>array<code>)
Hooks section contains information about hooks that you want to register/unregister.
Register: </code>register<code> (</code>array<code>)
Register section contains information about hooks that you want to register. It is an array of hooks.
Unregister: </code>unregister<code> (</code>array<code>)
Unregister section contains information about hooks that you want to unregister. It is an array of hooks.
Hook name: </code>name<code> (</code>string<code>)
Hook name is a key of </code>register<code>/</code>unregister<code> section. It has to be valid hook name.
Hook version </code>version<code> (</code>string<code>) - optional
Hook version is a key of </code>register<code>/</code>unregister<code> section. It&#039;s PrestaShop version from which hook should be registered/unregistered based on compare operator.
Hook compare operator </code>compare_operator<code> (</code>string<code>) - optional
Hook compare operator is a key of </code>register<code>/</code>unregister<code> section. It has to be valid hook compare operator. You can find all available values </code>'<'<code>, </code>'>'<code>, </code>'='<code>, </code>'<='<code>, </code>'>='<code>.
Example:
</code></pre>yaml hooks: register:
- name: displayHome
- name: displayModernHook
version: '8.0.0' compare_operator: '>='
version: '8.0.0' compare_operator: '<' unregister:
<pre><code>
Splitting configuration file
You can split your configuration file into multiple files. It might be useful when your .yml file is getting bigger and bigger.
You can create multiple configuration files and then import them into main configuration file.
Example:
File: </code>my_table_name.yml<code> </code></pre>yaml database_tables: my_table_name: columns:
type: integer length: 11 notnull: true autoincrement: true
type: boolean notnull: true default: 0 primary:
indexes:
- name: my_table_name_active_index
columns:
<pre><code> File: </code>my_table_name_lang.yml<code> </code></pre>yaml my_table_name_lang: columns:
type: integer length: 11 notnull: true
type: integer length: 11 notnull: true
type: string length: 255 notnull: true primary:
constraint_keys:
- name: my_table_name_lang_constraint_lang
foreign_table: lang update: NO ACTION delete: CASCADE local_columns:
foreign_columns:
<pre><code> File: </code>hooks.yml<code> </code></pre>yaml hooks: register:
- name: displayHome
- name: displayModernHook
version: '8.0.0' compare_operator: '>='
version: '8.0.0' compare_operator: '<' unregister:
<pre><code> Main configuration file: </code></pre>yaml imports:
- { resource: 'my_table_name.yml' }
- { resource: 'my_table_name_lang.yml' }
- { resource: 'hooks.yml' }
</code>``
> [!IMPORTANT] > Order of imports is important. Configuration files are merged in the same order as they are imported.
Komentarze (0)
Chwilowo nie możesz polubić tej opinii
Zgłoś komentarz
Zgłoszenie wysłane
Twoje zgłoszenie nie może zostać wysłane