PHP code example of tina4stack / tina4php

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

    

tina4stack / tina4php example snippets


/**
* @description Swagger Description
* @tags Example,Route
*/
\Tina4\Get::add("/hello-world", function(\Tina4\Response $response){
    return $response ("Hello World!");
});

/**
 * Example of route calling class , method
 * Note the swagger annotations will go in the class
 */
\Tina4\Get::add("/test/class", ["Example", "route"]);



class Example
{
    public function someThing() {
        return "Yes!";
    }
    
    /**
     * @param \Tina4\Response $response
     * @return array|false|string
     * @description Hello Normal -> see Example.php route
     */
    public function route (\Tina4\Response $response) {
        return $response ("OK!");
    }

}


global $DBA;
$DBA = new \Tina4\DataSQLite3("test.db");
  

class Address extends \Tina4\ORM
{
    public $id;
    public $address;
    public $customerId;

    //Link up customerId => Customer object
    public $hasOne = [["Customer" => "customerId"]];
}

class Customer extends \Tina4\ORM
{
    public $primaryKey = "id";
    public $id;
    public $name;

    //Primary key id maps to customerId on Address table
    public $hasMany = [["Address" => "customerId"]];
}




$customer = (new Customer());
$customer->id = 1;
$customer->name = "Test";
$customer->save();

$address = (new Address());
$address->address = "1 Street";
$address->customerId = 1;
$address->save();

$customer = (new Customer());
$customer->addresses[0]->address = "Another Address";
$customer->addresses[0]->address->save(); //Save the address
$customer->load("id = 1");

$address = new Address();
$address->load("id = 1");
$address->address = "New Street Address";
$address->customer->name = "New Name for customer"
$address->customer->save(); //save the customer
$address->save();



/**
 * Some function to add numbers
 * @tests
 *   assert (1,1) === 2, "1 + 1 = 2"
 *   assert is_integer(1,1) === true, "This should be an integer"
 */
function add ($a,$b) {
    return $a+$b;
}


//Example of the triggered event, notice the sleep timer which should shut down most code on windows or linux making PHP wait for the result.

\Tina4\Thread::addTrigger("me", static function($name, $sleep=1, $hello="OK"){
    $iCount = 0;
    while ($iCount < 10) {
        file_put_contents("./log/event.log", "Hello {$name} {$hello}!\n", FILE_APPEND);
        sleep($sleep);
        $iCount++;
    }
});

\Tina4\Get::add("/test", function(\Tina4\Response $response){
    
    \Tina4\Thread::trigger("me", ["Again", 1, "Moo!"]);

    return $response("OK!");
});

\Tina4\Get::add("/test/slow", function(\Tina4\Response $response){

    \Tina4\Thread::trigger("me", ["Hello", 3]);

    return $response("OK!");
});

Route::get("/phpinfo", function(Response $response){
  ob_start();
  phpinfo();
  $data = ob_get_contents();
  ob_clean();
  return $response($data, HTTP_OK, TEXT_HTML);
});

php bin\tina4

docker build . -t tina4stack/php:7.4

 docker push tina4stack/php:7.4

brew tap kabel/php-ext
brew install php-imap