-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robin Mulder
committed
Jun 7, 2024
1 parent
fc5ca38
commit ce6a384
Showing
10 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
.idea | ||
composer.lock | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ | ||
); |