Code Monkey home page Code Monkey logo

contextdi's Introduction

ContextDI

Allows for context dependency injection.

Concept

The concept is that there is often the need to know the context of the request. A context can be made up of one or more different facets such as User, Customer, Domain, GeoLocation, User IP, The presence of an outstanding quote, etc

The bundle builds on the existing Symfony DI system by allowing custom providers and consumers to be defined and then tagghed with what the need or what they can provide.

The system does not load the context until it is needed.

There is cache so that the context is not recomputed everytime a service wants to use it.

Consumer

A consumer service must implement the TechData\ContextDiBundle\Interfaces\ContextConsumerInterface interface and then be tagged with one or more tags of name "context_consumer" which it wants to consume. The tag needs to have an attribute of "context_name" which is a string that specifies which type of context it is looking for.

Provider

A provider service must implement the TechData\ContextDiBundle\Interfaces\ContextProviderInterface interface and then be tagged with a single tag of name "context_provider" which it wants to provide. The tag needs to have an attribute of "context_name" which is a string that specifies which type of context being provided.

Cache

There is a cache which is checked prior to attempting to resolve the context. if the context is already available in the cache, it will be used. The default cache service caches the contexts per request.

Configuiration

The available types of context need to be specified (or "registered") in the main config file ("config.yml"). This is done by specifying the name of an interface or class which will be provided by the provider when the context is resolved. This ensures only certain contexts are available, and the context types are known to the consumer which will need to use them. Example.

tech_data_context_di:
    available_contexts:
        user: Vendor\Bundle\Interfaces\UserInterface
        customer: Vendor\Bundle\Entity\CustomerInterface

Optionally, the cache type can be switched to a different cache service which implements the TechData\ContextDiBundle\Interfaces\ContextCacheInterface. This can be done by specifying the cache service name in the config as shown below. This defaults to "tech_data_context_di.cache.single_request" which is a per request cache.

tech_data_context_di:
    available_contexts:
        user: Vendor\Bundle\Interfaces\UserInterface
        customer: Vendor\Bundle\Entity\CustomerInterface
    cache_service: some.custom.cache.service.name

Included Providers

Some providers for common use cases will be added soon.

contextdi's People

Contributors

westinpigott avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.