SlideShare a Scribd company logo
1 of 25
Download to read offline
Service and context
Building a context-dependent API
By Michał Kurzeja
A particularly challenging project we developed included building a
brandnewAPIfeaturingmethodstoexportdatatopartnerservices.
That meant:
whatever was released could no longer be modified to maintain
	 the integrity of the system
a need to develop an easy way to add new features to the API
doing it all without copying tons of code and in line with the logic
	 of the applications
The answer I eventually came up with was...
...to make the services dependent on the context
Any number- or string-based value could serve as such:
	 API version
	 session parameter
	 user setting
This approach produces code as follows:
With an approach like this:
an object is produced for each class defined for a given
	 context
there is no limit to the number of contexts we can create
	 for each service
we can define dependencies for each context
With service tagging and container compiling, we can use
tags to make every service we wish context-dependent.
For example:
Explanation:
context.sensitive is the tag name
it accepts two arguments: context
	 and alias
alias groups services that perform
	 the same task
we pick a service based on the value
	 of the context parameter (e.g. for
	 the 	processor service retrieved
	 in the a context, the ProcessorA
	 object is created, and for the
	 b context – ProcessorB)
the processor.universal alias returns
	 ProcessorAB for both a and b con-
	text
We need a class that delivers each service when needed.
Let’s call it ContextAwareContainer and start with defining
its interface:
In the next few slides, we will go through an example of a
class that implements the interface
Explanation:
ContextAwareContainer is a service
	 that holds a reference to the con-
	tainer
the inner array $services contains
	 the IDs of services we reference
	through context and alias
the set(...) method shown in the pre-
	 vious slide builds the array
the get(...) method shown in the pre-
	 vious slide returns a service based
	on context and alias passed as
	arguments
Explanation:
the initializable interface prevents
	 more services from being registe-
	 red right after the initialization
The definition of the class as a service:
The last step is to create an object that implements the
CompilerPassInterface interface. It finds the services we
tagged and saves their data in the ContextAwareContainer.
Explanation:
the findTaggedServiceIds(…)
	 method accepts an array
the indices of the array are the IDs
	 of services that have the context.		
	 sensitive tag
the values of the array are the attri-
	 butes of those tags
all the data is used as arguments of
	the set(...) method
At this point we can already use the ContextAwareContainer
to select services based on context.
Let’s further this concept by considering another scenario:
What if we had no choice but to make our service depen-
dent on another service and the class of the dependency
changes with the context? For example:
To achieve this, we have to find a way to pass the alias of
the dependency to the definition of the service. Let’s see
an example.
Service
Context::A Context::B
A
Dependency
B
Dependency
Explanation:
the properties attribute is a multi-
	 dimensional array that accepts
	 any type of value
the dynamic_arguments is an array.
	 Its keys are contexts and its valu-
	 es are an array of aliases
the new setup is as follows: when
	 we retrieve the processor.depen-
	dant service with the a context,
	 it passes a reference to a servi-
	 ce with the processor alias and
	 the a context (that is an object of
	the ProcessorA class) as its de-
	 pendency. However, if the con-
	 text equals b, a service of the
	 ProcessorB class is injected
	 into an object of the
	 ProcessorABDependant class.
We now have to modify the ContextAwareInterface so that
it stores the information on the dynamic dependencies.
Explanation:
we expand the set(…) function so
	 that it accepts the $arguments
	 array that contains our context-
	 related dependencies
we save the aliases in the inner
	 $arguments array as well. now
	 on they can be referenced with 	
	 the ID of the parent service and
	context
Let’s have the service accept the dependencies we defined.
To do so, we have to define an interface. We can call it the
DynamicArgumentsInterface.
The following is an implementation of the definition for the
ProcessorABDependant class:
Now we can modify the get(…) function in the ContextA-
wareContainer, so that it can pass the dependencies we
defined to a service we want to retrieve:
Explanation:
the beginning is the same – we start
	 with retrieving our service of
	choice
if the DynamicArgumentsInterface
	 is implemented, we get all of our
	 dependencies and pass them as
	 an array to the
setDynamicsArguments(...) function
if the DynamicArgumentsInterface
	 is not implemented, the logic
	 remains the same
The final step is to have the container compiler read
all of the dynamic arguments and pass them to the
ContextAwareContainer.
Let’s expand the ContextSensitiveServicesCompilerPass to
reflect this.
Explanation:
to register the calling of the set(…)
	 method, we retrieve the proper-
	ties option of the service that is
	 currently being processed
next, we read its dynamic_arguments
	parameter
we finish by adding it as the last
	 argument of the set(...) function
This is how we go about defining dependencies between
services that automatically adjust to a given context.
Just a bit of effort and a few simple classes allowed us to
create a functionality that expands on what can be achieved
with the Dependency Injection component. The result is
convenient to use and achieves a high level of abstraction.
Find the code from this article in my repo:
I hope you enjoyed the presentation. If you have
any feedback, or you believe that there is a better
way to solve this issue, please contact me at:
michal.kurzeja@polcode.net

More Related Content

More from Polcode

What’s Next for the Web?
What’s Next for the Web?What’s Next for the Web?
What’s Next for the Web?Polcode
 
Book Recommended By Our CTO
Book Recommended By Our CTOBook Recommended By Our CTO
Book Recommended By Our CTOPolcode
 
8 Biggest Web Design Trends For 2018 eCommerce
8 Biggest Web Design Trends For 2018 eCommerce8 Biggest Web Design Trends For 2018 eCommerce
8 Biggest Web Design Trends For 2018 eCommercePolcode
 
World Wide Web today
World Wide Web todayWorld Wide Web today
World Wide Web todayPolcode
 
Wordpress in numbers
Wordpress in numbersWordpress in numbers
Wordpress in numbersPolcode
 
Cryptocurrencies in e-commerce
Cryptocurrencies in e-commerceCryptocurrencies in e-commerce
Cryptocurrencies in e-commercePolcode
 
Why Choose WooCommerce?
Why Choose WooCommerce?Why Choose WooCommerce?
Why Choose WooCommerce?Polcode
 
A guide to vastly improving your eCommerce business by investing nothing more...
A guide to vastly improving your eCommerce business by investing nothing more...A guide to vastly improving your eCommerce business by investing nothing more...
A guide to vastly improving your eCommerce business by investing nothing more...Polcode
 
Boost your conversions by 40% and more with these 10 growth hacking tips!
Boost your conversions by 40% and more with these 10 growth hacking tips!Boost your conversions by 40% and more with these 10 growth hacking tips!
Boost your conversions by 40% and more with these 10 growth hacking tips!Polcode
 
Future web developer, you are going to be tremendously valuable
Future web developer, you are going to be tremendously valuableFuture web developer, you are going to be tremendously valuable
Future web developer, you are going to be tremendously valuablePolcode
 
10 reasons why Symfony is just the right fit for your project
10 reasons why Symfony is just the right fit for your project10 reasons why Symfony is just the right fit for your project
10 reasons why Symfony is just the right fit for your projectPolcode
 
Free, SaaS or Enterprise? You’re asking the wrong question!
Free, SaaS or Enterprise? You’re asking the wrong question!Free, SaaS or Enterprise? You’re asking the wrong question!
Free, SaaS or Enterprise? You’re asking the wrong question!Polcode
 
Improve your web and app development with the Symfony3 framework.
Improve your web and app development with the Symfony3 framework.Improve your web and app development with the Symfony3 framework.
Improve your web and app development with the Symfony3 framework.Polcode
 
Polcode’s (not so) secret recipe for an efficient software development process
Polcode’s (not so) secret recipe for an efficient software development processPolcode’s (not so) secret recipe for an efficient software development process
Polcode’s (not so) secret recipe for an efficient software development processPolcode
 
Multi-threaded web crawler in Ruby
Multi-threaded web crawler in RubyMulti-threaded web crawler in Ruby
Multi-threaded web crawler in RubyPolcode
 
How to become a better developer?
How to become a better developer?How to become a better developer?
How to become a better developer?Polcode
 
Asynchroniczny PHP & komunikacja czasu rzeczywistego z wykorzystaniem websock...
Asynchroniczny PHP & komunikacja czasu rzeczywistego z wykorzystaniem websock...Asynchroniczny PHP & komunikacja czasu rzeczywistego z wykorzystaniem websock...
Asynchroniczny PHP & komunikacja czasu rzeczywistego z wykorzystaniem websock...Polcode
 
Waiting to work in IT only after graduation?
Waiting to work in IT only after graduation?Waiting to work in IT only after graduation?
Waiting to work in IT only after graduation?Polcode
 

More from Polcode (18)

What’s Next for the Web?
What’s Next for the Web?What’s Next for the Web?
What’s Next for the Web?
 
Book Recommended By Our CTO
Book Recommended By Our CTOBook Recommended By Our CTO
Book Recommended By Our CTO
 
8 Biggest Web Design Trends For 2018 eCommerce
8 Biggest Web Design Trends For 2018 eCommerce8 Biggest Web Design Trends For 2018 eCommerce
8 Biggest Web Design Trends For 2018 eCommerce
 
World Wide Web today
World Wide Web todayWorld Wide Web today
World Wide Web today
 
Wordpress in numbers
Wordpress in numbersWordpress in numbers
Wordpress in numbers
 
Cryptocurrencies in e-commerce
Cryptocurrencies in e-commerceCryptocurrencies in e-commerce
Cryptocurrencies in e-commerce
 
Why Choose WooCommerce?
Why Choose WooCommerce?Why Choose WooCommerce?
Why Choose WooCommerce?
 
A guide to vastly improving your eCommerce business by investing nothing more...
A guide to vastly improving your eCommerce business by investing nothing more...A guide to vastly improving your eCommerce business by investing nothing more...
A guide to vastly improving your eCommerce business by investing nothing more...
 
Boost your conversions by 40% and more with these 10 growth hacking tips!
Boost your conversions by 40% and more with these 10 growth hacking tips!Boost your conversions by 40% and more with these 10 growth hacking tips!
Boost your conversions by 40% and more with these 10 growth hacking tips!
 
Future web developer, you are going to be tremendously valuable
Future web developer, you are going to be tremendously valuableFuture web developer, you are going to be tremendously valuable
Future web developer, you are going to be tremendously valuable
 
10 reasons why Symfony is just the right fit for your project
10 reasons why Symfony is just the right fit for your project10 reasons why Symfony is just the right fit for your project
10 reasons why Symfony is just the right fit for your project
 
Free, SaaS or Enterprise? You’re asking the wrong question!
Free, SaaS or Enterprise? You’re asking the wrong question!Free, SaaS or Enterprise? You’re asking the wrong question!
Free, SaaS or Enterprise? You’re asking the wrong question!
 
Improve your web and app development with the Symfony3 framework.
Improve your web and app development with the Symfony3 framework.Improve your web and app development with the Symfony3 framework.
Improve your web and app development with the Symfony3 framework.
 
Polcode’s (not so) secret recipe for an efficient software development process
Polcode’s (not so) secret recipe for an efficient software development processPolcode’s (not so) secret recipe for an efficient software development process
Polcode’s (not so) secret recipe for an efficient software development process
 
Multi-threaded web crawler in Ruby
Multi-threaded web crawler in RubyMulti-threaded web crawler in Ruby
Multi-threaded web crawler in Ruby
 
How to become a better developer?
How to become a better developer?How to become a better developer?
How to become a better developer?
 
Asynchroniczny PHP & komunikacja czasu rzeczywistego z wykorzystaniem websock...
Asynchroniczny PHP & komunikacja czasu rzeczywistego z wykorzystaniem websock...Asynchroniczny PHP & komunikacja czasu rzeczywistego z wykorzystaniem websock...
Asynchroniczny PHP & komunikacja czasu rzeczywistego z wykorzystaniem websock...
 
Waiting to work in IT only after graduation?
Waiting to work in IT only after graduation?Waiting to work in IT only after graduation?
Waiting to work in IT only after graduation?
 

Recently uploaded

Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 

Recently uploaded (20)

Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 

Service and context - building a context-dependent API

  • 1. Service and context Building a context-dependent API By Michał Kurzeja
  • 2. A particularly challenging project we developed included building a brandnewAPIfeaturingmethodstoexportdatatopartnerservices. That meant: whatever was released could no longer be modified to maintain the integrity of the system a need to develop an easy way to add new features to the API doing it all without copying tons of code and in line with the logic of the applications The answer I eventually came up with was...
  • 3. ...to make the services dependent on the context Any number- or string-based value could serve as such: API version session parameter user setting This approach produces code as follows:
  • 4. With an approach like this: an object is produced for each class defined for a given context there is no limit to the number of contexts we can create for each service we can define dependencies for each context
  • 5. With service tagging and container compiling, we can use tags to make every service we wish context-dependent. For example:
  • 6. Explanation: context.sensitive is the tag name it accepts two arguments: context and alias alias groups services that perform the same task we pick a service based on the value of the context parameter (e.g. for the processor service retrieved in the a context, the ProcessorA object is created, and for the b context – ProcessorB) the processor.universal alias returns ProcessorAB for both a and b con- text
  • 7. We need a class that delivers each service when needed. Let’s call it ContextAwareContainer and start with defining its interface:
  • 8. In the next few slides, we will go through an example of a class that implements the interface
  • 9. Explanation: ContextAwareContainer is a service that holds a reference to the con- tainer the inner array $services contains the IDs of services we reference through context and alias the set(...) method shown in the pre- vious slide builds the array the get(...) method shown in the pre- vious slide returns a service based on context and alias passed as arguments
  • 10. Explanation: the initializable interface prevents more services from being registe- red right after the initialization
  • 11. The definition of the class as a service:
  • 12. The last step is to create an object that implements the CompilerPassInterface interface. It finds the services we tagged and saves their data in the ContextAwareContainer.
  • 13. Explanation: the findTaggedServiceIds(…) method accepts an array the indices of the array are the IDs of services that have the context. sensitive tag the values of the array are the attri- butes of those tags all the data is used as arguments of the set(...) method
  • 14. At this point we can already use the ContextAwareContainer to select services based on context. Let’s further this concept by considering another scenario:
  • 15. What if we had no choice but to make our service depen- dent on another service and the class of the dependency changes with the context? For example: To achieve this, we have to find a way to pass the alias of the dependency to the definition of the service. Let’s see an example. Service Context::A Context::B A Dependency B Dependency
  • 16. Explanation: the properties attribute is a multi- dimensional array that accepts any type of value the dynamic_arguments is an array. Its keys are contexts and its valu- es are an array of aliases the new setup is as follows: when we retrieve the processor.depen- dant service with the a context, it passes a reference to a servi- ce with the processor alias and the a context (that is an object of the ProcessorA class) as its de- pendency. However, if the con- text equals b, a service of the ProcessorB class is injected into an object of the ProcessorABDependant class.
  • 17. We now have to modify the ContextAwareInterface so that it stores the information on the dynamic dependencies.
  • 18. Explanation: we expand the set(…) function so that it accepts the $arguments array that contains our context- related dependencies we save the aliases in the inner $arguments array as well. now on they can be referenced with the ID of the parent service and context
  • 19. Let’s have the service accept the dependencies we defined. To do so, we have to define an interface. We can call it the DynamicArgumentsInterface.
  • 20. The following is an implementation of the definition for the ProcessorABDependant class: Now we can modify the get(…) function in the ContextA- wareContainer, so that it can pass the dependencies we defined to a service we want to retrieve:
  • 21. Explanation: the beginning is the same – we start with retrieving our service of choice if the DynamicArgumentsInterface is implemented, we get all of our dependencies and pass them as an array to the setDynamicsArguments(...) function if the DynamicArgumentsInterface is not implemented, the logic remains the same
  • 22. The final step is to have the container compiler read all of the dynamic arguments and pass them to the ContextAwareContainer. Let’s expand the ContextSensitiveServicesCompilerPass to reflect this.
  • 23. Explanation: to register the calling of the set(…) method, we retrieve the proper- ties option of the service that is currently being processed next, we read its dynamic_arguments parameter we finish by adding it as the last argument of the set(...) function
  • 24. This is how we go about defining dependencies between services that automatically adjust to a given context. Just a bit of effort and a few simple classes allowed us to create a functionality that expands on what can be achieved with the Dependency Injection component. The result is convenient to use and achieves a high level of abstraction.
  • 25. Find the code from this article in my repo: I hope you enjoyed the presentation. If you have any feedback, or you believe that there is a better way to solve this issue, please contact me at: michal.kurzeja@polcode.net