Danack

Auryn example

Oct 13th, 2013
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.19 KB | None | 0 0
  1.  
  2. //First we setup some aliasing of interfaces to concrete classes
  3. $provider->alias('Intahwebz\Storage\Storage', 'Intahwebz\Storage\S3Storage');
  4. $provider->alias('Intahwebz\DB\DBConnection', 'Intahwebz\DB\ProxiedConnectionWrapper');
  5. //So any dependency on a 'Storage' interface will now have an S3Storage ojbect provided.
  6.  
  7.  
  8. $standardLogger = createStandardLogger();
  9. $provider->alias('Psr\Log\LoggerInterface', 'Monolog\Logger');
  10. $provider->share($standardLogger); //Used by all classes
  11. //Now any request for a PSR0 LoggerInterface will be given the '$standardLogger' object.
  12.  
  13.  
  14.  
  15.  
  16.  
  17. //Ok, this is the Controller for a page that grabs images from other websites.
  18.  
  19. class ImageFetcher extends \Intahwebz\Controller\Controller {
  20.  
  21.  function __construct(Storage $storage, ImageMapper $imageMapper, RemoteFileFetcher $remoteFileFetcher) {
  22.         $this->storage = $storage;
  23.         $this->imageMapper = $imageMapper;
  24.         $this->remoteFileFetcher = $remoteFileFetcher;
  25.     }
  26. }
  27.  
  28. //To instantiate it, I call:
  29. $provider->make('ImageFetcher');
  30.  
  31. //And that's it.
  32. //
  33. //All the dependencies of that class are recursively created.
  34. //
  35. //The dependencies of that controller class are listed below, there is actually quite a bit of work to
  36. //instantiate the controller class but it's all handled by Auryn.
  37.  
  38.  
  39.  
  40. class RemoteFileFetcher {} //No dependencies
  41.  
  42. abstract class Storage {}
  43.  
  44. class S3Storage extends Storage {
  45.     function __construct(
  46.         LoggerInterface $logger,
  47.         StoragePath $storagePath) {
  48.     }
  49. }
  50.  
  51.  
  52. class ImageMapper {
  53.   function __construct(
  54.         SQLQueryFactory $sqlQueryFactory,
  55.         ContentTable $contentTable,
  56.         ImageTable $imageTable,
  57.         TagTable $tagTable,
  58.         ContentBucketTable $contentBucketTable) {}
  59. }
  60.  
  61.  
  62.  
  63.  
  64. class SQLQueryFactory {
  65.     //Yeah, this is kind of a factory, but it's simple enough to not be
  66.     //a bad thing to do.
  67.     public function __construct(DBConnection $dbConnection) {
  68.         $this->dbConnection = $dbConnection;
  69.     }
  70.     function create() {
  71.         return new SQLQuery($this->dbConnection);
  72.     }
  73. }
  74.  
  75. class ContentTable extends SQLTableMap {
  76.    //My crappy DB abstraction layer.
  77.    function __construct(DBConnection $dbConnection){}
  78. }
Advertisement
Add Comment
Please, Sign In to add comment