Giter VIP home page Giter VIP logo

di's Introduction

The DI Package Build Status

Latest Stable Version Total Downloads Latest Unstable Version License

The Joomla! Dependency Injection package provides a powerful PSR-11 compatible Inversion of Control (IoC) Container for your application.

Requirements

  • PHP 7.2.5 or later

Installation via Composer

Add "joomla/di": "~2.0" to the require block in your composer.json and then run composer install.

{
	"require": {
		"joomla/di": "~2.0"
	}
}

Alternatively, you can simply run the following from the command line:

composer require joomla/di "~2.0"

If you want to include the test sources and docs, use

composer require --prefer-source joomla/di "~2.0"

di's People

Stargazers

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

Watchers

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

di's Issues

Deprecation notice on PHP 8

Steps to reproduce the issue

Use Joomla\Container\Container::buildObject().

Expected result

No notices.

Actual result

Deprecated: Method ReflectionParameter::getClass() is deprecated in vendor\joomla\di\src\Container.php on line 324

System information (as much as possible)

PHP 8.0.2

Additional comments

Both branches affected.

Deprecated

* @deprecated 3.0 Use ContainerInterface::has() instead

I was reviewing the deprecated messages in my Joomla 4 install and was confused by this one

2018-09-14T11:30:12+00:00 WARNING 127.0.0.1 deprecated Joomla\DI\Container::exists() is deprecated and will be removed in 3.0, use Psr\Container\ContainerInterface::has() instead.

So I came here to suggest that the message was clarified to include framework or similar so it was clearer when you hit the message in the CMS.

But then I discovered that the message doesn't exist here and the version of this file in my j4 install is completely different to this one??

Add enumeration method or implement Iterator in 2.x

I propose adding an enumeration method (e.g. getIterator) or implementing an enumeration interface (e.g. Iterator) or other Traversable interface (e.g. IteratorAggregate) would be useful. My container factory dynamically generates services and I would like to be able to pull every service from the container at runtime by iterating each one. The specific implementation is up to you.

I propose an implementation similar to the following.

    public function getIterator()
    {
        foreach ($this->aliases as $key) {
            yield $key;
        }

        foreach ($this->resources as $key => $resource) {
            yield $key;
        }
    }

There would presumably need to be some discussion around whether to include any $parents. I don't know what the use-case for parents is so I decline to comment.

A little fix to remove warning

Steps to reproduce the issue

Use Joomla fw/ DI on my platform, install by composer

composer install "joomla/di"

Expected result

Install without warning

Actual result

There is a warning

Class Joomla\DI\Tests\StubInterface located in ./vendor/joomla/di/Tests/Stubs/stubs.php does not comply with psr-4 autoloading standard. Skipping.
Class Joomla\DI\Tests\Stub1 located in ./vendor/joomla/di/Tests/Stubs/stubs.php does not comply with psr-4 autoloading standard. Skipping.
Class Joomla\DI\Tests\Stub2 located in ./vendor/joomla/di/Tests/Stubs/stubs.php does not comply with psr-4 autoloading standard. Skipping.
Class Joomla\DI\Tests\Stub3 located in ./vendor/joomla/di/Tests/Stubs/stubs.php does not comply with psr-4 autoloading standard. Skipping.
Class Joomla\DI\Tests\Stub4 located in ./vendor/joomla/di/Tests/Stubs/stubs.php does not comply with psr-4 autoloading standard. Skipping.
Class Joomla\DI\Tests\Stub5 located in ./vendor/joomla/di/Tests/Stubs/stubs.php does not comply with psr-4 autoloading standard. Skipping.
Class Joomla\DI\Tests\Stub6 located in ./vendor/joomla/di/Tests/Stubs/stubs.php does not comply with psr-4 autoloading standard. Skipping.
Class Joomla\DI\Tests\Stub7 located in ./vendor/joomla/di/Tests/Stubs/stubs.php does not comply with psr-4 autoloading standard. Skipping.
Class Joomla\DI\Tests\Stub8 located in ./vendor/joomla/di/Tests/Stubs/stubs.php does not comply with psr-4 autoloading standard. Skipping.
Class Joomla\DI\Tests\Stub9 located in ./vendor/joomla/di/Tests/Stubs/stubs.php does not comply with psr-4 autoloading standard. Skipping.

System information (as much as possible)

Docker container Debain 10
Nginx with PHP8

Additional comments

This just need to separate file into many files. Is there any deeper reason we keep them into one file ?

ContainerAwareTrait

What do you think about adding ContainerAwareTrait with getContainer and setContainer methods?

I know it's PHP 5.4+ but there could be a not in the docblock.

Container::buildObject may result in infitite loop

The buildObject method uses set method at the bottom.

In specific case this may lead to infinite loop of
Joomla\DI\Container->buildObject() and Joomla\DI\Container->getMethodArgs() methods resulting in
Fatal error: Maximum function nesting level of '100' reached, aborting!

Because same key is being set in two places at a time:

$fqcn = '\\Extension\\vendor\\FooComponent\\FooComponent';
$data = array();

$container->set(
    $fqcn,
    function (Container $c) use ($fqcn, $data)
    {
        $instance = $c->buildObject($fqcn);
        $instance->setData($data);

        return $instance;
    }
);

I'm not sure if this is a bug (I'd say the set method shouldn't be part of the buildObject) or I misunderstood the functionality.

[2.0] Change extend to allow any callable

The extend() method is typehinted to only allow a Closure to be used for service decorators. This is inconsistent with the rest of the container which allows any callable to be used for a service. Let's make extend() work with anything that is callable.

PHP 8 incompatibility

See the results of phan in 3.x-dev. In src/Container.php line 467 and 483, the methods \ReflectionType::isBuiltin() and \ReflectionType::getName() are used, which don't exist in PHP 8 anymore. This needs to be refactored.

Fatal error due to incompatible declaration

Steps to reproduce the issue

  1. Create a file that loads the Joomla bootstrap.php
  2. Load the DatabaseDriver from the container
  3. Script fails due to Fatal error: Declaration of Joomla\DI\Container::has(string $resourceName) must be compatible with Psr\Container\ContainerInterface::has(string $id): bool in /libraries/vendor/joomla/di/src/Container.php on line 107

Expected result

The DatabaseDriver is loaded

Actual result

A fatal error is thrown.

System information (as much as possible)

PHP 8.1/PHP 8.2
Joomla 5.0.2

Additional comments

The Container.php has this line:

public function has($resourceName)

that should be changed to:

public function has(string $resourceName): bool
<?php

class joomla {

public function __construct() {
   $this->loadJoomla();
   $db    = Factory::getContainer()->get('DatabaseDriver');
}

private function loadJoomla(): void
{
		// Tell Joomla we can be trusted
		if (!defined('_JEXEC'))
		{
			define('_JEXEC', 1);
		}

		// Set the base folder
		$base = dirname(__FILE__, 7);

        define('JDEBUG', false);

        if (!defined('JPATH_BASE'))
        {
            define('JPATH_BASE', dirname($base));
        }

        if (file_exists($base . '/../includes/defines.php'))
        {
            require_once $base . '/../includes/defines.php';
        }

        require_once $base . '/bootstrap.php';

        require_once JPATH_CONFIGURATION . '/configuration.php';
}
}

Implement PSR-11

The 2.0 branch already implements the Interop Container interface, which is what PSR-11 is based on. We should switch this to the PSR interface once the vote is complete and the package published and additionally try to implement it for the 1.x branch.

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.