Page 1 of 1

Sharing helper class with acp and ucp modules

Posted: Fri Feb 05, 2016 4:41 pm
by pierredu
Hello,

Following some pointers here and some sample code in extensions, I could write a helper class used by two code modules of my extension.

Is there a simple solution to also share some code with the ACP adn UCP modules ?

More, is there a way to do the same from within the migration file? There is not much code, maybe 10-20 lines, but I don't like to have code duplicated this way, because of the error risk if I have to change something.

Thanks in advance.

Re: Sharing helper class with acp and ucp modules

Posted: Fri Feb 05, 2016 5:17 pm
by gn#36
For ACP and UCP modules, you can simply request your helper class from the phpbb_container:

Code: Select all

global $phpbb_container;
$your_class = $phpbb_container->get('vendor.extname.your.custom.name.from.service.yml');
$your_class->run_code();
 
You have to define a service named vendor.extname.your.custom.name.from.service.yml in the config/service.yml file of your extension to use this.

For migration files, you could also use container aware migrations (that would probably be useful, if you potentially use your code also outside of migration files), or you create an (potentially abstract) base migration and extend that base migration by the migrations that actually do stuff and use your custom function.

Examples of such abstract migrations are found in the folder phpbb\db\migration, the container aware migration base is one of them. If you have code that is also used outside migrations, you simply extend that migration file and you will be able to use $your_class = $this->container->get('vendor.extname.your.custom.name.from.service.yml'); to get your service and use it inside a custom function (and of course, it also has to be defined in your service.yml). See here for a similar example.

You can also combine the two by creating a base migration that extends the container aware one and base your real migrations on that new base file.

Re: Sharing helper class with acp and ucp modules

Posted: Fri Feb 05, 2016 7:46 pm
by pierredu
Hello,

Thanks a lot for your comments. I'll look at this tomorrow and will reports what I've found.

Re: Sharing helper class with acp and ucp modules

Posted: Sat Feb 06, 2016 9:52 am
by pierredu
I'm making progress, but...
gn#36 wrote:You have to define a service named vendor.extname.your.custom.name.from.service.yml in the config/service.yml file of your extension to use this.
In my config/service.yml file, i defined

Code: Select all

     lmdi.gloss.core.helper:
        class: lmdi\gloss\core\helper
        arguments:
            - '@dbal.conn'
            - '%lmdi.gloss.tables.glossary%'
but I still get an error The service definition "lmdi.gloss.core.helper" does not exist.

Thanks in advance.

Re: Sharing helper class with acp and ucp modules

Posted: Sat Feb 06, 2016 10:30 am
by gn#36
Is your extension installed? Tried emptying the cache?

Re: Sharing helper class with acp and ucp modules

Posted: Sat Feb 06, 2016 4:12 pm
by pierredu
I'll check again.
Never sure to be safe.