PHP code example of meta-tech / pws-server

1. Go to this page and download the library: Download meta-tech/pws-server 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/ */

    

meta-tech / pws-server example snippets


namespace MetaTech\PwsServer;

use MetaTech\Silex\Application as App;
use MetaTech\Silex\Provider\ControllerServiceProvider as CtrlProvider;
use MetaTech\Silex\Provider\UserProvider;
use MetaTech\Db\PdoWrapper;
use MetaTech\Db\Profile;
use MetaTech\PwsAuth\Authenticator;
use MetaTech\PwsServer\Ctrl\Test;
use MetaTech\PwsServer\Ctrl\WebService;
use MetaTech\PwsServer\Ctrl\OtherWebService;

class Application extends App
{
    protected function setServices()
    {
        $app = $this;
        $app['ws.authenticator'] = function ($app) {
            return new Authenticator($app['config']['pwsauth']);
        };
        $app['pdo'] = function ($app) {
            return new PdoWrapper(new Profile($app['config']['db']['default']));
        };
        $app['user.provider'] = function ($app) {
            return new UserProvider($app['pdo']);
        };
    }

    protected function routingDefinition()
    {
        $this->register(new CtrlProvider(Test::class           , [$this], '/'));
        $this->register(new CtrlProvider(WebService::class     , [$this], '/ws'));
        $this->register(new CtrlProvider(OtherWebService::class, [$this], '/ws/deep'));
    }
}

use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Request;
use MetaTech\PwsServer\Ws\Controller;

class WebService extends Controller
{
    public function index(Request $request)
    {
        $done = true;
        $msg  = 'this is index';
        return $this->response($done, $msg);
    }

    public function routing(ControllerCollection $collection) : ControllerCollection
    {
        $collection = parent::routing($collection);
        $_          = $this->ns();

        $collection->match('/', "$_:index");

        return $collection;
    }
}

namespace MetaTech\PwsServer\Ws;

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use MetaTech\PwsAuth\Authenticator;
use MetaTech\Silex\Ws\Authentication as BaseAuthentication;
use MetaTech\Silex\Provider\UserProvider;

class Authentication extends BaseAuthentication
{
    protected $userProvider;

    public function __construct(Session $session, Authenticator $authenticator, PasswordEncoderInterface $passEncoder = null, UserProvider $userProvider)
    {
        parent::__construct($session, $authenticator, $passEncoder);
        $this->userProvider = $userProvider;
    }

    public function checkUser($login, $password, $key, PasswordEncoderInterface $passEncoder = null)
    {
        $done = false;
        try {
            if (!is_null($passEncoder)) {
                $user = $this->userProvider->loadUserByUsername($login);
                $salt = $this->authenticator->getUserSalt($login);
                $done = $user->key == $key && $passEncoder->encodePassword($password, $salt) == $user->getPassword();
            }
        }
        catch(\Exception $e) {
            //~ var_dump($e->getTraceAsString());
        }
        return $done;
    }
}

namespace MetaTech\PwsServer\Ws;

use Silex\Application;
use MetaTech\Silex\Ws\Controller as BaseController;
use MetaTech\PwsServer\Ws\Authentication;

class Controller extends BaseController
{
    public function __construct(Application $app = null)
    {
        $this->session = $app['session'];
        $this->handler = new Authentication($this->session, $app['ws.authenticator'], $app['security.encoder.pbkdf2'], $app['user.provider']);
    }
}