Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Parameter Laravel Package

respect/parameter

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require respect/parameter
    

    Add to composer.json under require-dev if only for testing.

  2. Basic Usage

    use Respect\Parameter\Parameter;
    use Respect\Parameter\Exception\ParameterException;
    
    $container = new DI\Container(); // PSR-11 compatible container
    $parameter = new Parameter($container);
    
    try {
        $service = $parameter->resolve('MyService'); // Resolves by type
    } catch (ParameterException $e) {
        // Handle missing/incorrect resolution
    }
    
  3. First Use Case Resolve a constructor dependency in a Laravel service provider:

    public function register()
    {
        $this->app->bind(MyClass::class, function ($app) {
            $parameter = new Parameter($app);
            return new MyClass(
                $parameter->resolve(MyDependency::class)
            );
        });
    }
    

Implementation Patterns

Dependency Injection Workflows

  1. Constructor Injection

    class MyService {
        public function __construct(
            private MyDependency $dependency,
            private AnotherDependency $another
        ) {}
    }
    // Resolve via:
    $service = $parameter->resolve(MyService::class);
    
  2. Method Injection

    $service = $parameter->resolve(MyService::class);
    $service->doSomething(
        $parameter->resolve(RequiredParam::class)
    );
    
  3. Laravel Service Container Integration

    // In a controller/middleware:
    $this->parameter->resolve(AuthService::class)->check();
    

Advanced Patterns

  • Named Parameters
    $parameter->resolve('App\Services\AuthService');
    
  • Interface Resolution
    $parameter->resolve(LoggerInterface::class); // Resolves concrete implementation
    
  • Optional Parameters
    $parameter->resolve(OptionalService::class, false); // Returns null if missing
    

Testing Patterns

// In PHPUnit tests:
$mockContainer = $this->createMock(ContainerInterface::class);
$mockContainer->method('get')->willReturn(new MockService());

$parameter = new Parameter($mockContainer);
$result = $parameter->resolve(MockService::class);
$this->assertInstanceOf(MockService::class, $result);

Gotchas and Tips

Common Pitfalls

  1. Circular Dependencies

    • Respect/Parameter does not handle circular dependencies natively.
    • Fix: Use Laravel’s built-in container or a dedicated DI library (e.g., PHP-DI).
  2. Non-PSR-11 Containers

    • Ensure your container implements Psr\Container\ContainerInterface.
    • Fix: Wrap non-compliant containers (e.g., Laravel’s Illuminate\Container\Container) with a PSR-11 adapter.
  3. Type Hinting Mismatches

    • If a class has strict type hints but the container returns a compatible but not identical type, resolution fails.
    • Fix: Use interfaces or abstract classes for dependencies.
  4. Performance Overhead

    • Repeated calls to resolve() for the same type may trigger redundant lookups.
    • Fix: Cache resolved instances (Laravel’s app()->make() already does this).

Debugging Tips

  • Enable Debug Mode

    $parameter = new Parameter($container, Parameter::DEBUG);
    

    Logs resolution attempts to stderr.

  • Check Container Contents

    if ($container->has(MyService::class)) {
        // Proceed with resolution
    }
    

Extension Points

  1. Custom Resolvers Override resolution logic via a decorator:

    $parameter = new Parameter($container);
    $parameter->setResolver(function ($type) {
        return new CustomResolver($type);
    });
    
  2. Parameter Objects Use Respect\Parameter\ParameterObject for immutable parameter configurations:

    $paramObj = new ParameterObject($container, MyService::class);
    $service = $paramObj->resolve();
    
  3. Laravel-Specific Tip Bind the package to Laravel’s container in AppServiceProvider:

    $this->app->singleton(Parameter::class, function ($app) {
        return new Parameter($app);
    });
    

    Then inject Parameter directly into classes:

    public function __construct(private Parameter $parameter) {}
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table
siberfx/flysystem-wasabi
hollyit/token-replace
ippanelcom/laravel-sdk
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel