tflori/dependency-injector

View on GitHub
src/Factory/Concern/IsSharable.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

namespace DependencyInjector\Factory\Concern;

trait IsSharable
{
    /** @var bool */
    protected $shared = false;

    /** @var mixed */
    protected $instance;

    public function share(bool $share = true)
    {
        $this->shared = $share;
    }

    public function isShared(): bool
    {
        return $this->shared;
    }

    public function getInstance(...$args)
    {
        if ($this->isShared()) {
            if (!$this->instance) {
                $this->instance = $this->build();
            }

            return $this->instance;
        }

        /** @noinspection PhpMethodParametersCountMismatchInspection */
        // a concrete factory could use this arguments
        return $this->build(...$args);
    }

    /**
     * This method builds the instance.
     *
     * @return mixed
     */
    abstract protected function build();
}