PHP code example of spiechu / lazy-pimple

1. Go to this page and download the library: Download spiechu/lazy-pimple library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spiechu / lazy-pimple example snippets





$pimpleContainer['lazy_loading_value_holder_factory_factory'] = function(Container $container) {
  return (new \Spiechu\LazyPimple\Factory\LazyLoadingValueHolderFactoryFactory())
    ->getFactory();
};

$pimpleContainer['lazy_service_factory'] = function(Container $container) {
  return new \Spiechu\LazyPimple\Factory\LazyServiceFactory($container['lazy_loading_value_holder_factory_factory']);
};



// imgine awesome service is expensive and should be lazy loaded
$pimpleContainer['awesome_service'] = function(Container $container) {
  return $container['lazy_service_factory']->getLazyServiceDefinition(AwesomeService::class, function() {
    return new AwesomeService();
  });
};



$pimpleContainer['first_subscriber'] = function(Container $container) {
  // subscriber has no idea it will be lazy loaded
  return new FirstSubscriber($container['awesome_service']);
};



$pimpleContainer->register(new LazyEventSubscriberServiceProvider(
  $pimpleContainer['lazy_service_factory'],
    // we're defining which service resolves to EventDispatcher
    'event_dispatcher',
    [
      // we're defining subscribers
      'first_subscriber' => FirstSubscriber::class,
    ]
));



use Pimple\Container;
use Spiechu\LazyPimple\DependencyInjection\LazyEventSubscriberServiceProvider;
use Spiechu\LazyPimple\Factory\LazyLoadingValueHolderFactoryFactory;
use Spiechu\LazyPimple\Factory\LazyServiceFactory;
use Spiechu\LazyPimple\FirstSubscriber;
use Spiechu\LazyPimple\Service\AnotherService;
use Spiechu\LazyPimple\Service\AwesomeService;
use Spiechu\LazyPimple\Service\EventEmittingService;
use Symfony\Component\EventDispatcher\EventDispatcher;

// prevent leaking any variables into global namespace
return call_user_func(function() {
  nction(Container $container) {
    return new LazyServiceFactory($container['lazy_loading_value_holder_factory_factory']);
  };

  $pimpleContainer['event_dispatcher'] = function(Container $container) {
    return new EventDispatcher();
  };

  // imgine awesome service is expensive and should be lazy loaded
  $pimpleContainer['awesome_service'] = function(Container $container) {
    return $container['lazy_service_factory']->getLazyServiceDefinition(AwesomeService::class, function() {
      return new AwesomeService();
    });
  };

  $pimpleContainer['another_service'] = function(Container $container) {
    // this one will receive proxy object
    return new AnotherService($container['awesome_service']);
  };

  $pimpleContainer['event_emitting_service'] = function(Container $container) {
    return new EventEmittingService($container['event_dispatcher']);
  };

  $pimpleContainer['first_subscriber'] = function(Container $container) {
    // subscriber has no idea it will be lazy loaded
    return new FirstSubscriber($container['awesome_service']);
  };

  $pimpleContainer->register(new LazyEventSubscriberServiceProvider(
    $pimpleContainer['lazy_service_factory'],
    // we're defining which service resolves to EventDispatcher
    'event_dispatcher',
    [
      // we're defining subscribers
      'first_subscriber' => FirstSubscriber::class,
    ]
  ));

  return $pimpleContainer;
});