PHP code example of waponix / pocket

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

    

waponix / pocket example snippets



use Waponix\Pocket\Pocket;

class Person()
{
}

Pocket::setRoot('./src'); // assuming that all of your classes are found inside the src folder within your project directory

$pocket = Pocket::getInstance();

$person = $pocket->get(Person::class); // will return an object instance of Person


class Person()
{
}

class Vehicle()
{
  public function __construct(
    public readonly Person $owner // this will be auto injected
  )
  {
  }
}

Pocket::setRoot('./src');

$pocket = Pocket::getInstance();
$person = $pocket->get(Vehicle::class);


use Waponix\Pocket\Attribute\Service;

#[Service(
	args: [
		'method' => 'GET', // will map to $method
		'url' => 'localhost' // will map to $url
	]
)]
class Post()
{
	publc function __construct(
		public readonly string $method,
		public readonly string $url,
	)
	{
	
	}
}

$post = $pocket->get(Post::class);

use Waponix\Pocket\Pocket;

Pocket::setRoot('./src');
Pocket::setParameters([
		'post' => [
			'method' => 'GET',
			'url' => 'localhost'
		]
	]);
	
$pocket = Pocket::getInstance();

#[Service(
    args: [
        'method' => '@post.method',
        'url' => '@post.url'
    ]
)]
class Post()
{
    publc function __construct(
        public readonly string $method,
        public readonly string $url,
    )
    {
    }
}

class HomeController
{
	public function index(Request $request): Response
	{
		// do something with the $request and return a Response
		return new Response();
	}
}

$pocket->invoke(HomeController::class, 'index');

use Waponix\Pocket\Attribute\Service;
use Waponix\Pocket\Attribute\Factory;

#[Service(
	factory: new Factory(
		class: PersonCreator::class,
		method: 'createBob'
	)
)]
class Bob extends PersonAbstract
{}

class PersonCreator
{
	public function createBob(): Person // concrete class
	{
		return new Bob();
	}
}

use Waponix\Pocket\Attribute\Service;

#[Service(tag: 'person')]
class John extends Person
{
}

$persons = $pocket->get('#person');

use Waponix\Pocket\Attribute\Service;

#[Service(tag: ['person', 'employee', 'owner'])]
class John extends Person
{
}