Zdjęcie niedostępne

falconize

Falconize is a simple tool designed to help you create a database structure and register hooks in PrestaShop modules

Developed by Oksydan

Get This Module Free on GitHub
License: MIT Status: Unknown Health: Very Outdated

Info updated 1 month ago

Sources
Stars: 9 Forks: 3 Last commit: Feb 20, 2024
Type
Module

Info checked: Mar 2, 2026

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-&gt;module = $module; $this-&gt;connection = $connection; $this-&gt;databasePrefix = $databasePrefix; $this-&gt;prestashopVersion = $prestashopVersion; }

public function getConnection(): Connection { return $this-&gt;connection; }

public function getModule(): PrestaShopModuleInterface { return $this-&gt;module; }

public function getConfigurationFile(): \SplFileInfo { // /../../config/configuration.yml is just an example path to configuration file return new \SplFileInfo(DIR . &#039;/../../config/configuration.yml&#039;); }

public function getDatabasePrefix(): string { return $this-&gt;databasePrefix; }

public function getPrestashopVersion(): string { return $this-&gt;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: &#039;8.0.0&#039; compare_operator: &#039;&gt;=&#039;

name: displayLegacyHook

version: &#039;8.0.0&#039; compare_operator: &#039;&lt;&#039; 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-&gt;falconize)) { $falconizeConfiguration = new FalconizeConfiguration( $this, SymfonyContainer::getInstance()-&gt;get(&#039;doctrine.dbal.default_connection&#039;), _DB_PREFIX_, _PS_VERSION_ ); $this-&gt;falconize = new Falconize($falconizeConfiguration); }

return $this-&gt;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 &lt;?php

...

class MyModule extends Module implements PrestaShopModuleInterface { ...

public function install() { return parent::install() &amp;&amp; $this-&gt;getFalconize()-&gt;install(); }

public function uninstall() { return parent::uninstall() &amp;&amp; $this-&gt;getFalconize()-&gt;uninstall(); } } &lt;pre&gt;&lt;code&gt;

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.

&lt;/code&gt;&lt;/pre&gt;php &lt;?php

function upgrade_module_1_1_0($module) // 1.1.0 is example version { return $module-&gt;getFalconize()-&gt;install(); } &lt;pre&gt;&lt;code&gt;

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: &lt;/code&gt;&lt;/pre&gt;yaml database_tables: my_table_name: ... &lt;pre&gt;&lt;code&gt; &amp;gt; [!IMPORTANT] &amp;gt; Order of tables is important. It&amp;#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:

&lt;/code&gt;&lt;/pre&gt;yaml database_tables: my_table_name: columns:

  • name: id_my_table_name

...

  • name: active

... &lt;pre&gt;&lt;code&gt; ##### 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:

&lt;/code&gt;&lt;/pre&gt;yaml

database_tables: my_table_name: columns:

  • name: id_my_table_name

... &lt;pre&gt;&lt;code&gt; ##### 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:

&lt;/code&gt;&lt;/pre&gt;yaml database_tables: my_table_name: columns:

  • name: id_my_table_name

type: integer ... &lt;pre&gt;&lt;code&gt; ##### 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:

&lt;/code&gt;&lt;/pre&gt;yaml database_tables: my_table_name: columns:

  • name: id_my_table_name

... length: 11 ... &lt;pre&gt;&lt;code&gt; ##### Column notnull: </code>notnull<code> (</code>boolean<code>) - optional

Column notnull is a key of column section. It has to be boolean.

Example:

&lt;/code&gt;&lt;/pre&gt;yaml database_tables: my_table_name: columns:

  • name: id_my_table_name

... notnull: true ... &lt;pre&gt;&lt;code&gt; ##### Column autoincrement: </code>autoincrement<code> (</code>boolean<code>) - optional

Column autoincrement is a key of column section. It has to be boolean.

Example:

&lt;/code&gt;&lt;/pre&gt;yaml database_tables: my_table_name: columns:

  • name: id_my_table_name

... autoincrement: true ... &lt;pre&gt;&lt;code&gt; ##### 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:

&lt;/code&gt;&lt;/pre&gt;yaml database_tables: my_table_name: columns:

  • name: active

... default: 0 ... &lt;pre&gt;&lt;code&gt;

Primary: </code>primary<code> (</code>array<code>) - optional

Primary section contains information about primary key. It is an array of column names.

Example:

&lt;/code&gt;&lt;/pre&gt;yaml database_tables: my_table_name: columns:

  • name: id_my_table_name

... primary:

  • id_my_table_name

&lt;pre&gt;&lt;code&gt;

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:

&lt;/code&gt;&lt;/pre&gt;yaml database_tables: my_table_name: columns:

  • name: active

... indexes:

  • name: my_table_name_active_index

columns:

  • active

&lt;pre&gt;&lt;code&gt;

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>&#039;NO ACTION&#039;<code>, </code>&#039;CASCADE&#039;<code>, </code>&#039;SET NULL&#039;<code>, </code>&#039;RESTRICT&#039;<code>, </code>&#039;SET DEFAULT&#039;<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>&#039;NO ACTION&#039;<code>, </code>&#039;CASCADE&#039;<code>, </code>&#039;SET NULL&#039;<code>, </code>&#039;RESTRICT&#039;<code>, </code>&#039;SET DEFAULT&#039;<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:

&lt;/code&gt;&lt;/pre&gt;yaml database_tables: my_table_name_lang: columns:

  • 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

&lt;pre&gt;&lt;code&gt;

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&amp;#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>&#039;&lt;&#039;<code>, </code>&#039;&gt;&#039;<code>, </code>&#039;=&#039;<code>, </code>&#039;&lt;=&#039;<code>, </code>&#039;&gt;=&#039;<code>.

Example:

&lt;/code&gt;&lt;/pre&gt;yaml hooks: register:

  • name: displayHome
  • name: displayModernHook

version: &#039;8.0.0&#039; compare_operator: &#039;&gt;=&#039;

  • name: displayLegacyHook

version: &#039;8.0.0&#039; compare_operator: &#039;&lt;&#039; unregister:

  • name: displayMyOldHook

&lt;pre&gt;&lt;code&gt;

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> &lt;/code&gt;&lt;/pre&gt;yaml 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

&lt;pre&gt;&lt;code&gt; File: </code>my_table_name_lang.yml<code> &lt;/code&gt;&lt;/pre&gt;yaml 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

&lt;pre&gt;&lt;code&gt; File: </code>hooks.yml<code> &lt;/code&gt;&lt;/pre&gt;yaml hooks: register:

  • name: displayHome
  • name: displayModernHook

version: &#039;8.0.0&#039; compare_operator: &#039;&gt;=&#039;

  • name: displayLegacyHook

version: &#039;8.0.0&#039; compare_operator: &#039;&lt;&#039; unregister:

  • name: displayMyOldHook

&lt;pre&gt;&lt;code&gt; Main configuration file: &lt;/code&gt;&lt;/pre&gt;yaml imports:

  • { resource: &#039;my_table_name.yml&#039; }
  • { resource: &#039;my_table_name_lang.yml&#039; }
  • { resource: &#039;hooks.yml&#039; }

</code>``

> [!IMPORTANT] > Order of imports is important. Configuration files are merged in the same order as they are imported.

  • Indeks
    falconize

  • License
    MIT
  • Status
    Unknown

Komentarze (0)

Na razie nie dodano żadnej recenzji.
Ładowanie...