Skip to content

Commit

Permalink
Initial commit, build 6830
Browse files Browse the repository at this point in the history
  • Loading branch information
khelle committed Sep 30, 2016
0 parents commit 2d1bc59
Show file tree
Hide file tree
Showing 44 changed files with 4,134 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.idea
vendor/
data/environment/.env
data/log
data/storage
data/temp
composer.lock
composer.phar
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Kraken Demo Application - Chat

[![Total Downloads](https://poser.pugx.org/kraken-php/kraken/downloads)](https://packagist.org/packages/kraken-php/demo-chat)
[![Latest Stable Version](https://poser.pugx.org/kraken-php/framework/v/stable)](https://packagist.org/packages/kraken-php/framework)
[![Latest Unstable Version](https://poser.pugx.org/kraken-php/framework/v/unstable)](https://packagist.org/packages/kraken-php/framework)
[![License](https://poser.pugx.org/kraken-php/framework/license)](https://packagist.org/packages/kraken-php/framework)
[![Gitter](https://badges.gitter.im/kraken-php/framework.svg)](https://gitter.im/kraken-php/framework?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![@kraken_php on Twitter](https://img.shields.io/badge/twitter-%40kraken__php-blue.svg)](https://twitter.com/kraken_php)

> **Note:** This repository contains pre-configured distributed chat application based on [Kraken Framework](https://github.com/kraken-php/framework).
<br>
<p align="center">
<img src="https://avatars2.githubusercontent.com/u/15938282?v=3&s=150" />
</p>

## Description

This repository demonstrates exemplary implementation of chat using HTTP and Websocket servers in PHP using [Kraken Framework](https://github.com/kraken-php/framework) components.

## Architecture

<p align="center">
<img src="https://docs.google.com/uc?export=download&id=0B_FVuB10kPjVWlZMeDFRaDBoTE0" width="453" height="261" />
</p>

## Screenshots

<p align="center">
<img src="https://docs.google.com/uc?export=download&id=0B_FVuB10kPjVOC1UM1hvaVNPS2M" width="880" height="512" />
</p>

## Requirements

* PHP-5.5, PHP-5.6 or PHP-7.0+,
* [Pthreads](http://php.net/manual/en/book.pthreads.php) extension enabled (only if you want to use threading),
* UNIX or ~~Windows~~ OS.

## Installation and Official Documentation

To install this application skeleton, please go to desired location to store project, then call composer:

```
composer create-project --prefer-dist kraken-php/demo-chat .
```

Documentation for the framework can be found in the [official documentation][2] page.

## Starting Project

### Basic Start

To start project, first run `kraken.server` instance.

$> kraken.server

Then, check if connection is working in another terminal window:

$> kraken server:ping

If everything works correctly, as final step run the application using:

$> kraken project:create

After project has been created successfully, go to `http://localhost:6080` address in your browser and you should be able
to see and use examplary chat.

To close whole project, use:

$> kraken project:destroy

If you have problems with configuring console-server connection, you can also try alternative start.

### Alternative Start

To start project directly, without console support, use:

$> php ./data/autorun/kraken.process undefined HttpBroker HttpBroker

**WARNING** This method will be deprecated in upcoming ver 0.4.

## Contributing

This library is pre-configured project application for Kraken Framework. To make contributions, please go to [framework repository][3].

## License

Kraken Framework is open-sourced software licensed under the [MIT license][6]. The documentation is provided under [FDL-1.3 license][7].

[1]: http://kraken-php.com
[2]: http://kraken-php.com/docs
[3]: http://kraken-php.com/getting_started
[4]: http://kraken-php.com/faq
[5]: http://kraken-php.com/docs/contributions
[6]: http://opensource.org/licenses/MIT
[7]: https://www.gnu.org/licenses/fdl-1.3.en.html
[8]: https://groups.google.com/forum/#!forum/kraken-php
38 changes: 38 additions & 0 deletions app/Command/Resource/ResourceReadCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Command\Resource;

use Kraken\Runtime\Command\Command;
use Kraken\Runtime\Command\CommandInterface;
use Kraken\Throwable\Exception\Runtime\ReadException;

class ResourceReadCommand extends Command implements CommandInterface
{
/**
* @override
* @inheritDoc
*/
protected function command($params = [])
{
$type = $params['type'];
$file = $params['file'];
$path = $this->runtime->getCore()->getBasePath() . '/public';

switch ($type)
{
case 'html':
$path .= '/' . $file;
break;

default:
$path .= '/' . $type . '/'. $file;
}

if (!file_exists($path))
{
throw new ReadException("File [$path] does not exist!");
}

return file_get_contents($path);
}
}
122 changes: 122 additions & 0 deletions app/Component/Http/Action/HttpAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace App\Component\Http\Action;

use App\Component\Job\JobQueueInterface;
use App\Component\Job\Throwable\JobQueueNotFoundException;
use Kraken\Network\Http\HttpRequestInterface;
use Kraken\Network\Http\HttpResponse;
use Kraken\Network\NetworkConnectionInterface;
use Kraken\Network\NetworkMessageInterface;
use Kraken\Promise\Promise;
use Kraken\Promise\PromiseInterface;

class HttpAction implements HttpActionInterface
{
/**
* @var JobQueueInterface|null
*/
protected $queue;

/**
* @param JobQueueInterface|null $queue
*/
public function __construct(JobQueueInterface $queue = null)
{
$this->queue = $queue;
}

/**
*
*/
public function __destruct()
{
unset($this->queue);
}

/**
* @override
* @inheritDoc
*/
public function queue($command, $params)
{
if ($this->queue === null)
{
return Promise::doReject(
new JobQueueNotFoundException('There is no queue.')
);
}

return $this->queue->queue($command, $params);
}

/**
* @override
* @inheritDoc
*/
public function action(HttpRequestInterface $request)
{
return new HttpResponse(404);
}

/**
* @override
* @inheritDoc
*/
public function handleConnect(NetworkConnectionInterface $conn)
{}

/**
* @override
* @inheritDoc
*/
public function handleDisconnect(NetworkConnectionInterface $conn)
{}

/**
* @override
* @inheritDoc
*/
public function handleMessage(NetworkConnectionInterface $conn, NetworkMessageInterface $message)
{
$response = $this->action($message);

if ($response instanceof PromiseInterface)
{
$response->done(
function($response) use($conn) {
$rep = new HttpResponse(200, [], $response);
$conn->send($rep);
$conn->close();
},
function($ex) use($conn) {
$rep = new HttpResponse(404);
$conn->send($rep);
$conn->close();
},
function($ex) use($conn) {
$rep = new HttpResponse(500);
$conn->send($rep);
$conn->close();
}
);
}
else
{
if (is_string($response))
{
$response = new HttpResponse(200, [], $response);
}

$conn->send($response);
$conn->close();
}
}

/**
* @override
* @inheritDoc
*/
public function handleError(NetworkConnectionInterface $conn, $ex)
{}
}
18 changes: 18 additions & 0 deletions app/Component/Http/Action/HttpActionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Component\Http\Action;

use App\Component\Job\JobQueueInterface;
use Kraken\Network\Http\HttpRequestInterface;
use Kraken\Network\Http\HttpResponseInterface;
use Kraken\Network\NetworkComponentInterface;
use Kraken\Promise\PromiseInterface;

interface HttpActionInterface extends NetworkComponentInterface, JobQueueInterface
{
/**
* @param HttpRequestInterface $request
* @return HttpResponseInterface|string|PromiseInterface $response
*/
public function action(HttpRequestInterface $request);
}
18 changes: 18 additions & 0 deletions app/Component/Http/Action/Index/IndexAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Component\Http\Action\Index;

use App\Component\Http\Action\HttpAction;
use Kraken\Network\Http\HttpRequestInterface;

class IndexAction extends HttpAction
{
/**
* @override
* @inheritDoc
*/
public function action(HttpRequestInterface $request)
{
return $this->queue('res:read', [ 'type' => 'html', 'file' => 'index.html' ]);
}
}
23 changes: 23 additions & 0 deletions app/Component/Http/Action/Resource/ResourceAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Component\Http\Action\Resource;

use App\Component\Http\Action\HttpAction;
use Kraken\Network\Http\HttpRequestInterface;

class ResourceAction extends HttpAction
{
/**
* @override
* @inheritDoc
*/
public function action(HttpRequestInterface $request)
{
$target = $request->getTarget();
$target = ltrim($target, '/');

list($type, $file) = explode('/', $target);

return $this->queue('res:read', [ 'type' => $type, 'file' => $file ]);
}
}
Loading

0 comments on commit 2d1bc59

Please sign in to comment.