GithubHelp home page GithubHelp logo

lucatume / di52 Goto Github PK

View Code? Open in Web Editor NEW
58.0 58.0 11.0 3.01 MB

The dependency injection container for streamlined WordPress development. Easily manage and inject dependencies for efficient and hassle-free app creation.

Home Page: http://theaveragedev.com

PHP 97.87% Makefile 1.57% Shell 0.12% Dockerfile 0.44%
dependency-injection php wordpress

di52's People

Contributors

bordoni avatar deepsource-io[bot] avatar defunctl avatar estevao90 avatar luc45 avatar lucatume avatar omarreiss avatar paragonie-scott avatar rene-hermenau avatar sc0ttkclark avatar truongwp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

di52's Issues

Remove use of aliases.php

While it makes sense on the back-compatibility front, the autoloading of the aliases.php file will create problems in some contexts, e.g. when using Strauss to prefix it.

Upgrade PHP version

Introduction

Looking at the Dependency Graph (which excludes private packages, but it's a somewhat reliable source of information), DI52 is mostly used in the WordPress environment.

PHP 5.2 is used in 0.8% of WordPress websites today. Outside the WordPress environment that's pretty much consistent.

Having PHP 5.2 compatibility made a lot of sense when DI52 was released, because that was the minimum PHP version for WordPress. Since April 2019, with the release of WordPress 5.1, the minimum PHP version was raised to 5.6.

Why am I opening this now?

DI52 adds an annoying and no longer maintained dependency to a project, which is xrstf/composer-php52. This is not a development dependency, this is a REAL dependency that's added on all projects that depend on DI52.

I, as a developer of WP STAGING, want to use DI52, but I don't want to depend on xrstf/composer-php52 because our minimum PHP version is 5.6.

What to do about it

  1. I can fork it and create a Luc45/DI56, for which I would ask your blessing to do so.
  2. You create a lucatume/DI56 repository which is an exact copy of DI52 but with composer5-3.json as default, and I will happily contribute to it.
  3. We move xrstf/composer-php52 to suggest in composer.json, add a php-52-compatibility.md file in the project, explaining that for PHP 5.2 compatibility the person should add the scripts portion in his composer.json and install it with composer update --install-suggests.

Request for feedback: Define Singleton or Bind as default auto-resolve behavior when creating a new Container instance

When the container auto injects a dependency, it returns a new instance of the dependency by default. (bind behavior)

Most of the cases, however, I'd like to inject the same instance (singleton behavior).

I'd like to propose a feature, which is to define the default behavior for auto resolving when creating an instance of the Container:

class tad_DI52_Container {
    public function __construct( $autoResolveAsSingleton = false ) {
        $this->autoResolveAsSingleton = $autoResolveAsSingleton;
    }
}

$container = new tad_DI52_Container( true );

Before I jump into trying to come a PR for this, I'd like to ask for feedback on this idea.

Thanks.

Incorrect exception thrown if failed to auto resolve nested dependency

Hi Luca,

This is an old issue I've always faced with DI52.

Consider the following scenario:

class Car {
	public function __construct( Engine $e ) {}
}

class Engine {
	public function __construct( NonExisting $g ) {}
}

$container = new tad_DI52_Container();
$container->make( Car::class );

( ! ) Fatal error: Uncaught RuntimeException: Error while making 'Car': 'Car' is not a bound alias or an existing class. in /shared/httpd/di52/htdocs/di52/src/tad/DI52/Container.php on line 352

The exception tells me that Car doesn't exist, where actually the problem is in my Engine because it depends on NonExisting.

An explicit binding throws an accurate error:

$container = new tad_DI52_Container();

$container->bind( Car::class, function ( tad_DI52_Container $c ) {
	return new Car( $c->make( NonExisting::class ) );
	// or return new Car( new NonExisting );
} );

$container->make( Car::class );

( ! ) Fatal error: Uncaught RuntimeException: Error while making 'NonExisting': Class NonExisting does not exist in /shared/httpd/di52/htdocs/di52/src/tad/DI52/Container.php on line 352

I might come up with a PR for it, but I felt like sharing this here first to give you a head start in case you want to look into this yourself.

afterBuildMethod is not called on decorator, but only on base instance

when providing afterbuildmethods to a bindDecorators call, once the container creates the instances, the afterbuildmethods are only called on the base class. the afterbuildmethods should be called on the first decorator so the calls goes down the chain, ultimately to the base class as last one.

Auto resolve does not respect the Singleton definition if registered as a callback

This test fails:

/** @test */
public function it_should_respect_singletons_when_auto_resolving() {
	$c = new tad_DI52_Container;

	$c->singleton(Dependency::class, function() {
		return new Dependency;
	});

	$parent = $c->make(Depending::class);
	$parent2 = $c->make(Depending::class);

        // Failed asserting that two variables reference the same object.
	$this->assertSame($parent->getDependency(), $parent2->getDependency());

	// If not using a closure, the test would pass:
	//$c->singleton(Dependency::class, Dependency::class);
}

This is because offsetGet calls the callback to get the instance of Dependency, which returns a new instance.

If the callback was executed already, it should save that instance in the singletons array and return from that.

Before I try to open a PR for it, can you please confirm this is not the expected behavior, and that test should be passing?

Update project's about to PHP 5.6

On this repo's "About" (that shows in the sidebar) it says A PHP 5.2 compatible dependency injection container..

It should be PHP 5.6.

Dynamically injected dependency behaves like a Singleton

When you have a class Class which depends on class Dependency and call Class 2 times, it will have the same instance of Dependency injected.

Please check the attached zip for proof. It's just printing a random number. container-make-test.zip

This is how it should be done? It's quite misleading when you have many dependencies not registered as singletons and I'd consider it a bug.

Lexical syntax error triggers an exception with message "Class does not exist"

Due to a PHP limitation, when using ReflectionClass on a class that has a syntax error that prevents PHP from parsing it (as with lexical errors, unexpected tokens), it throws a ReflectionException saying that The class does not exist:

[27-Nov-2020 12:23:53 UTC] PHP Fatal error:  Uncaught RuntimeException: 'WPStaging\Pro\Snapshot\Ajax\Delete'
	 => 'WPStaging\Pro\Snapshot\Service'
	 => Error while making 'WPStaging\Pro\Snapshot\Site\Service\SnapshotService': Class WPStaging\Framework\Filesystem\Filesystem does not exist in /var/www/single/wp-content/plugins/wp-staging-dev/vendor/lucatume/di52/src/tad/DI52/Container.php:356
Stack trace:
#0 /var/www/single/wp-content/plugins/wp-staging-dev/vendor/lucatume/di52/src/tad/DI52/Container.php(281): tad_DI52_Container->resolve('WPStaging\\Pro\\S...')
#1 /var/www/single/wp-content/plugins/wp-staging-dev/Pro/Snapshot/SnapshotServiceProvider.php(35): tad_DI52_Container->make('WPStaging\\Pro\\S...')
#2 /var/www/single/wp-content/plugins/wp-staging-dev/Framework/DI/ServiceProvider.php(10): WPStaging\Pro\Snapshot\SnapshotServiceProvider->addHooks()
#3 /var/www/single/wp-content/plugins/wp-staging-dev/vendor/lucatume/di52/src/tad/DI52/Container.php(484): WPStaging\Framework\DI\ServiceProvider->register()
#4 /var/www/single/wp-content/plugins/wp-stagin in /var/www/single/wp-content/plugins/wp-staging-dev/vendor/lucatume/di52/src/tad/DI52/Container.php on line 356

In reality, the error was a missing semi-colon on the WPStaging\Framework\Filesystem\Filesystem class.

I suggest we try to improve this error message, saying something like Could not reflect class XYZ. This class might have a syntax error, or might not exist.

I can open a PR for this in the next couple of days.

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.