di is very easy in symfony 2 - php-schulung.de€¦ · timon schroeter 2 di ist very easy in...
Post on 11-Aug-2020
8 Views
Preview:
TRANSCRIPT
Timon Schroeter 1 www.php-schulung.de
DI is very easy in Symfony 2
Timon Schroeter 2 www.php-schulung.de
DI ist very easy in Symfony 2
● Any ordinary PHP class can be managed by DIC● Only 2 lines of configuration per class are needed● Any class managed by the DIC is called a “Service”
Timon Schroeter 3 www.php-schulung.de
# app/config/config.yml# ...services: my_service: class: Acme\MyBundle\Service\AwesomeClass
DI ist very easy in Symfony 2
● Any ordinary PHP class can be managed by DIC● Only 2 lines of configuration per class are needed● Any class managed by the DIC is called a “Service”
Timon Schroeter 4 www.php-schulung.de
# app/config/config.yml# ...services: my_service: class: Acme\MyBundle\Service\AwesomeClass
DI ist very easy in Symfony 2
● Any ordinary PHP class can be managed by DIC● Only 2 lines of configuration per class are needed● Any class managed by the DIC is called a “Service”
Class to manage
Name of the new service
Timon Schroeter 5 www.php-schulung.de
# php app/console container:debug
Timon Schroeter 6 www.php-schulung.de
# app/config/config.yml# ...services: my_service: class: Acme\MyBundle\Service\AwesomeClass
DI ist very easy in Symfony 2
● Any ordinary PHP class can be managed by DIC● Only 2 lines of configuration per class are needed● Any class managed by the DIC is called a “Service”
Timon Schroeter 7 www.php-schulung.de
# app/config/config.yml# ...services: my_service: class: Acme\MyBundle\Service\AwesomeClass arguments: some_arg: “string” another:
- arry_member - arry_member
even_more: @another_service
DI ist very easy in Symfony 2
● Any ordinary PHP class can be managed by DIC● Only 2 lines of configuration per class are needed● Any class managed by the DIC is called a “Service”
Timon Schroeter 8 www.php-schulung.de
# app/config/config.yml# ...services: my_service: class: Acme\MyBundle\Service\AwesomeClass arguments: some_arg: “string” another:
- arry_member - arry_member
even_more: @another_service
DI ist very easy in Symfony 2
● Any ordinary PHP class can be managed by DIC● Only 2 lines of configuration per class are needed● Any class managed by the DIC is called a “Service”
Any other servicecan be injected as
as argument
Arguments can bestrings, numbers,
arrays, placeholders,and many more ...
Timon Schroeter 9 www.php-schulung.de
<?phpuse Guzzle\Http\ClientInterface;use Acme\Logger\LoggerInterface;
class FeedAggregator {private $client;private $logger;
public function __construct(ClientInterface $client, LoggerInterface $logger) {
$this->client = $client;$this->logger = $logger;
}
public function retrieveFeed ($baseurl, $path) {$request = $this->client->setBaseUrl($baseurl)->get($path);$response = $request->send();if (200 != $response->getStatusCode()) {
$this->logger->log('Could not get: '.$host.$path);return null;
}
return $response->getBody();}// ...
}
Timon Schroeter 10 www.php-schulung.de
<?phpuse Guzzle\Http\ClientInterface;use Acme\Logger\LoggerInterface;
class FeedAggregator {private $client;private $logger;
public function __construct(ClientInterface $client, LoggerInterface $logger) {
$this->client = $client;$this->logger = $logger;
}
public function retrieveFeed ($baseurl, $path) {$request = $this->client->setBaseUrl($baseurl)->get($path);$response = $request->send();if (200 != $response->getStatusCode()) {
$this->logger->log('Could not get: '.$host.$path);return null;
}
return $response->getBody();}// ...
}
Timon Schroeter 11 www.php-schulung.de
# app/config/config.yml# ...services: feed_aggregator: class: Acme\FeedBundle\Service\FeedAggregator arguments: client: @http_client logger: @logger
<?phpuse Guzzle\Http\ClientInterface;use Acme\Logger\LoggerInterface;
class FeedAggregator {private $client;private $logger;
public function __construct(ClientInterface $client, LoggerInterface $logger) {
$this->client = $client;$this->logger = $logger;
}
public function retrieveFeed ($baseurl, $path) {$request = $this->client->setBaseUrl($baseurl)->get($path);$response = $request->send();if (200 != $response->getStatusCode()) {
$this->logger->log('Could not get: '.$host.$path);return null;
}
return $response->getBody();}// ...
}
Timon Schroeter 12 www.php-schulung.de
# app/config/config.yml# ...services: feed_aggregator: class: Acme\FeedBundle\Service\FeedAggregator arguments: client: @http_client logger: @logger
<?phpuse Guzzle\Http\ClientInterface;use Acme\Logger\LoggerInterface;
class FeedAggregator {private $client;private $logger;
public function __construct(ClientInterface $client, LoggerInterface $logger) {
$this->client = $client;$this->logger = $logger;
}
public function retrieveFeed ($baseurl, $path) {$request = $this->client->setBaseUrl($baseurl)->get($path);$response = $request->send();if (200 != $response->getStatusCode()) {
$this->logger->log('Could not get: '.$host.$path);return null;
}
return $response->getBody();}// ...
}
Timon Schroeter 13 www.php-schulung.de
Different Config for Testing?
● app/config/config.yml● app/config/config_dev.yml● app/config/config_test.yml Put it here here
Timon Schroeter 14 www.php-schulung.de
Nachhaltige Architektur für große Web-Projekte
● Modularität● Erweiterbarkeit● Wartbarkeit● Testbarkeit● Performance
Timon Schroeter 15 www.php-schulung.de
Summary
● Our classes only depend on interfaces
Timon Schroeter 16 www.php-schulung.de
Summary
● Our classes only depend on interfaces● All implementation classes are instanciated and
provided (injected) by the DIC
Timon Schroeter 17 www.php-schulung.de
Summary
● Our classes only depend on interfaces● All implementation classes are instanciated and
provided (injected) by the DIC● Our classes create only domain entities,
exceptions and value objects
Timon Schroeter 18 www.php-schulung.de
Summary
● Our classes only depend on interfaces● All implementation classes are instanciated and
provided (injected) by the DIC● Our classes create only domain entities,
exceptions and value objects ● The DIC is not passed to any model class, domain
entity, exception or value object ● Controllers can access the DIC to obtain services
Timon Schroeter 19 www.php-schulung.de
Further Reading
● http://fabien.potencier.org/article/11/what-is-dependency-injection
● http://symfony.com/doc/current/components/dependency_injection/compilation.html
● http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html
● Use the source ...
Timon Schroeter 20 www.php-schulung.de
Further Reading
● http://fabien.potencier.org/article/11/what-is-dependency-injection
● http://symfony.com/doc/current/components/dependency_injection/compilation.html
● http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html
● Use the source ...
Timon Schroeter 21 www.php-schulung.de
Vielen Dank für Eure Aufmerksamkeit!
Timon Schroeter 22 www.php-schulung.de
SOLID
● S Single Responsibility Principle● O Open / Close Principle● L Liskov Substitution Principle● I Interface Segregation Principle● D Dependency Inversion Principle
top related