PHP code example of miniature / component
1. Go to this page and download the library: Download miniature/component 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/ */
miniature / component example snippets
function miniature_autoload($class)
{
$fileName = str_replace('\\', '/', realpath(__DIR__) . '/' . $class ) . '.php';
if (preg_match('/^(.*\/Miniature)\/(\w+)\/((\w+\/)*)(\w+)\.php/', $fileName)) {
$newFileName = preg_replace(
'/^(.*\/Miniature)\/(\w+)\/((\w+\/)*)(\w+)\.php/',
'$1/$2/src/$3$5.php',
$fileName
);
if (is_file($newFileName)) {
use Miniature\Component\Component;
class SelfSpeakingComponent extends Component {
protected static ?Component $instance = null;
}
$selfSpeakingComponentInstance = SelfSpeakingComponent::getInstance();
$paramObject = (new Miniature\Component\InitParameters())
->setConfigDirectory( __DIR__ . '/../config');
$selfSpeakingComponentInstance = SelfSpeakingComponent::getInstance($paramObject);
use Miniature\Component\Component;
use Miniature\Component\InitParametersInterface;
use Miniature\Component\InitParameters;
class SelfSpeakingComponent extends Component
{
protected static ?Component $instance = null;
protected static function autoInject() : ?InitParametersInterface
{
return (new InitParameters())
->setConfigDirectory( __DIR__ . '/../config');
}
}
$instantlyNeededInstance = SelfSpeakingComponent::getInstance();
namespace YourOwnExamleApp;
use Miniature\Component\Component;
use Miniature\Component\InitParametersInterface;
use Miniature\Component\InitParameters;
use YourOwnExamleApp\The2ndComponent;
use YourOwnExamleApp\ProductAccessInterface;
use YourOwnExamleApp\PersonAccessInterface;
use YourOwnExamleApp\AddressAccessInterface;
class SelfSpeakingComponent extends Component
{
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* INIT
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
protected static ?Component $instance = null;
protected static function autoInject() : ?InitParametersInterface
{
return (new InitParameters())
->setConfigDirectory( __DIR__ . '/../config');
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* PROVIDE
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
public function providePerson() : PersonAccessInterface
{
return $this->container->get('person_access');
}
public function provideAddress() : AddressAccessInterface
{
return $this->container->get('address_access');
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* CONSUME
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
public function consumeProduct() : ProductAccessInterface
{
return The2ndComponent::getInstance()->provideProduct();
}
}
$this->insureCoupling();
namespace YourOwnExamleApp;
use Miniature\Component\Component;
use Miniature\Component\InitParametersInterface;
use Miniature\Component\InitParameters;
use YourOwnExamleApp\The2ndComponent;
use YourOwnExamleApp\ProductAccessInterface;
use YourOwnExamleApp\PersonAccessInterface;
use YourOwnExamleApp\AddressAccessInterface;
class SelfSpeakingComponent extends Component
{
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* INIT
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
protected static ?Component $instance = null;
protected static function autoInject() : ?InitParametersInterface
{
return (new InitParameters())
->setConfigDirectory( __DIR__ . '/../config');
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* PROVIDE
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
public function providePerson() : PersonAccessInterface
{
$this->insureCoupling();
return $this->container->get('person_access');
}
public function provideAddress() : AddressAccessInterface
{
$this->insureCoupling();
return $this->container->get('address_access');
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* CONSUME
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
public function consumeProduct() : ProductAccessInterface
{
$this->insureCoupling();
return The2ndComponent::getInstance()->provideProduct();
}
}
return [
'SelfSpeakingComponent' => [
'providePerson' => [
'The2ndComponent' => [
'consumeAPerson' => true
]
],
'provideAddress' => [
'The2ndComponent' => [
'consumeAdress' => true
]
],
'consumeProduct' => [
'SomeClassFromInsideOfTheContainer' => [
'fetchProduct' => true
],
'SomeOtherClassFromInsideOfTheContainer' => [
'retrieveProduct' => true,
'evaluateProduct' => true
],
'ClassOnlyInTestingEnvironment' => true
],
],
'The2ndComponent' => [
'provideProduct' =>[
'SelfSpeakingComponent' => [
'consumeProduct' => true
]
]
],
'Unfinished3rdComponent' => [
'provideTonsOfInfo' =>[
'The2ndComponent' => true
]
]
];
SelfSpeakingComponent::getInstance()->providePerson();
The2ndComponent::getInstance()->consumePerson();
return [
'di_mapping' => [
'person' => [
'class' => 'AppDemo\Person',
],
'address' => [
'class' => 'AppDemo\Address',
'args' => [ '@person' ]
],
'person_manager' => [
'class' => 'AppDemo\PersonManager',
'args' => [
'person' => '@person',
'address' => '@address',
'db_wrapper' => '@mysql_wrapper'
]
],
'mysql_wrapper' => [
'class' => 'AppDemo\MysqlWrapper',
'singleton' => true,
'args' => [
'%mysql_person_connection',
'%access_token'
]
],
'person_access' => [
'class' => 'AppDemo\PersonAccess',
'static' => 'getInstance',
'public' => true,
'args' => [
'@miniature.di_container'
]
],
],
'params' => [
'mysql_person_connection' => [
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'root',
'pw' => 'top-secret-123',
'dbName' => 'person',
'class' => 'Component\DbMysql'
],
'access_token' => 'xOQvvkE2NGsaoUPJqlwpuzP'
]
];
$instance = AppDemo\PersonAccess::getInstance($container);
$paramObject = (new Miniature\Component\InitParameters())
// other configurations
->setEnvAllowingPublicAccess('dev', 'test', 'prod');
SelfSpeakingComponent::getInstance()->person_access->somePublicMethod('string_parameter');
SelfSpeakingComponent::getInstance()->get('person_access')->somePublicMethod('string_parameter');
$instanceFromMapping = $this->container->get('class_key_string');
$instanceWithOverrides =
$this->container->get(
'person_manager',
[
'address' => '@other_address_key',
'person' => '@other_person_key',
]
);
$paramObject = (new Miniature\Component\InitParameters())
// other settings
->setYamlParserDecorator(new MyOwnAPP\YamlParserDecorator());
$paramObject = (new Miniature\Component\InitParameters())
->setConfigDirectory( __DIR__ . '/../config');
$selfSpeakingComponentInstance = SelfSpeakingComponent::getInstance($paramObject);
class SelfSpeakingComponent extends Component
{
protected static ?Component $instance = null;
protected static function autoInject() : ?InitParametersInterface
{
return (new InitParameters())
->setConfigDirectory( __DIR__ . '/../config');
}
}
$instantlyNeededInstance = SelfSpeakingComponent::getInstance();
$paramObject = (new Miniature\Component\InitParameters())
->setAppRootPath(__DIR__ . '/..')
->setConfigDirectory('config')
->setDotEnvPath('');
$paramObject = (new Miniature\Component\InitParameters())
->setAppRootPath(__DIR__ . '/..')
->setConfigDirectory(__DIR__ . '/../config');
$paramObject = (new Miniature\Component\InitParameters())
->setAppRootPath(__DIR__ . '/..')
->setConfigDirectory('config');
$paramObject->setConfigDirectory(__DIR__ . '/../config');
$paramObject->setDotEnvPath(__DIR__ . '/..');
$paramObject->setAvailableEnv(
['dev', 'test', 'prod', 'another']
);
$paramObject->setAvailableEnv('dev', 'test', 'prod', 'another');
$paramObject->setEnvAllowingPublicAccess('dev', 'test', 'another');
$paramObject->setYamlParserDecorator(new MyOwnAPP\YamlParserDecorator());
$paramObject->setDiSyntaxMapper(new Miniature\DiContainer\Syntax\MapperSymfonyStyle());
$paramObject = (new Miniature\Component\InitParameters())
->setAvailableEnv('develop', 'testing', 'production', 'another');