Skip to content

Commit

Permalink
Merge pull request #3 from chocofamilyme/laravel_8
Browse files Browse the repository at this point in the history
add support laravel 8
  • Loading branch information
Vadim89 authored Oct 28, 2020
2 parents 68f3867 + 8ab10c3 commit 4808ea1
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 69 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: PHP Composer

on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1

- name: Validate composer.json and composer.lock
run: composer validate

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run phpcs
run: vendor/bin/phpcs

- name: Run psalm
run: vendor/bin/psalm
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

## 2.0.0
- Laravel 8 support
- Minimum PHP version is set to 7.4
- Methods flush first argument make required
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
}
],
"require": {
"php": ">= 7.0.0",
"illuminate/log": "^6.0|^7.0"
"php": ">= 7.4.0",
"illuminate/log": "^8.0",
"illuminate/http": "^8.0",
"illuminate/routing": "^8.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.5",
"vimeo/psalm": "^3.18"
},
"autoload": {
"psr-4": {
Expand Down
31 changes: 31 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
<description>The coding standard for PHP_CodeSniffer itself.</description>
<arg name="colors"/>
<arg name="extensions" value="php" />
<arg name="error-severity" value="1" />
<arg name="warning-severity" value="6"/>

<rule ref="PSR12">
<exclude name="PSR2.Methods.MethodDeclaration.Underscore"/>
</rule>

<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="120"/>
<property name="absoluteLineLimit" value="0"/>
</properties>
</rule>
<rule ref="Generic.Files.LineEndings">
<severity>0</severity>
</rule>
<rule ref="Generic.PHP.ForbiddenFunctions">
<properties>
<property name="forbiddenFunctions" type="array" value="eval=>NULL,dd=>NULL,die=>NULL,var_dump=>NULL,sizeof=>count,delete=>unset,print=>echo,create_function=>NULL"/>
</properties>
</rule>

<file>./src</file>

<exclude-pattern>vendor</exclude-pattern>
</ruleset>
18 changes: 18 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<PropertyNotSetInConstructor errorLevel="suppress" />
</issueHandlers>
</psalm>
2 changes: 1 addition & 1 deletion src/Facades/Pinba.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ protected static function getFacadeAccessor()
{
return 'pinba';
}
}
}
20 changes: 15 additions & 5 deletions src/Middlewares/RightUrlMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@

use Chocofamilyme\LaravelPinba\Facades\Pinba;
use Closure;
use Illuminate\Http\Request;

class RightUrlMiddleware
{
const UNKNOWN_SCRIPT_NAME = '<unknown>';
private const UNKNOWN_SCRIPT_NAME = '<unknown>';

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param Closure $next
* @param Request $request
* @param Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
$return = $next($request);
Pinba::setScriptName($request->route()->uri);

$uri = self::UNKNOWN_SCRIPT_NAME;
if ($route = $request->route()) {
$uri = is_string($route) ? $route : $route->uri;
}

Pinba::setScriptName($uri);

return $return;
}
}
}
31 changes: 13 additions & 18 deletions src/Profiler/FileDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

namespace Chocofamilyme\LaravelPinba\Profiler;

use Log;
use Illuminate\Support\Facades\Log;

class FileDestination implements ProfilerInterface
{
/**
* @var array
*/
private $timers = [];
private array $timers = [];

/**
* Creates a timer which should be stopped somewhere
Expand All @@ -24,17 +21,17 @@ class FileDestination implements ProfilerInterface
public function startTimer(string $group, string $type, string $method, string $category): int
{
$tags = [
'group' => $group,
'type' => $type,
'method' => $method,
'group' => $group,
'type' => $type,
'method' => $method,
'category' => $category,
];

$timerId = count($this->timers);

$this->timers[$timerId] = [
'startTime' => microtime(true),
'tags' => $tags,
'tags' => $tags,
];

return $timerId;
Expand All @@ -48,7 +45,7 @@ public function startTimer(string $group, string $type, string $method, string $
public function stopTimer(int $timerId): void
{
if (isset($this->timers[$timerId])) {
$stopTime = microtime(true);
$stopTime = microtime(true);
$timeDifference = $stopTime - $this->timers[$timerId]['startTime'];
Log::info('Timer ' . print_r($this->timers[$timerId]['tags'], true) . ' : ' . $timeDifference . ' секунд');
unset($this->timers[$timerId]);
Expand Down Expand Up @@ -83,22 +80,20 @@ public function setScriptName(string $url): void
*
* @return array
*/
public function getTimers()
public function getTimers(): array
{
return $this->timers;
}

/**
* Useful when you need to send request data to the server immediately (for long running scripts)
*
* @param string|null $scriptName
* @param int|null $flag
* @param string $scriptName
* @param int|null $flag
*/
public function flush(?string $scriptName = null, ?int $flag = null): void
public function flush(string $scriptName, ?int $flag = null): void
{
if ($scriptName) {
$this->setScriptName($scriptName);
}
$this->setScriptName($scriptName);
$this->stopAllTimers();
}
}
}
8 changes: 4 additions & 4 deletions src/Profiler/NullDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ public function setScriptName(string $url): void
*
* @return array
*/
public function getTimers()
public function getTimers(): array
{
return [];
}

/**
* Useful when you need to send request data to the server immediately (for long running scripts)
*
* @param string|null $scriptName
* @param string $scriptName
* @param int|null $flag
*/
public function flush(?string $scriptName = null, ?int $flag = null): void
public function flush(string $scriptName, ?int $flag = null): void
{
}
}
}
37 changes: 25 additions & 12 deletions src/Profiler/PinbaDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

namespace Chocofamilyme\LaravelPinba\Profiler;

use Log;
use Illuminate\Support\Facades\Log;

class PinbaDestination implements ProfilerInterface
{
/**
* @var array
*/
private $timers = [];
private array $timers = [];

/**
* PinbaDestination constructor.
Expand All @@ -21,6 +18,8 @@ public function __construct()

/**
* Initialize pinba with some configuration
*
* @psalm-suppress UndefinedFunction
*/
protected function initializePinba()
{
Expand All @@ -45,14 +44,15 @@ protected function initializePinba()
* @param string $method
* @param string $category
*
* @psalm-suppress UndefinedFunction
* @return int It's the timerId
*/
public function startTimer(string $group, string $type, string $method, string $category): int
{
$tags = [
'group' => $group,
'type' => $type,
'method' => $method,
'group' => $group,
'type' => $type,
'method' => $method,
'category' => $category,
];

Expand All @@ -66,6 +66,8 @@ public function startTimer(string $group, string $type, string $method, string $
/**
* Stop the timer by timerId
*
* @psalm-suppress UndefinedFunction
*
* @param int $timerId
*/
public function stopTimer(int $timerId): void
Expand All @@ -80,6 +82,8 @@ public function stopTimer(int $timerId): void

/**
* Stop all timers
*
* @psalm-suppress UndefinedFunction
*/
public function stopAllTimers(): void
{
Expand All @@ -92,6 +96,8 @@ public function stopAllTimers(): void
* is served by single script. With this method you can rewrite it
*
* @param string $url
*
* @psalm-suppress UndefinedFunction
*/
public function setScriptName(string $url): void
{
Expand All @@ -103,19 +109,26 @@ public function setScriptName(string $url): void
*
* @return array
*/
public function getTimers()
public function getTimers(): array
{
return $this->timers;
}

/**
* Useful when you need to send request data to the server immediately (for long running scripts)
*
* @param string|null $scriptName
* @param string $scriptName
* @param int|null $flag
*
* @psalm-suppress UndefinedFunction
* @psalm-suppress UndefinedConstant
*/
public function flush(?string $scriptName = null, ?int $flag = null): void
public function flush(string $scriptName, ?int $flag = null): void
{
if (null === $flag) {
$flag = PINBA_FLUSH_ONLY_STOPPED_TIMERS;
}

pinba_flush($scriptName, $flag);
}
}
}
8 changes: 4 additions & 4 deletions src/Profiler/ProfilerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public function setScriptName(string $url): void;
*
* @return array
*/
public function getTimers();
public function getTimers(): array;

/**
* Useful when you need to send request data to the server immediately (for long running scripts)
*
* @param string|null $scriptName
* @param string $scriptName
* @param int|null $flag
*/
public function flush(?string $scriptName = null, ?int $flag = null): void;
}
public function flush(string $scriptName, ?int $flag = null): void;
}
Loading

0 comments on commit 4808ea1

Please sign in to comment.