PHP code example of upmind / provision-provider-base
1. Go to this page and download the library: Download upmind/provision-provider-base 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/ */
upmind / provision-provider-base example snippets
declare(strict_types=1);
namespace Upmind\ProvisionExample\Category\HelloWorld;
use Upmind\ProvisionBase\Provider\BaseCategory;
use Upmind\ProvisionBase\Provider\DataSet\AboutData;
use Upmind\ProvisionExample\Category\HelloWorld\Data\Greeting;
use Upmind\ProvisionExample\Category\HelloWorld\Data\PersonData;
/**
* A simple category for 'Hello World' provider implementations. All Providers
* of this Category must implement the 'greeting' function, using the single
* parameter 'name'.
*/
abstract class HelloWorldCategory extends BaseCategory
{
public static function aboutCategory(): AboutData
{
return AboutData::create()
->setName('Hello World')
->setDescription('A demonstration category that doesn\'t actually do anything');
}
/**
* Greet the user by name.
*/
abstract public function greeting(PersonData $person): Greeting;
}
declare(strict_types=1);
namespace Upmind\ProvisionExample\Category\HelloWorld;
use Upmind\ProvisionBase\Provider\Contract\ProviderInterface;
use Upmind\ProvisionBase\Provider\DataSet\AboutData;
use Upmind\ProvisionExample\Category\HelloWorld\Data\FooConfiguration;
use Upmind\ProvisionExample\Category\HelloWorld\Data\PersonData;
use Upmind\ProvisionExample\Category\HelloWorld\Data\Greeting;
/**
* This HelloWorld Provider 'Foo' provides its own implementation of the
* 'greeting' provision function.
*/
class ProviderFoo extends HelloWorldCategory implements ProviderInterface
{
/**
* @var FooConfiguration
*/
protected $configuration;
/**
* Providers always receive their configuration passed to their constructor.
*
* @param array $configuration
*/
public function __construct(FooConfiguration $configuration)
{
$this->configuration = $configuration;
}
public static function aboutProvider(): AboutData
{
return AboutData::create()
->setName('Hello Foo')
->setDescription('A demonstration of a provision function success case');
}
public function greeting(PersonData $person): Greeting
{
$apiKey = $this->configuration->api_key;
$apiSecret = $this->configuration->api_secret;
// $this->authenticateWithSomeApi($api_key, $api_secret);
// do something with configuration data
return Greeting::create()
->setMessage('Greeting generated successfully')
->setSentence(sprintf('Hello %s! From your friend, Foo.', $person->name));
}
}
declare(strict_types=1);
namespace Upmind\ProvisionExample\Category\HelloWorld\Laravel;
use Upmind\ProvisionBase\Laravel\ProvisionServiceProvider;
use Upmind\ProvisionExample\Category\HelloWorld\HelloWorldCategory;
use Upmind\ProvisionExample\Category\HelloWorld\ProviderFoo;
use Upmind\ProvisionExample\Category\HelloWorld\ProviderBar;
class ServiceProvider extends ProvisionServiceProvider
{
/**
* Bind the HelloWorld Category and its Providers.
*/
public function boot()
{
$this->bindCategory('hello-world', HelloWorldCategory::class);
$this->bindProvider('hello-world', 'foo', ProviderFoo::class);
$this->bindProvider('hello-world', 'bar', ProviderBar::class);
}
}
use Illuminate\Support\Facades\Cache;
use Upmind\ProvisionBase\Registry\Registry;
$cacheKey = 'upmind-provision-registry';
$serializedRegistry = serialize(Registry::getInstance());
Cache::forever($cacheKey, $serializedRegistry);
declare(strict_types=1);
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Cache;
use Upmind\ProvisionBase\Registry\Registry;
/**
* Bind the Provision Registry to the container
*/
class ProvisionRegistryServiceProvider extends ServiceProvider
{
/**
* @var string
*/
public const REGISTRY_CACHE_KEY = 'upmind-provision-registry';
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Load the Provision Registry from cache, or obtain a fresh instance and bind
* the Registry to the container.
*
* @return void
*/
public function register()
{
// Attempt to set the Registry instance from cache
if ($cachedRegistry = Cache::get(self::REGISTRY_CACHE_KEY)) {
$registry = unserialize($cachedRegistry);
if ($registry instanceof Registry) {
Registry::setInstance($registry);
}
}
// Bind registry as singleton to container
$this->app->singleton(Registry::class, function () {
return Registry::getInstance();
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [Registry::class];
}
}
use Upmind\ProvisionBase\ProviderFactory;
use Upmind\ProvisionBase\Registry\Registry;
$fooConfiguration = [
'api_key' => 'foo api key here',
'api_secret' => 'foo api secret here',
];
$factory = new ProviderFactory(Registry::getInstance());
$foo = $factory->create('hello-world', 'foo', $fooConfiguration);
// => Instance of Upmind\ProvisionBase\Provider
$greetingParameters = [
'name' => 'Harry',
];
$greeting = $foo->makeJob('greeting', $greetingParameters);
// => Instance of Upmind\ProvisionBase\ProviderJob
$greetingResult = $greeting->execute();
// => Instance of Upmind\ProvisionBase\Result\ProviderResult
$data = $greetingResult->getData();
// Greeting result data:
// [
// 'sentence' => 'Hello Harry! From your friend, Foo.'
// ]
declare(strict_types=1);
namespace Upmind\ProvisionExample\Category\HelloWorld\Data;
use Upmind\ProvisionBase\Provider\DataSet\DataSet;
use Upmind\ProvisionBase\Provider\DataSet\Rules;
/**
* Data set encapsulating a person.
*
* @property-read string $name Name of the person
*/
class PersonData extends DataSet
{
public static function rules(): Rules
{
return new Rules([
'name' => ['
declare(strict_types=1);
namespace Upmind\ProvisionExample\Category\HelloWorld\Data;
use Upmind\ProvisionBase\Provider\DataSet\ResultData;
use Upmind\ProvisionBase\Provider\DataSet\Rules;
/**
* Data set encapsulating a greeting.
*
* @property-read string $sentence Greeting sentence
*/
class Greeting extends ResultData
{
public static function rules(): Rules
{
return new Rules([
'sentence' => ['
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.