PHP code example of greatcode / controller-global

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

    

greatcode / controller-global example snippets


use Greatcode\ControllerGlobal\CtrlGlobal;

// From a Connection instance
$ctrl = new CtrlGlobal($connection);

// From a config array (passed to Connection::fromConfig)
$ctrl = new CtrlGlobal(['driver' => 'sqlite', 'dbname' => ':memory:']);

// With a custom AES encryption key
$ctrl = new CtrlGlobal($connection, 'my-secret-key');

// From environment variables / global $cfg array
$ctrl = new CtrlGlobal();

// Singleton
$ctrl = CtrlGlobal::getInstance();

$ctrl->insert('users', [
    'name'  => 'Alice',
    'email' => '[email protected]',
]);
// returns 'success'

$ctrl->insertAll('users', [
    ['name' => 'Alice', 'email' => '[email protected]'],
    ['name' => 'Bob',   'email' => '[email protected]'],
]);

$ctrl->update(
    'users',
    ['email' => '[email protected]'],  // SET
    ['id'    => 42]                  // WHERE (AND-joined)
);

$ctrl->delete('users', ['id' => 42]);

$ctrl->deleteAll('users', [
    ['id' => 1],
    ['id' => 2],
]);

$rows = $ctrl->GetGlobalFilter('SELECT * FROM users WHERE active = ?', [1]);

$rows = $ctrl->GetGlobalFilter(
    'SELECT * FROM users WHERE role = :role',
    [':role' => 'admin']
);

$rows = $ctrl->runSql('SELECT * FROM orders WHERE status = ?', ['pending']);

$name = $ctrl->getName('SELECT name FROM categories WHERE id = ?', [5]);

$token   = $ctrl->encode('sensitive-value');
$decoded = $ctrl->decode($token); // 'sensitive-value'

$response = $ctrl->httpGet('https://api.example.com/items');
$response = $ctrl->httpPost('https://api.example.com/items', ['json' => ['name' => 'foo']]);
$response = $ctrl->httpPut('https://api.example.com/items/1', ['json' => ['name' => 'bar']]);
$response = $ctrl->httpPatch('https://api.example.com/items/1', ['json' => ['name' => 'baz']]);
$response = $ctrl->httpDelete('https://api.example.com/items/1');

$client = $ctrl->getHttpClient(); // GuzzleHttp\Client

use Greatcode\ControllerGlobal\Connection;

$conn = new Connection('mysql:host=127.0.0.1;dbname=mydb;charset=utf8mb4', 'user', 'pass');

$conn = new Connection('sqlite::memory:', options: [PDO::ATTR_EMULATE_PREPARES => true]);

$conn = Connection::fromConfig([
    'driver'   => 'mysql',       // default: 'mysql'
    'host'     => '127.0.0.1',   // default: 'localhost'
    'port'     => 3306,
    'dbname'   => 'mydb',
    'charset'  => 'utf8mb4',     // default: 'utf8mb4'
    'username' => 'user',
    'password' => 'pass',
    'options'  => [],            // extra PDO attributes
]);

// SQLite in-memory
$conn = Connection::fromConfig(['driver' => 'sqlite', 'dbname' => ':memory:']);

$conn = Connection::fromEnv();

$conn = Connection::getInstance('mysql:host=127.0.0.1;dbname=mydb', 'user', 'pass');

// Subsequent calls with any arguments return the first instance
$same = Connection::getInstance();

$conn->transaction(function (Connection $db) {
    $db->exec("INSERT INTO orders (total) VALUES (100)");
    $db->exec("UPDATE stock SET qty = qty - 1 WHERE id = 5");
});

// Return values are passed through
$id = $conn->transaction(fn(Connection $db) => $db->lastInsertId());

use Greatcode\ControllerGlobal\CtrlGroupPos;

$pos = new CtrlGroupPos('PC-001', 'https://integrator.example.com');
bash
export DB_DRIVER=mysql
export DB_HOST=127.0.0.1
export DB_PORT=3306
export DB_NAME=test_db
export DB_USER=root
export DB_PASSWORD=secret

./vendor/bin/phpunit --testsuite Integration --filter Mysql
bash
export PGSQL_HOST=127.0.0.1
export PGSQL_PORT=5432
export PGSQL_NAME=test_db
export PGSQL_USER=postgres
export PGSQL_PASSWORD=secret

./vendor/bin/phpunit --testsuite Integration --filter Pgsql