Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //First we setup some aliasing of interfaces to concrete classes
- $provider->alias('Intahwebz\Storage\Storage', 'Intahwebz\Storage\S3Storage');
- $provider->alias('Intahwebz\DB\DBConnection', 'Intahwebz\DB\ProxiedConnectionWrapper');
- //So any dependency on a 'Storage' interface will now have an S3Storage ojbect provided.
- $standardLogger = createStandardLogger();
- $provider->alias('Psr\Log\LoggerInterface', 'Monolog\Logger');
- $provider->share($standardLogger); //Used by all classes
- //Now any request for a PSR0 LoggerInterface will be given the '$standardLogger' object.
- //Ok, this is the Controller for a page that grabs images from other websites.
- class ImageFetcher extends \Intahwebz\Controller\Controller {
- function __construct(Storage $storage, ImageMapper $imageMapper, RemoteFileFetcher $remoteFileFetcher) {
- $this->storage = $storage;
- $this->imageMapper = $imageMapper;
- $this->remoteFileFetcher = $remoteFileFetcher;
- }
- }
- //To instantiate it, I call:
- $provider->make('ImageFetcher');
- //And that's it.
- //
- //All the dependencies of that class are recursively created.
- //
- //The dependencies of that controller class are listed below, there is actually quite a bit of work to
- //instantiate the controller class but it's all handled by Auryn.
- class RemoteFileFetcher {} //No dependencies
- abstract class Storage {}
- class S3Storage extends Storage {
- function __construct(
- LoggerInterface $logger,
- StoragePath $storagePath) {
- }
- }
- class ImageMapper {
- function __construct(
- SQLQueryFactory $sqlQueryFactory,
- ContentTable $contentTable,
- ImageTable $imageTable,
- TagTable $tagTable,
- ContentBucketTable $contentBucketTable) {}
- }
- class SQLQueryFactory {
- //Yeah, this is kind of a factory, but it's simple enough to not be
- //a bad thing to do.
- public function __construct(DBConnection $dbConnection) {
- $this->dbConnection = $dbConnection;
- }
- function create() {
- return new SQLQuery($this->dbConnection);
- }
- }
- class ContentTable extends SQLTableMap {
- //My crappy DB abstraction layer.
- function __construct(DBConnection $dbConnection){}
- }
Advertisement
Add Comment
Please, Sign In to add comment