Skip to content

Commit

Permalink
Merge branch '5.x' into add-report-api-doc
Browse files Browse the repository at this point in the history
  • Loading branch information
RCheesley authored Jun 13, 2024
2 parents b9305b1 + 2c900c1 commit c85c410
Show file tree
Hide file tree
Showing 15 changed files with 947 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .vale.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
StylesPath = .github/styles
Vocab = Mautic
MinAlertLevel = warning
MinAlertLevel = suggestion
[*.{md,rst}]
BasedOnStyles = Vale, Google, Mautic
120 changes: 120 additions & 0 deletions docs/components/cache.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,124 @@
Cache
#####

Symfony makes heavy use of a filesystem cache. When developing for Mautic, clearing the cache is a regular occurrence. By default, Mautic instances have the cache located in ``var/cache/ENV`` where ``ENV`` is the environment currently accessed - ``dev`` or ``prod``. To rebuild the cache, delete the relevant ``ENV`` folder within the cache directory, or run the Symfony command ``php bin/console cache:clear --env=ENV``. If a specific environment isn't passed to the command via ``--env=ENV``, Mautic uses the ``dev`` environment by default.


.. vale off
In the ``dev`` environment, Mautic doesn't cache translations, views, and assets. However, changes to these files require clearing the cache for them to take effect in the ``prod`` environment. Changes to Mautic config files, Symfony config files, etc., require clearing of the cache regardless of the environment.

.. vale on
The typical rule of thumb is, if Mautic isn't acting as you expect after making changes, try clearing your cache. If you get ``class could not be found`` or ``cannot redeclare class`` errors when using the ``cache:clear`` command, manually delete the ``var/cache/ENV`` folder - replacing ENV with the environment e.g ``dev`` or ``prod`` - then run the command and/or browse to the site to rebuild.

Cache bundle
************

Enables PSR-6 and PSR-16 caching. Check :xref:`Symfony Cache Component`

Namespace versus tag
====================

This bundle introduces tags to the cache. All its adapters are fully tag aware which makes the use of namespace obsolete for daily use.

Previously, if you wanted to keep control on cache section and didn't want to hold the index of all keys to clear, you would have to use namespace.

The main disadvantage of this approach is that Mautic creates a new adapter for each namespace.

From Symfony 3.4, the cache uses tag-aware adapters. If you want to clear all records related to your Bundle or Component, you just need to tag them.

.. code-block:: php
/** @var CacheProvider $cache */
$cache = $this->get('mautic.cache.provider');
/** @var CacheItemInterface $item */
$item = $cache->getItem('test_tagged_Item');
$item->set('yesa!!!');
$item->tag(['firstTag', 'secondTag']);
$item->expiresAfter(20000);
All you need to do now is to clear all tagged items:

.. code-block:: php
$cache->invalidateTags(['firstTag']);
Pools clearing
==============

Removing cache items
--------------------

Cache Pools include methods to delete a cache item, some of them, or all of them. The most common is ``Psr\\Cache\\CacheItemPoolInterface::deleteItem``, which deletes the cache item identified by the given key.

.. code-block:: php
$isDeleted = $cache->deleteItem('user_'.$userId);
Use the ``Psr\\Cache\\CacheItemPoolInterface::deleteItems`` method to delete several cache items simultaneously - it returns true only if all the items have been deleted, even when any or some of them don't exist.

Configuration
-------------

Plugins come preconfigured to utilize filesystem caching.

These are the default settings:

.. code-block:: php
'cache_adapter' => 'mautic.cache.adapter.filesystem',
'cache_prefix' => 'app',
'cache_lifetime' => 86400
They can be overridden in ``local.php`` like this:

.. code-block:: php
'cache_adapter' => 'mautic.cache.adapter.redis',
'cache_prefix' => 'app_cache',
'cache_lifetime' => 86400,
Delivered adapters
------------------
.. vale off
- ``mautic.cache.adapter.filesystem``
- ``mautic.cache.adapter.memcached``

.. code-block:: php
'memcached' => [
'servers' => ['memcached://localhost'],
'options' => [
'compression' => true,
'libketama_compatible' => true,
'serializer' => 'igbinary',
],
],
- ``mautic.cache.adapter.redis``

Redis configuration in ``local.php``:

.. code-block:: php
'redis' => [
'dsn' => 'redis://localhost',
'options' => [
'lazy' => false,
'persistent' => 0,
'persistent_id' => null,
'timeout' => 30,
'read_timeout' => 0,
'retry_interval' => 0,
],
],
In order to use another adapter, just set it up as a service.

Clearing the cache
------------------

The ``cache:clear`` command clears Mautic's cache. Use this command:

.. code-block:: bash
bin/console mautic:cache:clear
4 changes: 4 additions & 0 deletions docs/components/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ To add a custom Form Field, use the ``$event->addFormField($identifier, $paramet
- Required
- string
- View template used to render the ``formType``, for example ``HelloWorldBundle:SubscribedEvents\FormField:customfield.html.php``
* - ``help``
- Optional
- string
- The language string for providing a help text below the form element.
* - ``formTypeOptions``
- Optional
- array
Expand Down
2 changes: 1 addition & 1 deletion docs/form_hooks/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ You can define hooks for multiple Forms using the API name of the Form as follow
.. Note:: Define the Form hooks somewhere in the DOM before the code that loads the Form.

You'll need to view the HTML of the Form to find it's API name. The easiest way to do this is to browse to the Form's details in Mautic then click the "Manual Copy" button. Look for the following in the second box:
You'll need to view the HTML of the Form to find its API name. The easiest way to do this is to browse to the Form details in Mautic then click the 'Self-hosted' button. Look for the following in the second box:

.. code-block:: html

Expand Down
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ There are several ways to support Mautic other than contributing with code.
plugins/data
plugins/translations
plugins/continuous-integration
plugins/from-4-to-5

.. toctree::
:maxdepth: 2
Expand Down Expand Up @@ -145,6 +146,7 @@ There are several ways to support Mautic other than contributing with code.
rest_api/contacts
rest_api/fields
rest_api/notifications
rest_api/point_groups
rest_api/reports
rest_api/text_messages

Expand Down
2 changes: 1 addition & 1 deletion docs/links/doctrine_docs_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

link_name = "Doctrine ORM"
link_text = "Doctrine ORM"
link_url = "https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/index.html"
link_url = "https://www.doctrine-project.org/projects/doctrine-orm/en/2.14/index.html"

link.xref_links.update({link_name: (link_text, link_url)})
2 changes: 1 addition & 1 deletion docs/links/doctrine_docs_orm_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

link_name = "Doctrine ORM annotations"
link_text = "Doctrine ORM annotations"
link_url = "https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/annotations-reference.html"
link_url = "https://www.doctrine-project.org/projects/doctrine-orm/en/2.14/reference/annotations-reference.html"

link.xref_links.update({link_name: (link_text, link_url)})
2 changes: 1 addition & 1 deletion docs/links/doctrine_docs_orm_php_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

link_name = "Doctrine ORM PHP mapping"
link_text = "Doctrine ORM PHP mapping"
link_url = "https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/php-mapping.html#classmetadatabuilder"
link_url = "https://www.doctrine-project.org/projects/doctrine-orm/en/2.14/reference/php-mapping.html#classmetadatabuilder"

link.xref_links.update({link_name: (link_text, link_url)})
7 changes: 7 additions & 0 deletions docs/links/mautic_php_to_twig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import link

link_name = "PHP to Twig migration"
link_text = "Upgrading PHP to Twig templates"
link_url = "https://github.com/mautic/mautic/blob/5.x/UPGRADE-PHP-TO-TWIG-TEMPLATES.md"

link.xref_links.update({link_name: (link_text, link_url)})
7 changes: 7 additions & 0 deletions docs/links/phpstan_baseline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import link

link_name = "PHPSTAN baseline"
link_text = "PHPSTAN baseline"
link_url = "https://phpstan.org/user-guide/baseline"

link.xref_links.update({link_name: (link_text, link_url)})
7 changes: 7 additions & 0 deletions docs/links/symfony_cache_component.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import link

link_name = "Symfony Cache Component"
link_text = "Symfony Cache Component"
link_url = "https://symfony.com/doc/current/components/cache.html"

link.xref_links.update({link_name: (link_text, link_url)})
124 changes: 124 additions & 0 deletions docs/plugins/from-4-to-5.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
Update Plugins for Mautic 5
=======================================

Here is a list of steps that most of the Plugins may need to take to upgrade from Mautic 4 to Mautic 5. You should be able to get through each step, make a commit, move to the next one and once you are at finished you have upgraded your Plugin.

Continuous Integration
-------------------------

If you don't have CI configured, this is the time to do it. This is an optional step but it makes sense to do it at the beginning rather than later. Here's how to get it done: :doc:`/plugins/continuous-integration`.

In your PR add also support for PHP 8.1 and 8.2, and upgrade the Mautic version from 4.4 to 5.1. One more thing is that Mautic 5 have ``local.php`` in ``config/local.php`` instead of ``app/config/local.php`` so update that as well.

Autowiring
-------------

Mautic 5 comes with autowiring of PHP services which means the developer experience is much improved, and the code size is reduced.

There is a great doc already written on this topic: :doc:`/plugins/autowiring` - so refer to that for instructions.

To quickly verify that the wiring of services is complete and configured correctly you may find this command faster than refreshing the browser window:

``rm -rf var/cache && bin/console``

.. note:: Ideally, you should be able to delete the whole ``services`` section from your ``config.php`` file, but do that as a cherry on top once you are sure everything is working as the later steps in this process may yet cause you difficulties.

``config.php`` - controllers
---------------------------

``config.php`` should be much lighter now when all services are gone after autowiring is configured. There is one more thing to verify. The controllers are now defined with a different syntax. Here is an example:

.. code:: diff
- 'controller' => 'CronfigBundle:Cronfig:index',
+ 'controller' => 'MauticPlugin\CronfigBundle\Controller\CronfigController::indexAction'
Symfony 5 is much more explicit. That's a good thing even if it's longer. You don't have to guess what the syntax is. It's basically just standard FQCN (Fully Qualified Class Name) with the full method name behind the 2 colons. You don't even need to call the controller method `*Action` any more.

Rendering views
------------------

As Symfony 5 removed the PHP templating engine, Mautic had to switch to Twig. Your Plugin must also update the any views from PHP to Twig. Here is a helpful resource on how to migrate the ``*.html.php`` files to ``*.html.twig`` files:

:xref:`PHP to Twig migration`

In the controllers, you'll also have to update the view paths like this:

.. code:: diff
- $this->renderView('MauticCoreBundle:Notification:flash_messages.html.php');
+ $this->renderView('@MauticCore/Notification/flash_messages.html.twig');
Running this command is faster than refreshing all the views in the browser. It validates the Twig syntax and can guide you through the process:

``bin/console lint:twig plugins/MyBundle``

.. note:: Update MyBundle with your bundle name.

.. vale off
The Integration class
------------------------

.. vale on
If you went ahead and deleted all services from ``config.php``, you may experience problems if you're using Mautic's Integration classes and interfaces. The inner workings of the IntegrationsBundle expects that your Integration has a service key in a specific format. Mautic 6 aims to improve this, but for now, add an alias to ``services.php``:

.. code:: php
$services->alias('mautic.integration.[MY_INTEGRATION]', \MauticPlugin\[MY_INTEGRATION]Bundle\Integration\[MY_INTEGRATION]Integration::class);
.. note:: Replace `[MY_INTEGRATION]` with your Plugin name.

Compiler passes
------------------

If your Plugin uses a compiler pass, you may have to verify that it works correctly. In many cases you may have to change the service alias with FQCN like so:

.. code:: diff
- ->setDecoratedService('mautic.form.type.email', 'mautic.form.type.email.inner');
+ ->setDecoratedService(EmailType::class, 'mautic.form.type.email.inner')
Getting container in tests
-----------------------------

This one is a quick find and replace:

.. code:: diff
- $handlerStack = self::$container->get('mautic.http.client.mock_handler');
+ $handlerStack = static::getContainer()->get(MockHandler::class);
Notice you can also use FQCN instead of string service keys which is more convenient.

Automated refactoring
------------------------

Your Plugin should be working on Mautic 5 by now. Wouldn't it be great to shorten the code a little more? Mautic 5 uses PHP 8.0+ so can take advantage of the syntax. Rector can upgrade the code for you.

Run ``bin/rector process plugins/MyBundle`` and review the changes.

.. note:: Update MyBundle with your bundle name.

Automated code style
-----------------------

Another great way how to improve your Plugin code base quality is to run the CS Fixer: ``bin/php-cs-fixer fix plugins/MyBundle``.

.. note:: Update MyBundle with your bundle name.

Static analysis
-------------------

PHPSTAN is another amazing tool that detects bugs for you. It's better to run it on the whole codebase including core Mautic, so it's aware of all classes.

Run ``composer phpstan``

If your Plugin has more PHPSTAN errors than you can handle right now, consider using :xref:`PHPSTAN baseline`. It allows you to store your tech debt to a single file and it forces you to write better code from now on. And you can reduce the baseline by small chunks every month to get to 0.

Conclusion
----------

This list of steps is compiled by Mautic Plugin developers for the Mautic Plugin developers. If you find that some common problem isn't addressed here, please add it.
Loading

0 comments on commit c85c410

Please sign in to comment.