generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support call Profiler from Container DI (#23)
* [add] Added the ability to use Profiler in your classes * [add] Added example usage in readme file --------- Co-authored-by: kEERill <[email protected]>
- Loading branch information
Showing
7 changed files
with
191 additions
and
82 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
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
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,42 @@ | ||
<?php | ||
|
||
namespace Maantje\XhprofBuggregatorLaravel\Factories; | ||
|
||
use Illuminate\Foundation\Application; | ||
use Maantje\XhprofBuggregatorLaravel\Middleware\XhprofProfiler; | ||
use SpiralPackages\Profiler\Driver\NullDriver; | ||
use SpiralPackages\Profiler\Profiler; | ||
use SpiralPackages\Profiler\Storage\NullStorage; | ||
|
||
final class OptionalProfilerFactory | ||
{ | ||
protected ProfilerFactory $profilerFactory; | ||
|
||
public function __construct( | ||
protected Application $app | ||
) { | ||
$this->profilerFactory = new ProfilerFactory($this->app['config']); | ||
} | ||
|
||
public function create(): Profiler | ||
{ | ||
if ($this->isEnabled()) { | ||
return $this->profilerFactory->create(); | ||
} | ||
|
||
return new Profiler( | ||
storage: new NullStorage, | ||
driver: new NullDriver, | ||
appName: $this->app['config']->get('app.name'), | ||
); | ||
} | ||
|
||
private function isEnabled(): bool | ||
{ | ||
if ($this->app['request']->hasHeader(XhprofProfiler::HEADER)) { | ||
return filter_var($this->app['request']->header(XhprofProfiler::HEADER), FILTER_VALIDATE_BOOLEAN); | ||
} | ||
|
||
return $this->app['config']->get('xhprof.enabled'); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace Maantje\XhprofBuggregatorLaravel\Factories; | ||
|
||
use Illuminate\Config\Repository; | ||
use SpiralPackages\Profiler\DriverFactory; | ||
use SpiralPackages\Profiler\Profiler; | ||
use SpiralPackages\Profiler\Storage\StorageInterface; | ||
use SpiralPackages\Profiler\Storage\WebStorage; | ||
use Symfony\Component\HttpClient\CurlHttpClient; | ||
|
||
final class ProfilerFactory | ||
{ | ||
public function __construct( | ||
protected Repository $configRepository | ||
) {} | ||
|
||
public function create(): Profiler | ||
{ | ||
return new Profiler( | ||
$this->makeStorage(), | ||
DriverFactory::createXhrofDriver(), | ||
$this->configRepository->get('app.name') | ||
); | ||
} | ||
|
||
private function makeStorage(): StorageInterface | ||
{ | ||
return new WebStorage( | ||
new CurlHttpClient, | ||
$this->configRepository->get('xhprof.endpoint'), | ||
); | ||
} | ||
} |
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
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
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