# Composer in a CMS

# A Prefixed WorPress Plugin

In the WordPress world, plugins are packages of code that extend the core functionality of WordPress. WordPress plugins are made up of PHP code and can include other assets such as images, CSS, and JavaScript.

By making your plugin, you are extending WordPress. You are building additional functionality on top of what WordPress already offers. For example, you could write a plugin that displays links to your site's ten most recent posts.

To include Composer dependencies and avoid naming conflicts with other plugins, PHP-Prefixer provides the service that prefixes all PHP files with the custom prefix for the project.

In this guide, we will create the Hello Prefixed World Plugin, based on the Hello Dolly (opens new window) plugin. Hello Dolly, one of the first plugins is only 82 lines long. Hello Dolly shows lyrics from the famous song (opens new window) in the WordPress admin. Some CSS is used in the PHP file to control how the lyrics are styled.

To integrate a Composer dependency, install the Laravel illuminate/support (opens new window) library, and use it to show a formatted date before the Hello Dolly lyrics.

Essentially, this is how the modified Hello Dolly plugin looks after the prefixing process with the new PPP namespace:

function hello_dolly() {
	require_once __DIR__.'/vendor/autoload.php';

	$chosen = hello_prefixed_world_get_lyric();
	$lang   = '';
	if ( 'en_' !== substr( get_user_locale(), 0, 3 ) ) {
		$lang = ' lang="en"';
	}

    // The Carbon library reference is prefixed with PPP
	$now = \PPP\Carbon\Carbon::now();
	$formattedDate = $now->toDateTimeString();

	printf(
		'<p id="dolly"><span class="screen-reader-text">%s </span><span dir="ltr"%s>%s // %s</span></p>',
		__( 'Quote from Hello Dolly song, by Jerry Herman:', 'hello-dolly' ),
		$lang,
		$formattedDate,
		$chosen
	);
}

# WordPress plugin basics

In this guide, we follow WordPress's Plugin Handbook / Plugin Basics (opens new window) steps to create the plugin structure.

The source code of the plugin can be found in this repository: https://github.com/PHP-Prefixer/hello-wp-world (opens new window)

# The Composer Schema of the WordPress Plugin

In this version of the Hello Dolly plugin, the plugin shows the date and hour before the lyrics. To do this, the plugin calls on the Carbon (opens new window) library, a dependency of the Laravel illuminate/support (opens new window) library.

The plugin has the following composer.json to declare the project, the illuminate/support library, and the prefixer service configuration:

{
    "name": "php-prefixer/hello-prefixed-world-for-wp",
    "description": "Hello Prefixed World plugin for WordPress. A plugin to showcase the PHP-Prefixer service. Install any library freely. PHP-Prefixer will manage your namespaces.",
    "require": {
        "illuminate/support": "^8.10"
    },
    "extra": {
        "php-prefixer": {
            "project-name": "Hello Prefixed World for WordPress",
            "namespaces-prefix":: "PPP",
            "global-scope-prefix": "PPP_",

            "exclude-paths": [
                "bin/",
                "doctrine/inflector/docs",
                "voku/portable-ascii/build"
            ]
        }
    }
}

In particular, this schema has the attribute excludePaths to help package the plugin for distribution. The excludePaths remove folders that should not be included in the target prefixed plugin (commands, unit tests, library documentation, etc.).

The prefixed project ZIP file will be installed on WordPress once it is processed and available for download.

# A Prefixed Joomla Module

In the Joomla world, modules are lightweight and flexible extensions. They are used for small sections of the page that are generally less complex and can be seen across different components.

To include Composer dependencies and avoid naming conflicts with other modules, PHP-Prefixer provides a way to prefix all PHP files with a custom prefix for the project.

In this guide, we will create the Hello Prefixed World module, based on the documented Hello World! Module (opens new window). This tutorial will explain how to go about creating a simple Hello World! Module. In this tutorial, you will learn how the basic file structure of a module can be modified to include Composer dependencies and how to prefix them.

To integrate a Composer dependency, install the Laravel illuminate/support (opens new window) library and use it below to show a formatted date before the classic "Hello, World!" message.

Essentially, this is how the modified Hello World! Module looks after the prefixing process with the new PPP namespace:

<?php

namespace ModHelloWorld;

// No direct access
defined('_JEXEC') or die;

use PPP\Carbon\Carbon;

class Helper
{
    /**
     * Retrieves the hello message
     *
     * @param   array  $params An object containing the module parameters
     *
     * @access public
     */
    public static function getHello($params)
    {
        $now = Carbon::now();
        $formattedDate = $now->toDateTimeString();

        return $formattedDate.' / Hello, World!';
    }
}
...

# Joomla module basics

In this guide, we follow Joomla's Creating a simple module/Developing a Basic Module (opens new window) steps to create the module structure.

The source code of the module can be found in this repository: https://github.com/PHP-Prefixer/hello-joomla-world (opens new window)

# The Composer Schema of the Joomla Module

In this version of the Hello World Module, the module shows the date and hour before the "Hello, World!" message. To do this, the module calls on the Carbon (opens new window) library, a dependency of the Laravel illuminate/support (opens new window) library.

The module has the following composer.json to declare the project, the illuminate/support library, and the prefixer service configuration:

{
    "name": "php-prefixer/hello-prefixed-world-for-joomla",
    "description": "Hello Prefixed World module for Joomla. A module to showcase the PHP-Prefixer service. Install any library freely. PHP-Prefixer will manage your namespaces.",
    "require": {
        "illuminate/support": "^8.10"
    },
    "extra": {
        "php-prefixer": {
            "project-name": "Hello Prefixed World for Joomla",
            "namespaces-prefix":: "PPP",
            "global-scope-prefix": "PPP_",

            "exclude-paths": [
                "bin/",
                "doctrine/inflector/docs",
                "voku/portable-ascii/build"
            ]
        }
    },
    "autoload": {
        "psr-4": {
            "ModHelloWorld\\": "src/"
        }
    }
}

In particular, this schema has the attribute excludePaths to help package the module for distribution. The excludePaths remove folders that should not be included in the target prefixed module (commands, unit tests, library documentation, etc.).

The prefixed project ZIP file will be ready for installation on Joomla once it is processed and available for downloaded.

# A Prefixed PrestaShop Module

In the PrestaShop world, extensibility revolves around modules, which are small programs that use PrestaShop’s functionalities. These programs change the functionalities or add to them to make PrestaShop easier to use and tailored to the merchant’s needs.

To include Composer dependencies and avoid naming conflicts with other modules, PHP-Prefixer provides a way to prefix all PHP files with a custom prefix for the project.

In this guide, we will create the Hello PrestaShop Products Module, based on the Friends of Presta/products (opens new window) module. The module demonstrates all available customizations in Product pages (e.g., adding a new "alternative description" field in the catalog). Thanks to Friends of Presta/Mickaël Andrieu (opens new window).

The Products already integrates a Composer dependency, the tightenco/collect (opens new window) library, to integrate the Illuminate Collections library, "a fluent, convenient wrapper for working with arrays of data."

This is how the modified Hello PrestaShop Products module looks after the prefixing process with the new PPP namespace:

namespace PsProducts;

use PPP\Tightenco\Collect\Support\Collection;

/**
 * Manage a ProductsCollection instead of a collection of Products array.
 */
class ProductsCollection extends Collection
{
}

# PrestaShop module basics

In this guide, we follow PrestaShop's Chapter 4 - PrestaShop modules / Getting started (opens new window) steps to create the module structure.

The Hello PrestaShop Products Module is based on the Friends of Presta/products (opens new window) module. It demonstrates all the available customizations in product pages (i.e., adding a new "alternative description" field in the catalog), thanks to Friends of Presta/Mickaël Andrieu (opens new window).

The source code of the module can be found in this repository: https://github.com/PHP-Prefixer/hello-prestashop-world (opens new window)

# The Composer Schema of the PrestaShop Module

In this version of the Hello PrestaShop Products module, the module adds a new "alternative description" field in the catalog. In the process, the module uses the tightenco/collect (opens new window) library to integrate the Illuminate Collections library.

The module has the following composer.json to declare the project, the tightenco/collect library, and the prefixer service configuration:

{
    "name": "php-prefixer/hello-prestashop-products",
    "description": "Hello PrestaShop Products module. A module to showcase the PHP-Prefixer service. Install any library freely. PHP-Prefixer will manage your namespaces.",
    "type": "prestashop-module",
    "license": "MIT",
    "require": {
        "php": "^7.1",
        "tightenco/collect": "^5.6"
    },
    "autoload": {
        "psr-4": {
            "PsProducts\\": "src/"
        },
        "exclude-from-classmap": []
    },
    "config": {
        "preferred-install": "dist"
    },
    "extra": {
        "php-prefixer": {
            "project-name": "Hello PrestaShop Products",
            "namespaces-prefix":: "PPP",
            "global-scope-prefix": "PPP_",

            "exclude-paths": [
                "bin/"
            ]
        }
    }
}

In particular, this schema has the attribute excludePaths to help package the module for distribution. The excludePaths remove folders that should not be included in the target prefixed module (commands, unit tests, library documentation, etc.).

The prefixed project ZIP file will be installed on PrestaShop once it is processed and available for download.

::: Tip NOTE: Remember that the module name is helloprestashopproducts. The ZIP to be installed on PrestaShop should contain a folder helloprestashopproducts. Please, prepare the installation ZIP file. Alternatively, you can manually create helloprestashopproducts folder inside the modules folder of the site and copy all the module files into the folder. :::