magento-di

Configure Magento 2 dependency injection — di.xml, types, virtual types, preferences, argument replacement, and Object Manager. Use when wiring dependencies, creating class variations, or configuring module integrations.

17 stars

Best use case

magento-di is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Configure Magento 2 dependency injection — di.xml, types, virtual types, preferences, argument replacement, and Object Manager. Use when wiring dependencies, creating class variations, or configuring module integrations.

Teams using magento-di should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/magento-di/SKILL.md --create-dirs "https://raw.githubusercontent.com/OrcaQubits/agentic-commerce-skills-plugins/main/dist/antigravity/magento2-commerce/.agent/skills/magento-di/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/magento-di/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How magento-di Compares

Feature / Agentmagento-diStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Configure Magento 2 dependency injection — di.xml, types, virtual types, preferences, argument replacement, and Object Manager. Use when wiring dependencies, creating class variations, or configuring module integrations.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Magento 2 Dependency Injection

## Before writing code

**Fetch live docs**:
1. Web-search `site:developer.adobe.com commerce php development components dependency-injection` for the DI guide
2. Fetch `https://developer.adobe.com/commerce/php/development/` and navigate to DI documentation
3. Web-search `site:developer.adobe.com commerce php development build di-xml` for di.xml reference

## Conceptual Architecture

### How DI Works in Magento

Magento's Object Manager reads `di.xml` configurations and automatically injects dependencies into class constructors. You declare what you need; the framework provides it.

**Constructor injection** is the primary pattern — declare dependencies as constructor parameters with type hints.

### di.xml Scope

di.xml files are area-scoped:
- `etc/di.xml` — global (all areas)
- `etc/frontend/di.xml` — storefront only
- `etc/adminhtml/di.xml` — admin panel only
- `etc/webapi_rest/di.xml` — REST API only
- `etc/webapi_soap/di.xml` — SOAP API only

### Types

Configure constructor arguments for a specific class:
- Override default values
- Inject different implementations per area
- Argument types: `string`, `boolean`, `number`, `const`, `null`, `object`, `array`, `init_parameter`

### Virtual Types

Create class variations **without writing new PHP files**:
- Same base class with different constructor arguments
- Only exists in DI configuration
- Cannot be injected by classname directly (use as a `type` attribute value)
- Reduces code duplication significantly

### Preferences

Map an interface to a concrete implementation:
- `<preference for="InterfaceName" type="ConcreteClassName" />`
- Global preference applies everywhere unless overridden by area-specific di.xml
- Foundation of Magento's interface-based programming

### Argument Types

| Type | Description |
|------|-------------|
| `string` | String value |
| `boolean` | `true` or `false` |
| `number` | Integer or float |
| `const` | PHP constant value |
| `null` | Null value |
| `object` | Another class instance (injected) |
| `array` | Array of mixed argument types |
| `init_parameter` | Value from `Magento\Framework\App\DeploymentConfig` |

### Shared vs Non-Shared

- By default, Object Manager creates **shared** instances (singleton behavior)
- Set `shared="false"` on a type to get a new instance each time
- Factories (`SomeClassFactory`) always create new instances

### Sensitive/Environment Config

`init_parameter` type reads from `app/etc/env.php` — use for environment-specific values that shouldn't be in di.xml.

## Common Patterns

- **Interface → Implementation mapping**: preference for repository, data, and service interfaces
- **Logger customization**: virtual type with custom handler arguments
- **Collection modification**: type with different filter arguments per area
- **Plugin declaration**: type with plugin child element (covered in plugins skill)

## Best Practices

- Always inject interfaces, not concrete classes
- Use virtual types to avoid unnecessary PHP files
- Scope di.xml to the smallest applicable area
- Never call Object Manager directly in application code (only in factories and framework)
- Use `shared="false"` sparingly — most dependencies should be shared

Fetch the DI documentation for exact XML schema, element attributes, and current best practices before configuring.

Related Skills

magento-testing

17
from OrcaQubits/agentic-commerce-skills-plugins

Write tests for Magento 2 — PHPUnit unit tests, integration tests, MFTF functional tests, and API tests. Use when implementing test coverage for modules, debugging, or setting up CI/CD test pipelines.

magento-setup

17
from OrcaQubits/agentic-commerce-skills-plugins

Set up a Magento 2 Open Source project — installation, Composer setup, system requirements verification, and initial configuration. Use when starting a new Magento project or setting up a development environment.

magento-service-contracts

17
from OrcaQubits/agentic-commerce-skills-plugins

Implement Magento 2 service contracts — repository interfaces, data interfaces, SearchCriteria, and the repository pattern. Use when building module APIs, data access layers, or integrating with Magento's Web API.

magento-security

17
from OrcaQubits/agentic-commerce-skills-plugins

Implement Magento 2 security — CSP, 2FA, CSRF protection, ACL, admin security configuration, input validation, and security best practices. Use when hardening a Magento installation or reviewing security posture.

magento-plugins-interceptors

17
from OrcaQubits/agentic-commerce-skills-plugins

Implement Magento 2 plugins (interceptors) — before, after, and around methods for modifying class behavior without inheritance. Use when extending core or third-party module functionality.

magento-performance

17
from OrcaQubits/agentic-commerce-skills-plugins

Optimize Magento 2 performance — full page cache (Varnish), Redis, indexer tuning, JavaScript/CSS optimization, database optimization, and profiling. Use when diagnosing slow pages, optimizing load times, or configuring caching.

magento-module-dev

17
from OrcaQubits/agentic-commerce-skills-plugins

Create Magento 2 custom modules — registration, directory structure, models, resource models, collections, declarative schema, and data/schema patches. Use when building new modules or understanding module architecture.

magento-frontend

17
from OrcaQubits/agentic-commerce-skills-plugins

Build Magento 2 frontend — layout XML, blocks, PHTML templates, ViewModels, themes, JavaScript (RequireJS/KnockoutJS), and LESS/CSS. Use when customizing the storefront, building themes, or working with frontend components.

magento-events-cron

17
from OrcaQubits/agentic-commerce-skills-plugins

Implement Magento 2 events, observers, cron jobs, and message queues. Use when building event-driven logic, scheduled tasks, or asynchronous processing.

magento-eav-attributes

17
from OrcaQubits/agentic-commerce-skills-plugins

Work with Magento 2 EAV (Entity-Attribute-Value) system — create custom attributes, attribute sets, manage EAV tables, and understand the EAV data model. Use when adding product/category/customer attributes or working with the attribute system.

magento-deploy

17
from OrcaQubits/agentic-commerce-skills-plugins

Deploy Magento 2 — deployment modes, static content deployment, DI compilation, CLI commands, zero-downtime strategies, and CI/CD pipeline setup. Use when preparing for production deployment or building deployment automation.

magento-checkout

17
from OrcaQubits/agentic-commerce-skills-plugins

Customize Magento 2 checkout — payment methods, shipping carriers, totals collectors, and checkout UI. Use when building custom payment/shipping integrations or modifying the checkout flow.