di is very easy in symfony 2 - php-schulung.de€¦ · timon schroeter 2 di ist very easy in...

22
Timon Schroeter 1 www.php-schulung.de DI is very easy in Symfony 2

Upload: others

Post on 11-Aug-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

Timon Schroeter 1 www.php-schulung.de

DI is very easy in Symfony 2

Page 2: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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”

Page 3: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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”

Page 4: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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

Page 5: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

Timon Schroeter 5 www.php-schulung.de

# php app/console container:debug

Page 6: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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”

Page 7: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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”

Page 8: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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 ...

Page 9: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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();}// ...

}

Page 10: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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();}// ...

}

Page 11: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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();}// ...

}

Page 12: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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();}// ...

}

Page 13: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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

Page 14: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

Timon Schroeter 14 www.php-schulung.de

Nachhaltige Architektur für große Web-Projekte

● Modularität● Erweiterbarkeit● Wartbarkeit● Testbarkeit● Performance

Page 15: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

Timon Schroeter 15 www.php-schulung.de

Summary

● Our classes only depend on interfaces

Page 16: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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

Page 17: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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

Page 18: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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

Page 19: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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 ...

Page 20: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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 ...

Page 21: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

Timon Schroeter 21 www.php-schulung.de

Vielen Dank für Eure Aufmerksamkeit!

Page 22: DI is very easy in Symfony 2 - php-schulung.de€¦ · Timon Schroeter 2 DI ist very easy in Symfony 2 Any ordinary PHP class can be managed by DIC Only 2 lines of configuration per

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