Skip to content

Commit

Permalink
Merge pull request #3 from felix-app/master
Browse files Browse the repository at this point in the history
New features for laravel-request-library
  • Loading branch information
andersao committed Jan 29, 2016
2 parents 78f347b + 500c321 commit 08c4c33
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 170 deletions.
1 change: 1 addition & 0 deletions src/Prettus/RequestLogger/Handler/HttpLoggerHandler.php
Expand Up @@ -12,6 +12,7 @@ class HttpLoggerHandler extends RotatingFileHandler implements HandlerInterface

public function __construct($filename = null, $maxFiles = 0, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
{

$filename = !is_null($filename) ? $filename : config("request-logger.logger.file", storage_path("logs/http.log") );
parent::__construct($filename, $maxFiles, $level, $bubble, $filePermission, $useLocking);
}
Expand Down
47 changes: 47 additions & 0 deletions src/Prettus/RequestLogger/Helpers/BaseInterpolation.php
@@ -0,0 +1,47 @@
<?php namespace Prettus\RequestLogger\Helpers;

use Prettus\RequestLogger\Contracts\Interpolable;

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Class BaseInterpolation
* @package Prettus\RequestLogger\Helpers
*/
abstract class BaseInterpolation implements Interpolable {

/**
* @var Request
*/
protected $request;

/**
* @var Response
*/
protected $response;

/**
* @param Request $request
*/
public function setRequest(Request $request)
{
$this->request = $request;
}

/**
* @param Response $response
*/
public function setResponse(Response $response)
{
$this->response = $response;
}

/**
* @param string $raw
*/
protected function escape($raw)
{
return preg_replace('/\s/', "\\s", $raw);
}
}
34 changes: 19 additions & 15 deletions src/Prettus/RequestLogger/Helpers/Benchmarking.php
Expand Up @@ -4,7 +4,8 @@
* Class Benchmarking
* @package Prettus\RequestLogger\Helpers
*/
class Benchmarking {
class Benchmarking
{

/**
* @var array
Expand All @@ -15,10 +16,11 @@ class Benchmarking {
* @param $name
* @return mixed
*/
public static function start($name){
public static function start($name)
{
$start = microtime(true);

self::$timers[$name] = [
static::$timers[$name] = [
'start'=>$start
];

Expand All @@ -30,21 +32,22 @@ public static function start($name){
* @return float
* @throws \Exception
*/
public static function end($name){
public static function end($name)
{

$end = microtime(true);

if( isset(self::$timers[$name]) && isset(self::$timers[$name]['start']) )
{
if( isset(self::$timers[$name]['duration']) ){
return self::$timers[$name]['duration'];
if( isset(static::$timers[$name]) && isset(static::$timers[$name]['start']) ) {

if( isset(static::$timers[$name]['duration']) ){
return static::$timers[$name]['duration'];
}

$start = self::$timers[$name]['start'];
self::$timers[$name]['end'] = $end;
self::$timers[$name]['duration'] = $end - $start;
$start = static::$timers[$name]['start'];
static::$timers[$name]['end'] = $end;
static::$timers[$name]['duration'] = $end - $start;

return self::$timers[$name]['duration'];
return static::$timers[$name]['duration'];
}

throw new \Exception("Benchmarking '{$name}' not started");
Expand All @@ -55,7 +58,8 @@ public static function end($name){
* @return float
* @throws \Exception
*/
public static function duration($name){
return self::end($name);
public static function duration($name)
{
return static::end($name);
}
}
}
79 changes: 29 additions & 50 deletions src/Prettus/RequestLogger/Helpers/RequestInterpolation.php
@@ -1,44 +1,28 @@
<?php namespace Prettus\RequestLogger\Helpers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Prettus\RequestLogger\Contracts\Interpolable;

/**
* Class RequestInterpolation
* @package Prettus\RequestLogger\Helpers
*/
class RequestInterpolation implements Interpolable {

/**
* @var Request
*/
protected $request;

/**
* @param Request $request
*/
public function setRequest($request)
{
$this->request = $request;
}
class RequestInterpolation extends BaseInterpolation {

/**
* @param string $text
* @return string
*/
public function interpolate($text){
public function interpolate($text)
{

$variables = explode(" ",$text);

foreach( $variables as $variable )
{
$output = [];
preg_match("/{\s*(.+?)\s*}(\r?\n)?/", $variable, $output);
if( isset($output[1]) )
{
$value = $this->resolveVariable($output[0], $output[1]);
$text = str_replace($output[0], $value, $text);
foreach( $variables as $variable ) {
$matches = [];
preg_match("/{\s*(.+?)\s*}(\r?\n)?/", $variable, $matches);
if( isset($matches[1]) ) {
$value = $this->escape($this->resolveVariable($matches[0], $matches[1]));
$text = str_replace($matches[0], $value, $text);
}
}

Expand All @@ -58,14 +42,16 @@ public function resolveVariable($raw, $variable)
"port",
"queryString",
"remoteUser",
"referrer"
"referrer",
'body'
], [
"ip",
"getScheme",
"getPort",
"getQueryString",
"getUser",
"referer"
"referer",
"getContent"
],camel_case($variable));

$server_var = str_replace([
Expand All @@ -86,33 +72,26 @@ public function resolveVariable($raw, $variable)
"HTTP_USER_AGENT"
], strtoupper(str_replace("-","_", $variable)) );

if( method_exists($this->request, $method) )
{
if( method_exists($this->request, $method) ) {
return $this->request->$method();
}
elseif( isset($_SERVER[$server_var]) )
{
} elseif( isset($_SERVER[$server_var]) ) {
return $this->request->server($server_var);
}
else
{
$output = [];
preg_match("/([-\w]{2,})(?:\[([^\]]+)\])?/", $variable, $output);

if( count($output) == 2 )
{
switch($output[0])
{
case "date": $output[] = "clf"; break;
} else {
$matches = [];
preg_match("/([-\w]{2,})(?:\[([^\]]+)\])?/", $variable, $matches);

if( count($matches) == 2 ) {
switch($matches[0]) {
case "date":
$matches[] = "clf";
break;
}
}

if( is_array($output) && count($output) == 3 )
{
list($line, $var, $option) = $output;
if( is_array($matches) && count($matches) == 3 ) {
list($line, $var, $option) = $matches;

switch(strtolower($var))
{
switch(strtolower($var)) {
case "date":

$formats = [
Expand All @@ -133,7 +112,7 @@ public function resolveVariable($raw, $variable)
}
}
}

return $raw;
}
}
}
81 changes: 22 additions & 59 deletions src/Prettus/RequestLogger/Helpers/ResponseInterpolation.php
@@ -1,57 +1,25 @@
<?php namespace Prettus\RequestLogger\Helpers;

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Prettus\RequestLogger\Contracts\Interpolable;

/**
* Class ResponseInterpolation
* @package Prettus\RequestLogger\Helpers
*/
class ResponseInterpolation implements Interpolable {

/**
* @var Response
*/
protected $response = null;

/**
* @var Request
*/
protected $request = null;

/**
* @param Response $response
*/
public function setResponse($response)
{
$this->response = $response;
}

/**
* @param Request $request
*/
public function setRequest($request)
{
$this->request = $request;
}
class ResponseInterpolation extends BaseInterpolation {

/**
* @param string $text
* @return string
*/
public function interpolate($text){

public function interpolate($text)
{
$variables = explode(" ",$text);

foreach( $variables as $variable )
{
$output = [];
preg_match("/{\s*(.+?)\s*}(\r?\n)?/", $variable, $output);
if( isset($output[1]) )
{
$value = $this->resolveVariable($output[0], $output[1]);
$text = str_replace($output[0], $value, $text);
foreach( $variables as $variable ) {
$matches = [];
preg_match("/{\s*(.+?)\s*}(\r?\n)?/", $variable, $matches);
if( isset($matches[1]) ) {
$value = $this->escape($this->resolveVariable($matches[0], $matches[1]));
$text = str_replace($matches[0], $value, $text);
}
}

Expand All @@ -77,25 +45,18 @@ public function resolveVariable($raw, $variable)
"getStatusCode"
],camel_case($variable));

if( method_exists($this->response, $method) )
{
if( method_exists($this->response, $method) ) {
return $this->response->$method();
}
elseif( method_exists($this, $method) )
{
} elseif( method_exists($this, $method) ) {
return $this->$method();
}
else
{
$output = [];
preg_match("/([-\w]{2,})(?:\[([^\]]+)\])?/", $variable, $output);
} else {
$matches = [];
preg_match("/([-\w]{2,})(?:\[([^\]]+)\])?/", $variable, $matches);

if( is_array($output) && count($output) == 3 )
{
list($line, $var, $option) = $output;
if( count($matches) == 3 ) {
list($line, $var, $option) = $matches;

switch(strtolower($var))
{
switch(strtolower($var)) {
case "res":
return $this->response->headers->get($option);
default;
Expand All @@ -110,7 +71,8 @@ public function resolveVariable($raw, $variable)
/**
* @return int
*/
public function getContentLength(){
public function getContentLength()
{

$path = storage_path("framework".DIRECTORY_SEPARATOR."temp");

Expand All @@ -130,11 +92,12 @@ public function getContentLength(){
/**
* @return float|null
*/
public function responseTime(){
public function responseTime()
{
try{
return Benchmarking::duration('application');
}catch (\Exception $e){
return null;
}
}
}
}

0 comments on commit 08c4c33

Please sign in to comment.