Skip to content

Guidelines and best practises

Salomon edited this page Dec 13, 2016 · 15 revisions

What resolves CUIF?

As their name tells Console User Interface Framework. Just user interface issues. When you work with CUIF, you DO NOT have to link your application to your interface. Although you need a subclass of Application, it is a user interface application. Your real application must be islated of your user interface implementation. Tomorrow you may use WebOS for give to user an web accesible UI. So, when things are done well, make It becomes an easy task.

Where I should put my code?

Well. You must make your application isolated. Build it with a Object Oriented Schema. If you already have an application, make at least classes with static methods such as a facade for your application, or like an abstraction layer.

Is a good practise put your application behind a webservice or a socket level service. I want to recommend it, because with CUIF you can write esaily an socket server, and salodev repo provides to you an abstraction class of socket.

If you goin to use an webservice or socket service, make an abstraction class of your servie. It will help to you while be time of move the service of server, change its hostname, its port, or anyelse. I recommend that you use it with an static mehod.

This example demostrates how you can isolate your UI app of your real application:

class MyLoginWindow extends \cuif\Window {
    public function init(array $params = array()) {
        $this->user = $this->createInputBox(0, 1, 'Username');
        $this->pass = $this->createInputBox(0, 2, 'Password');
        $this->createButton(0,3, 'Login')->bind('press', function() {
            MyService::Login($this->user->value, $this->pass->value);
        });
    }
}

Note that, on press "login" button, I am not making any query to database for validations and authentication. I delegates it to my real application. Because the login can becomes also form a web page.

Digging in MyService example class you can find other interesting things:

class MyService {
    static public function Login($user, $pass) {
        return self::CallService('login', array($user, $pass));
    }
    static public function CallService($serviceName, array $parameters = array()) {
        $deferred = new \salodev\Deferred();
        $socket = self::GetCreateSocket();
        $string = json_encode(array($serviceName, $parameters));
        $socket->writeAndReadAsync($string, function($return) use ($deferred) {
            // Here you can validate format of service response, and look for any error,
            // so you may reject $deferred instance.
            $deferred->resolve($return);
        });
        return $deferred;
    }
}
Clone this wiki locally