Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Mulder committed Jun 7, 2024
1 parent fc5ca38 commit ce6a384
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/analyse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: PHPStan

on: ['push', 'pull_request']

jobs:
test:
runs-on: ubuntu-latest
name: analyse

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
extensions: dom, curl, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Install dependencies
run: composer install --no-interaction

- name: Analyse
run: vendor/bin/phpstan analyse
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
.idea
composer.lock
/vendor
57 changes: 57 additions & 0 deletions Model/Profiler/XhprofProfiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace JustBetter\XhprofProfiler\Model\Profiler;

use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\RuntimeException;
use SpiralPackages\Profiler\Driver\XhprofDriver;
use SpiralPackages\Profiler\Storage\WebStorage;
use Symfony\Component\HttpClient\NativeHttpClient;
use Magento\Framework\App\DeploymentConfig;

class XhprofProfiler
{
public const HEADER = 'X-Xhprof-Enabled';

public const IGNORED_FUNCTIONS_KEY = 'ignored_functions';

public array $tags;

public function __construct(
protected XhprofDriver $driver,
protected DeploymentConfig $deploymentConfig,
array $tags = []
)
{
$this->tags = $tags;
}

public function handle(): void
{
$this->driver->start(
[
self::IGNORED_FUNCTIONS_KEY => ['SpiralPackages\Profiler\Profiler::end'],
]
);
}

/**
* @throws FileSystemException
* @throws RuntimeException
*/
public function terminate(array $tags = []): void
{
$result = $this->driver->end();
$endpoint = $this->deploymentConfig->get('xhprofprofiler/endpoint');
$appName = $this->deploymentConfig->get('xhprofprofiler/app_name');
if (!empty($appName) && !empty($endpoint) && is_string($endpoint) && is_string($appName)) {
$storage = new WebStorage(new NativeHttpClient(), $endpoint);
$storage->store(
$appName,
\array_merge($this->tags ?? [], $tags),
new \DateTimeImmutable(),
$result
);
}
}
}
37 changes: 37 additions & 0 deletions Observer/StartXhprof.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace JustBetter\XhprofProfiler\Observer;

use JustBetter\XhprofProfiler\Model\Profiler\XhprofProfiler;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\Http;
use Throwable;

class StartXhprof implements ObserverInterface
{
public function __construct(
protected XhprofProfiler $profiler
)
{}

public function execute(Observer $observer): void
{
if ($this->isEnabled($observer->getRequest())) {
$this->profiler->handle();
}
}

public function isEnabled(Http $request): bool
{
if ($request->getHeader(XhprofProfiler::HEADER)) {
return filter_var($request->getHeader(XhprofProfiler::HEADER), FILTER_VALIDATE_BOOLEAN);
}

try {
return (bool) getenv('XHPROF_ENABLED');
} catch (Throwable) {
return false;
}
}
}
39 changes: 39 additions & 0 deletions Observer/StopXhprof.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace JustBetter\XhprofProfiler\Observer;

use JustBetter\XhprofProfiler\Model\Profiler\XhprofProfiler;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Throwable;

class StopXhprof implements ObserverInterface
{
public function __construct(
protected XhprofProfiler $profiler
)
{}

public function execute(Observer $observer): void
{
if ($this->isEnabled($observer->getRequest())) {
$this->profiler->terminate(
['route' => $observer->getRequest()->getFullActionName()]
);
}
}

public function isEnabled(Http $request): bool
{
if ($request->getHeader(XhprofProfiler::HEADER)) {
return filter_var($request->getHeader(XhprofProfiler::HEADER), FILTER_VALIDATE_BOOLEAN);
}

try {
return (bool) getenv('XHPROF_ENABLED');
} catch (Throwable) {
return false;
}
}
}
45 changes: 45 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "justbetter/magento2-xhprof-profiler",
"description": "Xhprof profiler integration for Magento",
"keywords": [
"justbetter",
"magento",
"buggregator",
"xhprof"
],
"type": "magento2-module",
"license": "MIT",
"require": {
"php": ">=8.2",
"magento/framework": "*",
"magento/module-config": "^101.2",
"spiral-packages/profiler": "^1.2"
},
"repositories": {
"magento": {
"type": "composer",
"url": "https://repo-magento-mirror.fooman.co.nz/"
}
},
"authors": [
{
"name": "Robin Mulder",
"email": "[email protected]",
"homepage": "https://justbetter.nl",
"role": "Developer"
}
],
"autoload": {
"psr-4": { "JustBetter\\XhprofProfiler\\": "" },
"files": [ "registration.php" ]
},
"require-dev": {
"bitexpert/phpstan-magento": "^0.30.1",
"phpstan/phpstan": "^1.10"
},
"config": {
"allow-plugins": {
"magento/composer-dependency-version-audit-plugin": true
}
}
}
9 changes: 9 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer name="JustBetter_XhprofProfiler::start_xhprof" instance="JustBetter\XhprofProfiler\Observer\StartXhprof" />
</event>
<event name="controller_action_postdispatch">
<observer name="JustBetter_XhprofProfiler::stop_xhprof" instance="JustBetter\XhprofProfiler\Observer\StopXhprof" />
</event>
</config>
7 changes: 7 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="JustBetter_XhprofProfiler" setup_version="1.0.0">
<sequence>
</sequence>
</module>
</config>
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
includes:
- vendor/bitexpert/phpstan-magento/extension.neon
parameters:
paths:
- .
excludePaths:
- vendor
- Test/*
level: 9
checkMissingIterableValueType: false
9 changes: 9 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'JustBetter_XhprofProfiler',
__DIR__
);

0 comments on commit ce6a384

Please sign in to comment.