1. Go to this page and download the library: Download thecodingmachine/funky 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/ */
thecodingmachine / funky example snippets
class WhoopsMiddlewareServiceProvider implements ServiceProviderInterface
{
public function getFactories()
{
return [
Middleware::class => [self::class,'createMiddleware'],
];
}
public function getExtensions()
{
return [
MiddlewareListServiceProvider::MIDDLEWARES_QUEUE => [self::class,'updatePriorityQueue']
]
}
public static function createMiddleware() : WhoopsMiddleware
{
return new WhoopsMiddleware();
}
public static function updatePriorityQueue(ContainerInterface $container, \SplPriorityQueue $queue) : \SplPriorityQueue
{
$queue->insert($container->get(Middleware::class), MiddlewareOrder::EXCEPTION_EARLY);
return $queue;
}
}
class WhoopsMiddlewareServiceProvider extends Funky\ServiceProvider
{
/**
* @Factory(
* tags={@Tag(name="middlewares.queue", priority=MiddlewareOrder::EXCEPTION_EARLY)}
* )
*/
public static function createMiddleware() : WhoopsMiddleware
{
return new WhoopsMiddleware();
}
}
$ composer
/**
* @Factory
*/
public static function createMiddleware() : WhoopsMiddleware
{
return new WhoopsMiddleware();
}
$container->get(WhoopsMiddleware::class) // will return the service
/**
* @Factory(name="whoops")
*/
public static function createMiddleware() : WhoopsMiddleware
{
return new WhoopsMiddleware();
}
$container->get('whoops') // will return the service
/**
* @Factory(nameFromMethodName=true)
*/
public static function whoopsMiddleware() : WhoopsMiddleware
{
return new WhoopsMiddleware();
}
$container->get('whoopsMiddleware') // will return the service
/**
* @Factory()
*/
public static function myService(LoggerInterface $logger) : MyService
{
// $logger is fetched from the container using "$container->get(LoggerInterface::class)"
return new MyService($logger);
}
/**
* @Factory()
*/
public static function myService(string $ROOT_PATH) : MyService
{
// $ROOT_PATH is fetched from the container using "$container->get('ROOT_PATH')"
return new MyService(string $ROOT_PATH);
}
/**
* @Factory()
*/
public static function myService(ContainerInterface $container) : MyService
{
return new MyService($container->get('ROOT_PATH'));
}
/**
* @Extension()
*/
public static function registerTwigExtension(\Twig_Environment $twig, MyTwigExtension $extension) : \Twig_Environment
{
$twig->register($extension);
return $twig;
}
/**
* The extended entry is named twig because the method name is "twig"
* @Extension(name="twig")
*/
public static function registerExtension(\Twig_Environment $twig, MyTwigExtension $extension) : \Twig_Environment
{
// ...
}
/**
* The extended entry is named "twig" because the method name is "twig"
* @Extension(nameFromMethodName=true)
*/
public static function twig(\Twig_Environment $twig, MyTwigExtension $extension) : \Twig_Environment
{
// ...
}
/**
* Here, the tag "twigExtensions" used in the function above is injected using auto-wiring.
* @Factory()
*/
public static function twig(iterable $twigExtensions) : \Twig_Environment
{
// ...
$twig = new Twig_Environement(...);
foreach ($twigExtensions as $twigExtension) {
$twig->register($twigExtension);
}
}