Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service to use config parameters in custom storage like DB #10357

Closed
wants to merge 19 commits into from

Conversation

kuzmany
Copy link
Member

@kuzmany kuzmany commented Aug 23, 2021

Q A
Branch? "features" for all features, enhancements and bug fixes (until 3.3.0 is released)
Bug fix? yes/no
New feature? yes/no
Deprecations? yes/no
BC breaks? yes/no
Automated tests included? yes/no
Related user documentation PR URL mautic/mautic-documentation#...
Related developer documentation PR URL mautic/developer-documentation#...
Issue(s) addressed Fixes #...

Description:

This PR allow developer create own storage for parameters - by default store in local.php. Some enviroment infrastructure do not allow change it static files due server replication. You can use database or redis or enything else.

Example of service for parameters storage:

            'mautic.parameters.storage.database' => [
                'class'     => \Mautic\CoreBundle\ParametersStorage\Database\DatabaseParametersStorage::class,
                'arguments' => [
                    'mautic.cache.provider',
                ],
                'tag'       => 'mautic.parameters.storage.service',
            ],

Then in in parameters_local.php you set

\Mautic\CoreBundle\ParametersStorage\ParametersStorage::PARAMETERS_STORAGE => 'mautic.parameters.storage.database',

This PR also include Add support for database cache provider + allow use multiple cache provider in code

Cache component (https://developer.mautic.org/#cache-bundle) in Mautic support at the moment filesystem, memcached a redis mechanism. This PR add database cache adapter (https://symfony.com/doc/current/components/cache/adapters/pdo_doctrine_dbal_adapter.html)

Also this allow use multiple cache provider in your code.

$cache = $this->get('mautic.cache.provider');
$item = $cache->getItem('test_tagged_Item');
$item->set('yesa!!!');

and

$cache = $this->get('mautic.cache.provider')->getCacheAdapter('mautic.cache.adapter.db');
$item = $cache->getItem('test_tagged_Item');
$item->set('yesa!!!');

Just setup in parameters_local.php

'cache_adapter' => 'mautic.cache.adapter.db',

You can also change there timeout to never expired data

        'cache_adapter_db'        => [ ],

Steps to test this PR:

  1. Load up this PR

@kuzmany kuzmany added WIP PR's that are not ready for review and are currently in progress enhancement Any improvement to an existing feature or functionality labels Aug 23, 2021
@cla-bot cla-bot bot added the cla-signed The PR contributors have signed the contributors agreement label Aug 23, 2021
@codecov
Copy link

codecov bot commented Aug 23, 2021

Codecov Report

Merging #10357 (d3bd064) into 4.x (21430d8) will increase coverage by 0.01%.
The diff coverage is 93.02%.

❗ Current head d3bd064 differs from pull request most recent head 1db5f68. Consider uploading reports for the commit 1db5f68 to get more accurate results
Impacted file tree graph

@@             Coverage Diff              @@
##                4.x   #10357      +/-   ##
============================================
+ Coverage     42.14%   42.16%   +0.01%     
- Complexity    34533    34545      +12     
============================================
  Files          2062     2065       +3     
  Lines        111560   111584      +24     
============================================
+ Hits          47021    47045      +24     
  Misses        64539    64539              
Impacted Files Coverage Δ
...ndencyInjection/Compiler/ParametersStoragePass.php 83.33% <83.33%> (ø)
...CoreBundle/ParametersStorage/ParametersStorage.php 90.00% <90.00%> (ø)
...ParametersStorage/Local/LocalParametersStorage.php 91.66% <91.66%> (ø)
...ndles/ConfigBundle/Controller/ConfigController.php 57.60% <100.00%> (-0.61%) ⬇️
...Bundle/DependencyInjection/MauticCoreExtension.php 91.82% <100.00%> (+0.21%) ⬆️
app/bundles/CoreBundle/MauticCoreBundle.php 100.00% <100.00%> (ø)

@RCheesley RCheesley marked this pull request as draft August 24, 2021 16:57
@kuzmany kuzmany changed the title Add services to use cofnig parameters in storage like DB Add services to use config parameters in custom storage like DB Aug 26, 2021
@RCheesley RCheesley changed the base branch from features to 4.x August 30, 2021 17:52
@RCheesley
Copy link
Sponsor Member

@kuzmany just updated the base for you to 4.x as it's a feature PR.

@kuzmany kuzmany changed the title Add services to use config parameters in custom storage like DB Add service to use config parameters in custom storage like DB Oct 6, 2021
Copy link
Contributor

@alanhartless alanhartless left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kuzmany I made some comments just by reading the code. I have not tested it. It needs code coverage for the new services and that existing services still work as expected.


parent::__construct(
new \Symfony\Component\Cache\Adapter\PdoAdapter($connection, (string) $namespace, $lifetime, $options),
new \Symfony\Component\Cache\Adapter\PdoAdapter($connection, (string) $namespace, $lifetime, $options)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to duplicate this?

{
if (is_null($this->psr16)) {
$this->psr16 = new Psr6Cache($this->getCacheAdapter());
$this->psr16 = new Psr6Cache($this->getCacheAdapter($cacheAdapter));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will cause issues when the cache provider is used for caching (i.e. Redis) and the database (for config) because first to call the method wins resulting subsequent to use something other than the cache adaptor configured.

Consider using a complete new service for this instead of sharing the same as the cache provider. DatabaseParametersStorage could potentially use the same class but should be defined as a new/separate service.

'mautic.parameters.storage.database' => [
'class' => \Mautic\CoreBundle\ParametersStorage\Database\DatabaseParametersStorage::class,
'arguments' => [
'mautic.cache.provider',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment above about using a new service instead of sharing mautic.cache.provider


public function write(array $parameters): void
{
$this->simpleCache->set(ParametersStorage::PARAMETERS_STORAGE, $parameters);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm torn on this. I see the benefit of quicker coding by using the cache provider to do this but I also think it's a bit risky because important configuration options will be stored in the same location as cached items. It'll need to be called out that using the DB to store parameters means you cannot reset or truncate the database cache blindly. If using a different service than the shared mautic.cache.provider, maybe you could use a new table just for config parameters as well to help protect from that scenario?

Copy link
Member Author

@kuzmany kuzmany Oct 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alanhartless thank you for your comments. Do you mean use cache service with store to another table like cache_items or do you mean create new table as part of Mautic?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new table that stores just config values that doesn't get tangled up in other cached items because the mautic.cache.provider service is shared for both.

// if db_driver and mailer_from_name are present then it is assumed all the steps of the installation have been
// performed; manually deleting these values or deleting the config file will be required to re-enter
// installation.
if (empty($params['db_driver']) || empty($params['mailer_from_name'])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this check moved somewhere else? If not, then was the installer tested with the default file based config? It looks like this method is used in a few places so I'm not sure of the full impact of removing this nor if there is really any value in having it now.

@RCheesley RCheesley added the needs-rebase PR's that need to be rebased label Aug 4, 2023
@kuzmany
Copy link
Member Author

kuzmany commented Feb 14, 2024

Close, we need release M5 version of the solution

@kuzmany kuzmany closed this Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The PR contributors have signed the contributors agreement enhancement Any improvement to an existing feature or functionality needs-rebase PR's that need to be rebased WIP PR's that are not ready for review and are currently in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants