PHP code example of ggedde / spry
1. Go to this page and download the library: Download ggedde/spry 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/ */
ggedde / spry example snippets
Spry\Spry::run('../config.php');
$config->salt = '';
$config->endpoint = 'http://localhost:8000';
$config->componentsDir = __DIR__.'/components';
...
echo Spry::config()->salt;
echo Spry::config()->db['database_name']
$config->mySetting = '123';
echo Spry::config()->mySetting;
namespace Spry\SpryComponent;
use Spry\Spry;
class MyComponent
{
private static $id = 2; // Component ID
public static function setup() {
Spry::addFilter('configure', 'MyComponent::stuff');
}
public static function stuff($config) {
// Do Stuff
return $config;
}
public static function getRoutes() {
return [
'/items/{id}' => [
'label' => 'Get Item',
'controller' => 'MyComponent::get',
'access' => 'public',
'methods' => 'GET',
'params' => [
'id' => [
' public static function getSchema() {
return [
'items' => [
'columns' => [
'name' => [
'type' => 'string',
],
],
],
];
}
public static function getTests() {
return [
'items_get' => [
'label' => 'Get Example',
'route' => '/items/123',
'method' => 'GET',
'params' => [],
'expect' => [
'status' => 'success',
],
],
];
}
public static function get($params = [])
{
$response = Spry::db()->get('items', '*', $params)
return Spry::response(self::$id, 01, $response);
}
}
public static function getRoutes() {
return [
'/items/{id}' => [
'label' => 'Get Item',
'controller' => 'MyComponent::get',
'access' => 'public',
'methods' => 'GET',
'params' => [
'id' => [
'
$config->routes[
'/items/{id}' => [
'label' => 'Get Item',
'controller' => 'MyComponent::get',
'access' => 'public',
'methods' => 'GET',
'params' => [
'id' => [
'
Spry::db()->get('items', '*', ['id' => 123]);
Spry::db()->select('items', '*', ['date[>]' => '2020-01-01']);
Spry::db()->insert('items', ['name' => 'test', 'date' => '2020-01-01']);
Spry::db()->update('items', ['name' => 'newtest'], ['id' => 123]);
Spry::db()->delete('items', ['id' => 123]);
$config->dbProvider = 'Spry\\SpryProvider\\SpryDB';
$config->db = [... ];
Spry::log()->message("My Message");
Spry::log()->warning("Warning");
Spry::log()->error("Error");
$config->loggerProvider = 'Spry\\SpryProvider\\SpryLogger';
$config->logger = [... ];
Spry::response($data = null, $responseCode = 0, $responseStatus = null, $meta = null, $additionalMessages = []);
$data = ['id' => 123, 'name' => 'John'];
return Spry::response($data, 0);
Spry::stop($responseCode = 0, $responseStatus = null, $data = null, $additionalMessages = [], $privateData = null);
if ($error) {
Spry::stop(0);
}
public static function getCodes()
{
return [
0 => [ // Get Single
'success' => ['en' => 'Successfully Retrieved Item'],
'warning' => ['en' => 'No Item with that ID Found'],
'error' => ['en' => 'Error: Retrieving Item'],
],
1 => [ // Get Multiple
'info' => ['en' => 'No Results Found'],
'success' => ['en' => 'Successfully Retrieved Items'],
'error' => ['en' => 'Error: Retrieving Items'],
],
2 => [ // Insert
'success' => ['en' => 'Successfully Created Item'],
'error' => ['en' => 'Error: Creating Item'],
],
3 => [ // Update
'success' => ['en' => 'Successfully Updated Item'],
'error' => ['en' => 'Error: Updating Item'],
],
4 => [ // Delete
'success' => ['en' => 'Successfully Deleted Item'],
'error' => ['en' => 'Error: Deleting Item'],
],
];
}
$config->tests[
1 => [
0 => [ // Get Single
'success' => ['en' => 'Successfully Retrieved Item'],
'warning' => ['en' => 'No Item with that ID Found'],
'error' => ['en' => 'Error: Retrieving Item'],
],
1 => [ // Get Multiple
'info' => ['en' => 'No Results Found'],
'success' => ['en' => 'Successfully Retrieved Items'],
'error' => ['en' => 'Error: Retrieving Items'],
],
2 => [ // Insert
'success' => ['en' => 'Successfully Created Item'],
'error' => ['en' => 'Error: Creating Item'],
],
3 => [ // Update
'success' => ['en' => 'Successfully Updated Item'],
'error' => ['en' => 'Error: Updating Item'],
],
4 => [ // Delete
'success' => ['en' => 'Successfully Deleted Item'],
'error' => ['en' => 'Error: Deleting Item'],
],
],
2 => [
0 => [ // Get Single
'success' => ['en' => 'Successfully Retrieved Other Item'],
'warning' => ['en' => 'No Other Item with that ID Found'],
'error' => ['en' => 'Error: Retrieving Other Item'],
],
1 => [ // Get Multiple
'info' => ['en' => 'No Results Found'],
'success' => ['en' => 'Successfully Retrieved Other Items'],
'error' => ['en' => 'Error: Retrieving Other Items'],
],
2 => [ // Insert
'success' => ['en' => 'Successfully Created Other Item'],
'error' => ['en' => 'Error: Creating Other Item'],
],
3 => [ // Update
'success' => ['en' => 'Successfully Updated Other Item'],
'error' => ['en' => 'Error: Updating Other Item'],
],
4 => [ // Delete
'success' => ['en' => 'Successfully Deleted Other Item'],
'error' => ['en' => 'Error: Deleting Other Item'],
],
5 => ['redirect' = ['en' => 'Depricated: This route is depricated']],
6 => ['error' = ['en' => 'Error: Custom Error Message']],
7 => ['error' = ['en' => 'Error: Another Custom Error Message']],
8 => ['error' = ['en' => 'Error: And Another Custom Error Message']],
],
...
];
public static function getTests() {
return [
'items_insert' => [
'label' => 'Insert Item',
'route' => '/items/insert',
'method' => 'POST',
'params' => [
'name' => 'TestData',
],
'expect' => [
'status' => 'success',
],
],
'items_get' => [
'label' => 'Get Item',
'route' => '/items/{items_insert.body.id}',
'method' => 'GET',
'params' => [],
'expect' => [
'status' => 'success',
],
],
'items_delete' => [
'label' => 'Delete Item',
'route' => '/items/delete/',
'method' => 'POST',
'params' => [
'id' => '{items_insert.body.id}'
],
'expect' => [
'status' => 'success',
],
],
];
}
$config->tests[
'items_get' => [
'label' => 'Get Example',
'route' => '/items/123',
'params' => [],
'expect' => [
'code' => '1-200',
],
],
'items_get' => [
'label' => 'Get Example',
'route' => '/items/123',
'params' => [],
'expect' => [
'body.id[>]' => 12,
],
],
...
];
$config->rateLimits = [
'driver' => 'file',
'fileDirectory' => __DIR__.'/rate_limits',
'excludeTests' => false,
'default' => [
'by' => 'ip',
'limit' => 10,
'within' => 1,
'hook' => 'configure',
'excludeTests' => false
]
];
$config->routes = [
'/auth/login' => [
'label' => 'Auth Login',
'controller' => 'Auth::login',
'access' => 'public',
'methods' => 'POST',
'limits' => [
'by' => 'ip',
'limit' => 1,
'within' => 3,
'excludeTests' => false
],
'params' => [
'email' => [
'
Spry::addHook(string $hookName, string|callable $controller, [ mixed $extraData = null [, int $order = 0 ]] ) : void
Spry::addHook('configure', 'Spry\\SpryComponent\\MyComponent::myMethod', ['bar' => 345], 100);
Spry::runHook(string $hookName, [ mixed $data ] ) : void
Spry::runHook('configure',['foo' => 123] [[ mixed $data = null ], mixed $meta = null ] );
class MyComponent
public static function myMethod($data = null, $meta = null, $extraData = null) {
$data // [foo => 123]
$meta // null
$extraData // [bar => 345]
// Do Stuff
}
Spry::addFilter(string $filterName, string|callable $controller, [ mixed $extraData = null [, int $order = 0 ]] ) : void
Spry::addFilter('configure', 'Spry\\SpryComponent\\MyComponent::myMethod', ['bar' => 345], 100);
Spry::runFilter(string $filterName, [[ mixed $data = null ], mixed $meta = null ] ) : void
$config = Spry::runFilter('configure', $config, ['component' => 'someComponent', 'var' => 'abc']);
class MyComponent
public static function myMethod($config = null, $meta = null, $extraData = null) {
$config // $config
$meta // ['component' => 'someComponent', 'var' => 'abc']
$extraData // [bar => 345]
// Do Stuff
return $config;
}