Download the PHP package earc/component-di without Composer

On this page you can find all versions of the php package earc/component-di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package component-di

earc/component-di

The next generation of dependency injection is here to support you in your strive for high quality software.

earc/component-di is build on the top of earc/di, makes the components of your app explicit and gives you the freedom to restrict dependency access class wise to improve the decoupling of your components.

why should you use it?

earc/di is a really amazing, lightweight, easy to use dependency injection system that integrates even with symfony.

earc/components-di is earc/di++. You decide if classes are accessible from other components or not. It throws an error if you introduce an new dependency accidentally and thus helps you to keep the architectural design clean.

Real world example: Assume you maintain as a software engineer several web shops written mainly by your predecessors. Today your task is to write a new order export for one of these. Currently you need to pass the shipping costs to an object. You have an order which has a cart and there is a CartService::getShippingCost() method. Hurray, that was easy!

It passes code review, unit testing, integration testing, testing of your customer. But surprise surprise, you just introduced a new bug. If a customer confirms an order, the shop operator changes the shipping costs and then the order get exported the order get exported with the new shipping costs - but the customer has confirmed the old ones.

Maybe your lucky and the time window is small enough to let the bug never really happen. But face it, you have written bad buggy code and what makes it even worse, it passed all quality enforcement measures!

To cut a long story short, if the engineer who wrote the CartService::class had a tool to tell the dependency injection system that this service only belong to the cart component and its descendants, you would have had no chance to introduce this bug.

to what effort?

Nearly none.

You first define your components. That are just empty classes inheriting direct or indirect from the RootComponent::class.

For each of your classes you decide is it an public, protected or private Service or no Service at all. For each decision exists an separate Interface.

You decide to which component your class belong. The components class name you return in a method named getComponent.

And last but not least instead of

you write

Maybe that sounds much but it is at most a minute more work on each class.

installation

bootstrap

usage

defining components

Lets stay to the shop example from above. You have products, a cart, a wish list, a financial service provider, acheckout, a shop operator page tool, an third party inventory controll interface, an product import and an order export to make the list short.

Your Component definition could look like this:

Your pages component need to arrange products. That's no problem. Maybe the products component has a public service to deliver the data.

It is not about building walls, its about encapsulation. It enables you to divide your monolith into responsibilities that have some sort of api without breaking it into microservices.

the component interface flags

To become part of the component biosphere a class has to implement one of the following component interfaces: PublicServiceInterface, ProtectedServiceInterface, PrivateServiceInterface or NoServiceInterface. It flags the class with the desired visibility.

To get a grip on the component the interface enforces an getComponent method.

Since the CartService::class is a protected Service it would be visible to the classes of the cart component and the wish list component only.

the component resolver

di_comp returns the component resolver object that belongs to the passed class name.

Important: Use always di_comp(static::class) instead of di_comp(self::class) or di_comp(ClassName::class). If the class is extended only the late static binding of static::class ensures that php passes the right argument.

The component resolver object has three methods get() make() and param(), They all return the component resolver object to allow method chaining and the the first argument is a always the variable to pass the result of the call to.

dependency injection and the component resolver

The component resolver object methods get() and make() replace the di_* functions di_get and di_make respectively. Both perform a visibility check for the passed class name (or the decorator if decorated) against the current component first. If the visibility check fails a AccessDeniedException is thrown. If it is passed the result of a di_get or di_make call is written to the $object argument that was passed by reference.

Example: Using earc/di you write

the same using earc/component-di

You can check access by

parameter injection and the component resolver

The component resolver object method param() does not constrict access like get() and make() it enriches it. Each components class name is mapped to a key. You can check it by

This key is prefixed. First for the current component, if no parameter is found for the parent and so on. At last the parameter is looked up without prefix.

If a parameter is found it is written to the $parameter argument that was passed by reference.

Example: Using earc/di you write

the same using earc/component-di

Assuming this peace of code is from a class of the cart component. Then the first parameter lookup would be cartcomponent.service.param, the second would be productcomponent.service.param and the last would be service.param.

decoration, tagging, mocking

The other di_* functions for decoration, tagging and mocking stay the same and can be used without any restrictions.

di_clear_cache is not restricted either. But di_has should be used with care - maybe sometimes an

is closer to the truth.

exceptions

hacking earc/component-di

You can register your own component resolver as argument to ComponentDi::init(). After that di_comp returns the new component resolver. Just extend the existing ComponentResolver::class or implement the ComponentResolverInterface to bend the logic to your need.

releases

release v1.0 (alpha)

complete rewrite based on the complete rewrite of earc/di (release v2.0)

release v0.0

initial release

TODO


All versions of component-di with dependencies

PHP Build Version
Package Version
Requires php Version ^7.2
earc/di Version dev-master
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package earc/component-di contains the following files

Loading the files please wait ....