PHP code example of dczajkowski / colloquy

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

    

dczajkowski / colloquy example snippets




namespace App;

class SessionIdentifierResolver implements \Colloquy\IdentifierResolverInterface
{
    public function get($controller): string
    {
        return // code to get session id 
    }
}



\Colloquy\Colloquy::bind(
    'session',
    new \App\SessionIdentifierResolver,
    new \Colloquy\Drivers\FileDriver($pathToWritableDirectory)
);



namespace App\Http\Controllers;

/** @ColloquyContext('session') */
class FormController
{
    use \Colloquy\ColloquyContexts;
    
    /** @ColloquyPersist */
    protected $user;

    /** @ColloquyBegin */
    private function step1()
    {
        $this->user = new \App\Models\User;
    }

    private function step2()
    {
        $this->user->name = 'John';
    }

    /** @ColloquyEnd */
    private function step3()
    {
        echo $this->user->name; // John
    }
}



use Colloquy\Colloquy;
use Colloquy\Drivers\FileDriver;

class User
{
   private $name;
   
   public function __construct(string $name)
   {
       $this->name = $name;
   }
}

/** Starting a Conversation */

$wrapper = new Colloquy(new FileDriver('storage'));

$homeContext = $wrapper->context('Home');
$formContext = $wrapper->context('Form');

/** Primitive types */

$homeContext->add('Joe', 'name');
$homeContext->add('ilovecats', 'password');
$formContext->add('Jane', 'name');

$name = $homeContext->get('name'); // Joe

$homeContext->set('John', 'name');

$name = $homeContext->get('name'); // John

/** Objects */

$user = new User('Jack');

$homeContext->add($user, 'user');

var_dump($user); // User { name: "Jack" }

$user = $homeContext->get('user');

var_dump($user); // User { name: "Jack" }

/** Ending a Conversation */

$wrapper->end('Form');
$wrapper->end('Home');