PHP code example of andy-shea / pulp

1. Go to this page and download the library: Download andy-shea/pulp 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/ */

    

andy-shea / pulp example snippets


class SecurityService {

  protected $strategy;
  protected $acl;

  public function __construct(
    AuthenticationStrategy $strategy,
    AccessControlList $acl
  ) {
    $this->strategy = $strategy;
    $this->acl = $acl;
  }

  public authenticateUser($username, $password) {
    return $this->strategy->autheticate($username, $password);
  }

  public authoriseUser(User $user, Resource $resource) {
    return $this->acl->isAllowed($user, $resource);
  }

}

public class SecurityModule extends AbstractModule {

  protected void configure() {
    $this->bind(AuthenticationStrategy::class)->to(BasicAuthStrategy::class);
    $this->bind(AccessControlList::class)->to(AdminAcl::class);
  }

}

class SecurityService {

  #[Inject]
  public function __construct(
    AuthenticationStrategy $strategy,
    AccessControlList $acl
  ) {
    ...
  }

}

$injectorBuilder = new InjectorBuilder();
$modules = [new SecurityModule()];
$injector = $injectorBuilder->addModules($modules)->build();
$securityService = $injector->getInstance('SecurityService')

$this->bind(AuthenticationStrategy::class)->to(BasicAuthStrategy::class);

$this->bind(BasicAuthStrategy::class)->to(PersistentBasicAuthStrategy::class);

$basicAuthStrategy = new BasicAuthStrategy();
$this->bind(AuthenticationStrategy::class)->toInstance($basicAuthStrategy);
$this->bind('dbConnectionString')->toInstance('pgsql:host=localhost;port=5432;dbname=anotherdb');

public class SecurityModule extends AbstractModule {

  protected void configure() {
    ...
  }

  #[Provides(Database::class)]
  public function provideDatabase() {
    $db = new PostgresDatabase('pgsql:host=localhost;port=5432;dbname=testdb');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $db;
  }

}

interface Provider {

  function get();

}

public class DatabaseProvider implements Provider {

  protected $dbLog;

  #[Inject]
  public __construct(Log $dbLog) {
    $this->dbLog = $dbLog;
  }

  public function get() {
    $db = new PostgresDatabase('pgsql:host=localhost;port=5432;dbname=testdb');
    $db->setLog($this->dbLog);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $db;
  }

}

public class SecurityModule extends AbstractModule {

  protected void configure() {
    $this->bind(Database::class)->toProvider(DatabaseProvider::class);
  }

}

class BillingService {

  protected $paymentProcessorProvider;

  #[Inject]
  public function __construct(#[Provides(CreditCardProcessor::class)] Provider $paymentProcessorProvider) {
    $this->paymentProcessorProvider = $paymentProcessorProvider;
  }

}

class CreditCardPayment implements Payment {

  #[Inject]
  public function __construct(MerchantGateway $gateway) {
    ...
  }

}

class CashPayment implements Payment {
  ...
}

interface PaymentFactory {

  #[Returns(CreditCardPayment::class)]
  function createCreditCardPayment();

  #[Returns(CashPayment::class)]
  function createCashPayment();

}

public class PaymentModule extends AbstractModule {

  protected void configure() {
    $this->install(new FactoryProvider('PaymentFactory'));
  }

}

class CashDrawer {

  protected $paymentFactory;

  #[Inject]
  public function __construct(PaymentFactory $paymentFactory) {
    $this->paymentFactory = $paymentFactory;
  }

  public function addCreditCardPayment($amount) {
    $payment = $this->paymentFactory->createCreditCardPayment();
    ...
  }

}

class ClientSecurityService {

  #[Inject]
  public function __construct(Database $clientDatabase) {
    ..
  }

}

class AdminSecurityService {

  #[Inject]
  public function __construct(Database $adminDatabase) {
    ..
  }

}

#[Attribute(Attribute::TARGET_PARAMETER), Qualifier]
final class ClientDatabase {}

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER), Qualifier]
final class AdminDatabase {}

class ClientSecurityService {

  #[Inject]
  public function __construct(#[ClientDatabase] Database $db) {
    ..
  }

}

class AdminSecurityService {

  #[Inject, AdminDatabase] protected Database $db;

}

public class SecurityModule extends AbstractModule {

  protected void configure() {
    $this->bind(ClientDatabase::class)->toProvider(ClientDatabaseProvider::class);
    $this->bind(AdminDatabase::class)->toProvider(AdminDatabaseProvider::class);
  }

}

class ClientSecurityService {

  #[Inject]
  public function __construct(#[Named('ClientDatabase')] Database $db) {
    ..
  }

}

class AdminSecurityService {

  #[Inject, Named('AdminDatabase')] protected Database $db;

}

public class SecurityModule extends AbstractModule {

  protected void configure() {
    $this->bind('ClientDatabase')->toProvider(ClientDatabaseProvider::class);
    $this->bind('AdminDatabase')->toProvider(AdminDatabaseProvider::class);
  }

}

$this->bind(AuthenticationStrategy::class)->to(BasicAuthStrategy::class)->in(Scopes::singleton());

$this->bind(RouteMapper::class)->in(Scopes::singleton());

#[Provides(Database::class), Singleton]
public function provideDatabase() {
  ...
}

class SecurityService {

  protected $strategy;
  protected $acl;
  protected $log;
  #[Inject] protected EmailService $emailService;

  #[Inject]
  public function __construct(
    AuthenticationStrategy $strategy,
    AccessControlList $acl
  ) {
    $this->strategy = $strategy;
    $this->acl = $acl;
  }

  #[Inject]
  public function setLog(Log $securityLog) {
    $this->log = $securityLog;
  }

  public authenticateUser($username, $password) {
    return $this->strategy->autheticate($username, $password);
  }

  public authoriseUser(User $user, Resource $resource) {
    return $this->acl->isAllowed($user, $resource);
  }

}

class PostgresDatabase {

  #[Inject]
  public function __construct(
    #[Named('adminConnectionString')] $connectionString = 'pgsql:host=localhost;port=5432;dbname=testdb'
  ) {
    ...
  }

}

class CreditCardPayment implements Payment {

  #[Inject]
  public function __construct(MerchantGateway $gateway, #[Assisted] $amount) {
    ...
  }

}

class CashPayment implements Payment {

  #[Inject]
  public function __construct(#[Assisted] $amount) {
    ...
  }

}

interface PaymentFactory {

  #[Returns(CreditCardPayment::class)]
  function createCreditCardPayment($amount);

  #[Returns(CashPayment::class)]
  function createCashPayment($amount);

}

public class PaymentModule extends AbstractModule {

  protected void configure() {
    $this->install(new FactoryProvider('PaymentFactory'));
  }

}

class CashDrawer {

  protected $paymentFactory;

  #[Inject]
  public function __construct(PaymentFactory $paymentFactory) {
    $this->paymentFactory = $paymentFactory;
  }

  public function addCreditCardPayment($amount) {
    $payment = $this->paymentFactory->createCreditCardPayment($amount);
    ...
  }

}