Skip to content

Commit 590c712

Browse files
author
Dmitry Tarasov
committed
API transports stubs
1 parent a4e9d36 commit 590c712

6 files changed

Lines changed: 206 additions & 1 deletion

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Stackify\Log\Entities\Transport;
4+
5+
use Stackify\Log\Entities\Api\LogMsg;
6+
7+
class ApiMessage
8+
{
9+
10+
/**
11+
* @var \Stackify\Log\Entities\Api\LogMsg[]
12+
*/
13+
public $Log;
14+
15+
/**
16+
* @var string
17+
*/
18+
public $Logger;
19+
20+
/**
21+
* @var string
22+
*/
23+
public $AppName;
24+
25+
/**
26+
* @var string
27+
*/
28+
public $EnvironmentName;
29+
30+
/**
31+
* @param string $loggerName
32+
* @param string $appName
33+
* @param string $environmentName
34+
* @param \Stackify\Log\Entities\Api\LogMsg[] $logMsgs
35+
*/
36+
public function __construct($loggerName, $appName, $environmentName, array $logMsgs)
37+
{
38+
// @TODO modify when API will exist
39+
$this->Logger = $loggerName;
40+
$this->AppName = $appName;
41+
$this->EnvironmentName = $environmentName;
42+
$this->Log = $logMsgs;
43+
}
44+
45+
}

src/Stackify/Log/MessageBuilder.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ public function getAgentMessage(LogEntryInterface $logEntry)
4242
*/
4343
public function getApiMessage(array $logMsgs)
4444
{
45-
$message = new ApiMessage($this->loggerName, $this->appName, $logMsgs);
45+
$message = new ApiMessage(
46+
$this->loggerName,
47+
$this->appName,
48+
$this->environmentName,
49+
$logMsgs
50+
);
4651
return $this->encodeJSON($message);
4752
}
4853

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Stackify\Log\Transport;
4+
5+
use Stackify\Log\Entities\LogEntryInterface;
6+
7+
abstract class AbstractApiTransport extends AbstractTransport
8+
{
9+
10+
private $queue = array();
11+
private $commonOptions = array('proxy');
12+
protected $apiKey;
13+
protected $proxy;
14+
15+
public function __construct($apiKey, array $options = array())
16+
{
17+
parent::__construct();
18+
$this->apiKey = $apiKey;
19+
// @TODO validate & merge
20+
$this->extractOption($options, 'proxy');
21+
// type, host, port, user, pass
22+
// @TODO shutdown case
23+
// register_shutdown_function(array($this, 'finish'));
24+
}
25+
26+
public function addEntry(LogEntryInterface $logEntry)
27+
{
28+
$this->queue[] = $this->messageBuilder->createLogMsg($logEntry);
29+
}
30+
31+
public function finish()
32+
{
33+
if (!empty($this->queue)) {
34+
$json = $this->messageBuilder->getApiMessage($this->queue);
35+
$this->send($json);
36+
}
37+
}
38+
39+
protected abstract function send($data);
40+
41+
protected abstract function getAllowedOptions();
42+
43+
protected function extractOption($options, $optionName)
44+
{
45+
$allowed = array_merge($this->commonOptions, $this->getAllowedOptions());
46+
if (isset($options[$optionName]) && in_array($optionName, $allowed)) {
47+
$this->$optionName = $options[$optionName];
48+
}
49+
}
50+
51+
protected function getApiHeaders()
52+
{
53+
return array(
54+
'X-Stackify-PV' => 'V1',
55+
'X-Stackify-Key' => $this->apiKey,
56+
);
57+
}
58+
59+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Stackify\Log\Transport\Config;
4+
5+
// @TODO set release values
6+
class Api
7+
{
8+
const API_BASE_URL = '';
9+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Stackify\Log\Transport;
4+
5+
use Stackify\Exceptions\InitializationException;
6+
7+
/**
8+
* Transport sends data using cURL PHP extension
9+
*/
10+
class CurlTransport extends AbstractApiTransport
11+
{
12+
13+
public function __construct($apiKey, array $options = array())
14+
{
15+
parent::__construct($apiKey, $options);
16+
if (!function_exists('curl_init')) {
17+
throw new InitializationException('cURL PHP extension is not available');
18+
}
19+
}
20+
21+
protected function getAllowedOptions()
22+
{
23+
return array();
24+
}
25+
26+
protected function getTransportName()
27+
{
28+
return 'CurlTransport';
29+
}
30+
31+
protected function send($data)
32+
{
33+
echo $data;
34+
}
35+
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Stackify\Log\Transport;
4+
5+
use Stackify\Log\Transport\Config\Api;
6+
7+
/**
8+
* @TODO
9+
*/
10+
class ExecTransport extends AbstractApiTransport
11+
{
12+
13+
private $curlPath = 'curl';
14+
15+
public function __construct($apiKey, array $options = array())
16+
{
17+
parent::__construct($apiKey, $options);
18+
if (isset($options['curlPath'])) {
19+
$this->curlPath = $options['curlPath'];
20+
}
21+
}
22+
23+
protected function getAllowedOptions()
24+
{
25+
return array(
26+
'curlPath',
27+
);
28+
}
29+
30+
protected function getTransportName()
31+
{
32+
return 'ExecTransport';
33+
}
34+
35+
protected function send($data)
36+
{
37+
echo $data;
38+
// @TODO
39+
$url = Api::API_BASE_URL;
40+
$cmd = "$this->curlPath -X POST";
41+
foreach ($this->getApiHeaders() as $name => $value) {
42+
$cmd .= " -H '$name: $value'";
43+
}
44+
// -d = data, -m = max time
45+
$cmd .= " -d '$data' '$url' -m 5";
46+
// exec will return immediately while curl will run in the background
47+
$cmd .= ' > /dev/null 2>&1 &';
48+
exec($cmd);
49+
}
50+
51+
}

0 commit comments

Comments
 (0)