[Solved][Help] Config_Text Usage Problem.

Discussion forum for Extension Writers regarding Extension Development.
User avatar
Dark❶
Registered User
Posts: 395
Joined: Mon Jan 15, 2018 1:22 pm
Location: D@rK V0id
Name: Dark❶ [dark1]
Contact:

Re: [Help] Config_Text Usage Problem.

Post by Dark❶ »

GanstaZ wrote: Sun Feb 04, 2018 12:36 pm Here you will get some info you may need => phpBB Docs . And like i mentioned above, use container only when you can't use services normally.
Sorry , I did not get what you were pointing to?
I had read that doc's before making a topic here , still i went through the doc's , are you pointing me to Migration tool for config_text? if yes I have done adding & removing of the values in table.

kindly give exact link to what you want me to read , please, It would be very helpful. :D

thanking you,best regards. :D
Dark❶ [dark1]
User avatar
Dark❶
Registered User
Posts: 395
Joined: Mon Jan 15, 2018 1:22 pm
Location: D@rK V0id
Name: Dark❶ [dark1]
Contact:

Re: [Help] Config_Text Usage Problem.

Post by Dark❶ »

posey wrote: Sun Feb 04, 2018 1:02 pm Yeah, you can change all occurences of $container with $phpbb_container aswell. It's just how you call the variable.
You can also name it $abc, but you want descriptive variable names.

Moreover, if you're doing it like the above way, you might aswell inject the config_text straight away ... :

services.yml

Code: Select all

< your class name >
	class: ........
	arguments:
		- '@service_container'
		- '@config_text'
Then in your event listener, just below where you declare your namespace:
use Symfony\Component\DependencyInjection\ContainerInterface;
Rest of the file:

Code: Select all

	/** @var ContainerInterface */
	protected $container;
	
	/** @var \phpbb\config\db_text */
	protected $config_text;

	public function __construct(ContainerInterface $container, \phpbb\config\db_text $config_text)
	{
		$this->container	= $container;
		$this->config_text	= $config_text;
	}
	
	public function handle()
	{
		$renderer = $this->container->get('text_formatter.renderer');
		
		$some_config_text = $this->config_text->get('some_config_text');
		...
Ho!! I thought $config_text could not be used , hence I asked for $container . :D

And I have made a pseudo code:

Code: Select all

	/** @var ContainerInterface */
	protected $phpbb_container;
	
	/** @var \phpbb\config\db_text */
	protected $config_text;

	public function __construct(ContainerInterface $phpbb_container, \phpbb\config\db_text $config_text) // check this
	{
		$this->phpbb_container	= $phpbb_container; // check this
		$this->config_text	= $config_text;
	}
	
	public function handle()
	{
		$some_config_text = $this->config_text->get('some_config_text');
		...
		...
		...
		$this->config_text->set('some_config_text',$some_config_text); // check this
	}
Kindly check the above code & correct me if I am wrong.

Thanking you , best regards. :D
Dark❶ [dark1]
User avatar
GanstaZ
Registered User
Posts: 1187
Joined: Wed Oct 11, 2017 10:29 pm
Location: GZOverse

Re: [Help] Config_Text Usage Problem.

Post by GanstaZ »

I mean read part about controllers & listeners, you will see in those sections how services are used. In code above you injected container, but it is not needed in files where you can inject services directly. Direct injection is what you did with config text.
You want to use container when you can't get a needed service directly (like in module classes ):

Code: Select all

global $phpbb_container;
$config_text = $phpbb_container->get('confix_text');
Usus est magister optimus! phpBB pre-Triton & latest php environment.
When answer lies in the question, question becomes redundant!
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: [Help] Config_Text Usage Problem.

Post by mrgoldy »

Yeah that's fine,
you'll have to make sure you use the use statement I posted above, otherwise it will throw errors, saying you're injecting a wrong instance.
But if you were just using it to get the $config_text, you might aswell omit it from the file, as you're no longer using/needing it.

Also when adding and getting config text, one usually JSON encodes/decodes it. (json_encode() & json_decode())

so:

Code: Select all

$some_config_text = json_decode($this->config_text->get('some_config_text'));

$this->config_text->set('some_config_text', json_encode($some_config_text));
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
Dark❶
Registered User
Posts: 395
Joined: Mon Jan 15, 2018 1:22 pm
Location: D@rK V0id
Name: Dark❶ [dark1]
Contact:

Re: [Help] Config_Text Usage Problem.

Post by Dark❶ »

GanstaZ wrote: Sun Feb 04, 2018 2:01 pm I mean read part about controllers & listeners, you will see in those sections how services are used. In code above you injected container, but it is not needed in files where you can inject services directly. Direct injection is what you did with config text.
You want to use container when you can't get a needed service directly (like in module classes ):

Code: Select all

global $phpbb_container;
$config_text = $phpbb_container->get('confix_text');
&
posey wrote: Sun Feb 04, 2018 2:02 pm Yeah that's fine,
you'll have to make sure you use the use statement I posted above, otherwise it will throw errors, saying you're injecting a wrong instance.
But if you were just using it to get the $config_text, you might aswell omit it from the file, as you're no longer using/needing it.

Also when adding and getting config text, one usually JSON encodes/decodes it. (json_encode() & json_decode())

so:

Code: Select all

$some_config_text = json_decode($this->config_text->get('some_config_text'));

$this->config_text->set('some_config_text', json_encode($some_config_text));
Thanks to both.

I'll try to implement with this Info and come back to you all , If I get error's or something.

Thanking you, Best regards. :+1:
:D :D :D
Dark❶ [dark1]
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: [Help] Config_Text Usage Problem.

Post by 3Di »

posey wrote: Sun Feb 04, 2018 2:02 pm you'll have to make sure you use the use statement I posted above, otherwise it will throw errors, saying you're injecting a wrong instance.
I believe the right injection in the construct would be
public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $phpbb_container)
shouldn't throw any error, no need to use.. not tested though.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: [Help] Config_Text Usage Problem.

Post by mrgoldy »

phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: [Help] Config_Text Usage Problem.

Post by 3Di »

We were talking about a listener, I will investigate about that use case.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
GanstaZ
Registered User
Posts: 1187
Joined: Wed Oct 11, 2017 10:29 pm
Location: GZOverse

Re: [Help] Config_Text Usage Problem.

Post by GanstaZ »

It doesn't matter how you use it, because both ways are working. It's more about the style how one may want to use it with or without use.
Usus est magister optimus! phpBB pre-Triton & latest php environment.
When answer lies in the question, question becomes redundant!
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28616
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: [Help] Config_Text Usage Problem.

Post by Paul »

You really should not inject the container itself in a listener, but inject the specific dependencies instead.
User avatar
Dark❶
Registered User
Posts: 395
Joined: Mon Jan 15, 2018 1:22 pm
Location: D@rK V0id
Name: Dark❶ [dark1]
Contact:

Re: [Help] Config_Text Usage Problem.

Post by Dark❶ »

3Di wrote: Mon Feb 05, 2018 11:40 pm
posey wrote: Sun Feb 04, 2018 2:02 pm you'll have to make sure you use the use statement I posted above, otherwise it will throw errors, saying you're injecting a wrong instance.
I believe the right injection in the construct would be
public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $phpbb_container)
shouldn't throw any error, no need to use.. not tested though.
Yes!! this is how i like. Thanks. :D
& for this , what should be my services.yml ? :?

PS: This is what I was searching for , in Doc's.... :+1:

&&&&&
Paul wrote: Tue Feb 06, 2018 8:33 am You really should not inject the container itself in a listener, but inject the specific dependencies instead.
Thanks :D , But What if ,
I have 8 to 14 specific dependencies in my Event listener to be Injected ,
Instead I use phpbb_container for all other's , Kind of One dependence to Rule e'm All [LOR reff]
which reduces the construct parameter's size , helps to keep it clean.
like ,

Code: Select all

public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $phpbb_container)
{
		$this->auth		= $phpbb_container->get('auth');
		$this->config		= $phpbb_container->get('config');
		$this->config_text	= $phpbb_container->get('config_text');
		$this->language		= $phpbb_container->get('language');
		$this->phpbb_log	= $phpbb_container->get('log');
		$this->request		= $phpbb_container->get('request');
		$this->template	= $phpbb_container->get('template');
		$this->user		= $phpbb_container->get('user');
}
Now ,
1. Does it harm the performance?
2. Does it Break anything?
3. Does it cause compatibility Issue?
4. Why should not it be used?
5. Is it against the phpBB code Style or ethics?

----------------------------------------------------------
Waiting for your reply , from Both. :D
Thanking you , Best regards. :D
Dark❶ [dark1]
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28616
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: [Help] Config_Text Usage Problem.

Post by Paul »

Well, if you have 14 dependencies you should rethink your class.
User avatar
GanstaZ
Registered User
Posts: 1187
Joined: Wed Oct 11, 2017 10:29 pm
Location: GZOverse

Re: [Help] Config_Text Usage Problem.

Post by GanstaZ »

No.. you should use service definitions & not container. + what is said above.

Method 1:

Code: Select all

use phpbb\auth\auth;
use phpbb\config\config;
...

public function __construct($auth, $config, ...)
{
    $this->auth   = $auth;
    $this->config = $config;
    ...
}
Method 2:

Code: Select all

public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, ...)
{
    $this->auth   = $auth;
    $this->config = $config;
    ...
}
Usus est magister optimus! phpBB pre-Triton & latest php environment.
When answer lies in the question, question becomes redundant!
Post Reply

Return to “Extension Writers Discussion”