spiral / spiral/framework

Autowiring with proxy

Open
#650 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Component: Core Discussion Feature
Dominant language
PHP
Stars
2.1k
Forks
92
Avg merge
14h 57m
Merged PRs (30d)
4

Description

Hard way

Using type association and intersection with the Lazy class, we can generate proxies and decorators.

class Service {
    public function __construct(
        Lazy|OrmInterface $proxy, // magic proxy
        Lazy&OrmInterface $decorator,
    ) {
        \assert($proxy instanceof Lazy);
        \assert($proxy::class === Lazy::class || $proxy::class === Proxy::class);
        \assert(!$proxy instanceof OrmInterface);

        \assert($decorator::class === 'AutoGeneratedDecoratorClassName');
        \assert($decorator instanceof Lazy);
        \assert($decorator instanceof OrmInterface);

        $proxy->getSchema();
        $decorator->getSchema();

        # Problems
        ## There are the same problems with properties like in Cycle ORM proxy entities
            $proxy->list[] = 'foo'; // error if proxy is not resolved
            $foo = &$proxy->list;   // error
        ## Decorator will have very difficult code
        ## Decorator is non-compatible with final classes and methods
    }
}

Simple way

Make the proxy without magic, just with get() method.

/**
 * @template TDefinition
 */
class Proxy {
    /**
     * @return TDefinition
     */
    public function get(): mixed;
}
class Service {
    /**
     * @var Proxy<OrmInterface> Use template for autocompletion
     */
    private Proxy $orm;

    public function __construct(
        Proxy|OrmInterface $proxy,
    ) {
        $this->orm = $proxy; // Mb psalm will throw some warning here
    }

    public function doSomeAction() {
        $this->orm->get() // Resolve ORM
            ->getSchema();
        // ...
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files, tests, or entry points. Start by tracing the framework's autowiring and proxy-generation implementation, then compare the magic proxy and explicit get() examples. Done should include a decided approach, documented behavior for proxy and decorator types, and tests covering the supported cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.