Skip to content

Latest commit

 

History

History
387 lines (315 loc) · 11 KB

README.md

File metadata and controls

387 lines (315 loc) · 11 KB

ctasca/mage-bundle

License PHP Version Require

Easily create Magento2/AdobeCommerce PHP/XML/JS files from a set of templates via the command-line.

Allows you to define your own templates as well as the data provided at files' creation time.

#1 Installation

composer require --dev ctasca/mage-bundle

#2 Copy template files to Magento root dev/ directory

cd <magento_root>/vendor/ctasca/mage-bundle && composer run-script post-install-cmd

After executing the above command, check that dev/ directory of your Magento installation contains a mage-bundle/ directory.

Enable the module

bin/magento module:enable Ctasca_MageBundle

Run setup:upgrade command

bin/magento setup:upgrade

Run setup:di:compile command (optional)

If Magento is running in production mode you will need to also run:

bin/magento setup:di:compile

Available commands

bin/magento magebundle:module:create

Shortcut

bin/magento m:modu:c

Creates a skeleton Magento module in app/code directory, generating the required registration.php and etc/module.xml files


bin/magento magebundle:controller:create

Shortcut

bin/magento m:cont:c

Creates a Controller namespace and Action class in specified module. Developer is prompted to choose a router (either standard or admin), and an Action template from the ones available in mage-bundle/http-controller or mage-bundle/adminhtml-http-controller directories.


bin/magento magebundle:etc:xml:create

Shortcut

bin/magento m:e:x:c

Creates an XML file in Company/Module/etc directory. Templates can be chosen after specifying the area where the template applies to.


bin/magento magebundle:model:set:create

Shortcut

bin/magento m:m:s:c

Creates a Model, Resource Model and Collection classes in specified Company/Module.


bin/magento magebundle:repository:create

Shortcut

bin/magento m:r:c

Creates all the required class for a Repository in specified Company/Module.

IMPORTANT: In order to create a repository a model implementing an interface must exist.

If you need a Repository when creating a model-set with the command magebundle:model:set:create you can choose a template that will also create a Model implementing an interface

This command creates the following:

  • An API Interface for the repository
  • An API Data Interface for the model (if it doesn't exist)
  • An API Data Search Result Interface
  • The repository Model implementing the repository Interface

Do not forget to add the preferences to your di.xml for the repository classes once created. For example:

<config>
    <preference for="Company\Module\Api\Data\$MODEL_NAMEInterface" type="Company\Module\Model\$MODEL_NAME" />
    <preference for="Company\Module\Api\$MODEL_NAMERepositoryInterface" type="Company\Module\Model\$MODEL_NAMERepository" />
    <preference for="Company\Module\Api\Data\$MODEL_NAMESearchResultInterface" type="Company\Module\Model\$MODEL_NAMESearchResult" />
</config>

bin/magento magebundle:model:create

Shortcut

bin/magento m:mode:c

Creates a Model class in specified Company/Module. There is also the template to create an interface instead of a class.


bin/magento magebundle:block:create

Shortcut

bin/magento m:b:c

Creates a template Block class in specified Company/Module.


bin/magento magebundle:helper:create

Shortcut

bin/magento m:h:c

Creates a Helper class in specified Company/Module.


bin/magento magebundle:customer-data:create

Shortcut

bin/magento m:cu:c

Creates a CustomerData class in specified Company/Module.


bin/magento magebundle:view-model:create

Shortcut

bin/magento m:v:c

Creates a View Model class in specified Company/Module.


bin/magento magebundle:observer:create

Shortcut

bin/magento m:o:c

Creates an Observer class in specified Company/Module.


bin/magento magebundle:plugin:create

Shortcut

bin/magento m:p:c

Creates a Plugin class in specified Company/Module.


bin/magento magebundle:cron:create

Shortcut

bin/magento m:cr:c

Creates a Cron class in specified Company/Module.


bin/magento magebundle:console-command:create

Shortcut

bin/magento m:cons:c

Creates a Console Command class in specified Company/Module.


bin/magento magebundle:data-patch:create

Shortcut

bin/magento m:d:c

Creates a Setup Data Patch class in specified Company/Module.


bin/magento magebundle:schema-patch:create

Shortcut

bin/magento m:s:c

Creates a Setup Schema Patch class in specified Company/Module.


bin/magento magebundle:api-interface:create

Shortcut

bin/magento m:a:c

Creates an API interface in specified Company/Module. Templates can be chosen after specifying the area where the template applies to.

For functional API interfaces, the generated file will be created in the Company/Module/Api directory

For data API interfaces, the generated file will be created in the Company/Module/Api/Data directory


bin/magento magebundle:jquery-widget:create

Shortcut

bin/magento m:j:c

Creates a JQuery widget file in specified Company/Module. JS file will be created in the specified module's view/$AREA/web/js directory.


bin/magento magebundle:ui-component:create

Shortcut

bin/magento m:u:c

Creates an Ui Component JS file in specified Company/Module. JS file will be created in the specified module's view/$AREA/web/js directory.


bin/magento magebundle:logger:create

Shortcut

bin/magento m:l:c

Creates a Logger Handler and Logger classes files in specified Company/Module.

Log filename can be specified when executing this command.


bin/magento magebundle:js:mixin:create

Shortcut

bin/magento m:j:m:c

Creates a JS mixin file in specified Company/Module. JS file will be created in the specified module's view/$AREA/web/js directory.


bin/magento magebundle:exception:create

Shortcut

bin/magento m:e:c

Creates an Exception class in specified Company/Module.

Utilities commands

bin/magento magebundle:util:encryptor

Shortcut

bin/magento m:u:e

Encrypts/Decrypts a string using Magento crypt key

Setting custom working directory

By default, MageBundle creates files in the app/code directory and in the specified module's namespace

It is possible, to change this behaviour by creating a json file named pwd.json in the $MAGENTO-ROOT/dev/mage-bundle directory and specifying the directory (relative to magento root) where files will be created when executing MageBundle commands.

This can be useful, for example, when developing a module which is not located in app/code directory

Important:

The directory specified in pwd.json must end with a forward-slash

pwd.json example:

{
  "pwd" : "packages/my-package/"
}

About template files:

Template files are written in PHP version 8.1.

For example the http-get-action-json-result.tpl.php contains the following

{{php}}
declare(strict_types=1);

namespace {{namespace}};

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\Result\Json;

class {{class_name}} implements HttpGetActionInterface
{
    /**
     * @param RequestInterface $request
     * @param JsonFactory $jsonFactory
     */
    public function __construct(
        private readonly RequestInterface $request,
        private readonly JsonFactory $jsonFactory
    ){}

    /**
     * @return Json
     */
    public function execute(): Json
    {
        $jsonResponse = $this->jsonFactory->create();
        return $jsonResponse->setData([]);
    }
}

To set your own templates just place them in the MAGENTO_ROOT/dev/mage-bundle/$TEMPLATES_DIRECTORY/ directory and choose the template when executing the magebundle create commands.

Note: When executing the magebundle:etc:xml:create command, files are generated with the same name as the template file.
To create your own templates, use the filename for the xml file to be generated appending __$STRING to the template filename.
For example to define your own global acl.xml template, create, for example, the template file naming like so: acl__custom.tpl.xml. Then simply place it in the dev/mage-bundle/etc/global directory and select it when executing the create command.

IMPORTANT: If a filename already exists in a module's directory, the create command will not be executed and an error is output to the console.
This is to prevent overwriting an existing file.

For a list of the templates defined within the module go to...

The repository wiki page

Templates Data Provider

It is possible to define your own templates as well as the data that are passed when these are generated.

In order to do so, simply create a JSON file in the MAGENTO_ROOT/dev/mage-bundle/custom-data/#path-to-template# directory, naming the file exactly as the template file that is being generated and defining a JSON Object with setter methods as keys and their corresponding values.

As an example, for XML files generated in Company/Module/etc directories, custom data should be stored in MAGENTO_ROOT/dev/mage-bundle/custom-data/etc/#area#/#template_name#.tpl.json

Example

{
  "setTestNamespace" : "\\Ctasca\\MageBundle\\Test",
  "setCustomDataArray" : ["First Value", "Second Value"]
}

After creating this JSON file, it will be possible to use the placeholder {{test_namespace}} in a template file.

As setCustomDataArray provides an array, this will be imploded with PHP_EOL separator. To use it in your template files you would use the placeholder: {{custom_data_array}}