Принцип инверсии зависимостей (англ. dependency inversion principle, DIP):
Модули верхних уровней не должны зависеть от модулей нижних уровней. Оба типа модулей должны зависеть от абстракций. Абстракции не должны зависеть от деталей. Детали должны зависеть от абстракций.
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{Request,Entity,Migrations,Tests,Kernel.php}'
App\Console\:
resource: '../src/Console'
public: true
tags: ['console.command']
App\Command\:
resource: '../src/Command'
tags:
- { name: tactician.handler, typehints: true }
League\Tactician\CommandBus: '@tactician.commandbus'
class CreateCliRequest extends ContainerAwareCommand
{
/**
* @var CommandBus
*/
private $commandBus;
public function __construct(?string $name = null, CommandBus $commandBus)
{
parent::__construct($name);
$this->commandBus = $commandBus;
}
protected function configure()
{
$this
->setName('create:cli:req')
->addArgument('name', InputArgument::REQUIRED)
->setDescription('Some command');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$request = new CreateRequest(12, $input->getArgument('name'));
$result = $this->commandBus->handle($request);
$output
->writeln('<info>' .$result . '</info>');
}
}
любимые = [a, b, c]
нелюбимые = [d, e, f]
function menu(лимит){
result = []
while питательность(result) < лимит{
if random() > 0.3 // число взято наобум
продукт = случайный(любимые)
else
продукт = случайный(нелюбимые)
if продукт не в result
добавить продукт в result
}
выбросить из result последнее
return result
}
Yii::$container->setSingleton('common\domain\Entities\User\Profile\Interfaces\Repository', // указываем интерфейс
[ // указываем конфигурацию класса реализующего этот интерфейс
'class' => 'common\infrastructure\Entities\User\Profile\Repository'
],
[ // указываем какие данные необходимо передать в конструктор, в частности - экземпляр класса UserProfile
Instance::of('common\models\ActiveRecord\UserProfile')
]
);
Yii::$container->setSingleton('common\domain\Entities\User\Profile\Interfaces\Service',
[
'class' => 'common\application\User\Profile\Service',
],
[
Instance::of('common\domain\Entities\User\Profile\Interfaces\Repository')
],
);
Как еще можно отрефакторить данный метод?