Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Added Blackfire Support #504

Open
wants to merge 6 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cli/Valet/Binaries.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
class Binaries
{

const N98_MAGERUN = 'magerun';
const N98_MAGERUN_2 = 'magerun2';
const DRUSH_LAUNCHER = 'drush';
Expand Down
190 changes: 190 additions & 0 deletions cli/Valet/Blackfire.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

namespace Valet;

use DomainException;

class Blackfire extends AbstractService
{
const BF_FORMULA_NAME = 'blackfire-agent';
const BF_TAP = 'blackfireio/homebrew-blackfire';
const BF_V1300_VERSION = '1.34.3';
const BF_DEFAULT_VERSION = self::BF_V1300_VERSION;

const SUPPORTED_BF_VERSIONS = [
self::BF_V1300_VERSION => self::BF_V1300_VERSION
];

public $brew;
public $cli;
/**
* @var Pecl
*/
private $pecl;

/**
* Create a new instance.
*
* @param Configuration $configuration
* @param Brew $brew
* @param CommandLine $cli
* @param Pecl $pecl
*/
public function __construct(
Configuration $configuration,
Brew $brew,
CommandLine $cli,
Pecl $pecl
) {
$this->cli = $cli;
$this->brew = $brew;
$this->pecl = $pecl;
parent::__construct($configuration);
}

/**
* Install the service.
*
* @param string $version
* @return void
*/
public function install($version = self::BF_DEFAULT_VERSION)
{
if (!array_key_exists($version, self::SUPPORTED_BF_VERSIONS)) {
warning('The Blackfire version you\'re installing is not supported.');

return;
}


if ($this->installed($version)) {
info('[' . self::SUPPORTED_BF_VERSIONS[$version] . '] already installed');

return;
}

$extensionFile = $this->pecl->getExtensionDirectory() . "/blackfire.so";
if (!file_exists($extensionFile)) {
$phpVersion = str_replace('.', '', $this->getPhpVersion());
$url = "https://packages.blackfire.io/binaries/blackfire-php/{$version}/blackfire-php-darwin_amd64-php-{$phpVersion}.so";
$this->cli->quietlyAsUser("wget {$url} -O $extensionFile");
$this->pecl->enableExtension('blackfire');
$this->cli->runAsUser('valet restart php');
}

$this->brew->installOrFail(self::BF_FORMULA_NAME, [], [self::BF_TAP]);
if ($this->brew->isStartedService(self::BF_FORMULA_NAME)) {
$this->cli->quietlyAsUser('brew services restart ' . self::BF_FORMULA_NAME);
} else {
$this->cli->quietlyAsUser('brew services start ' . self::BF_FORMULA_NAME);
}
$this->cli->passthru('blackfire-agent --register');
$this->cli->passthru('blackfire config');
$this->cli->quietlyAsUser('brew services restart ' . self::BF_FORMULA_NAME);

$this->restart($version);
}

/**
* Returns wether Elasticsearch is installed.
*
* @param string $version
* @return bool
*/
public function installed($version = null)
{
$extensionFile = $this->pecl->getExtensionDirectory() . "/blackfire.so";
if (!file_exists($extensionFile)) {
return false;
}
if ($this->brew->installed(self::BF_FORMULA_NAME)) {
return true;
}

return false;
}

/**
* Restart the service.
*
* @param string $version
* @return void
*/
public function restart($version = null)
{
$version = ($version ? $version : $this->getCurrentVersion());
$version = $this->installed($version);
if (!$version) {
return;
}

$this->pecl->enableExtension('blackfire');
$this->cli->runAsUser('valet restart php');
info('[' . self::BF_FORMULA_NAME . '] Restarting');
$this->cli->quietlyAsUser('brew services restart ' . self::BF_FORMULA_NAME);
}

/**
* Stop the service.
*
* @param string $version
* @return void
*/
public function stop($version = null)
{
$version = ($version ? $version : $this->getCurrentVersion());
$version = $this->installed($version);
if (!$version) {
return;
}

$this->pecl->disableExtension('blackfire');
$this->cli->runAsUser('valet restart php');

info('[' . self::BF_FORMULA_NAME . '] Stopping');
$this->cli->quietly('sudo brew services stop ' . self::BF_FORMULA_NAME);
$this->cli->quietlyAsUser('brew services stop ' . self::BF_FORMULA_NAME);
}

/**
* Prepare for uninstallation.
*
* @return void
*/
public function uninstall()
{
$this->stop();
}

/**
* Returns the current running version.
*
* @return bool|int|string
*/
public function getCurrentVersion()
{
$currentVersion = false;
foreach (self::SUPPORTED_BF_VERSIONS as $version => $formula) {
if ($this->brew->isStartedService($formula)) {
$currentVersion = $version;
}
}

return $currentVersion;
}

/**
* Get the current PHP version from the PECL config.
*
* @return string
* The php version as string: 5.6, 7.0, 7.1, 7.2, 7.3
*/
protected function getPhpVersion()
{
$version = $this->cli->runAsUser('pecl version | grep PHP');
$version = str_replace('PHP Version:', '', $version);
$version = str_replace(' ', '', $version);
$version = substr($version, 0, 3);
return $version;
}
}
1 change: 0 additions & 1 deletion cli/Valet/Brew.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class Brew
{

public $cli;
public $files;

Expand Down
1 change: 0 additions & 1 deletion cli/Valet/Logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public function __construct(CommandLine $cli)

public function open($file)
{

$this->cli->quietly('open ' . $this->resolvePath($file));
}

Expand Down
7 changes: 6 additions & 1 deletion cli/Valet/Pecl.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Pecl extends AbstractPecl
const GEOIP_EXTENSION = 'geoip';
const MEMCACHE_EXTENSION = 'memcached';
const YAML_EXTENSION = 'yaml';
const BLACKFIRE_EXTENSION = 'blackfire';

// Extension aliases.
const APCU_BC_ALIAS = 'apc';
Expand Down Expand Up @@ -75,6 +76,10 @@ class Pecl extends AbstractPecl
self::YAML_EXTENSION => [
'5.6' => '1.3.1',
'extension_type' => self::NORMAL_EXTENSION_TYPE
],
self::BLACKFIRE_EXTENSION => [
'default' => false,
'extension_type' => self::NORMAL_EXTENSION_TYPE
]
];

Expand Down Expand Up @@ -338,7 +343,7 @@ public function fix()
foreach ($pearConfigSplit as $splitValue) {
if (strpos($splitValue, 'php_ini')) {
$pearConfig = unserialize($splitValue);
} else if (strpos($splitValue, 'Config')) {
} elseif (strpos($splitValue, 'Config')) {
$pearConfigVersion = $splitValue;
} else {
continue;
Expand Down
1 change: 0 additions & 1 deletion cli/Valet/PhpFpm.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ public function disableExtension($extension)
*/
public function isExtensionEnabled($extension)
{

$currentPhpVersion = $this->brew->linkedPhp();

if (!$this->brew->installed($currentPhpVersion . '-' . $extension)) {
Expand Down
2 changes: 1 addition & 1 deletion cli/drivers/PimcoreValetDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PimcoreValetDriver extends ValetDriver
public function serves($sitePath, $siteName, $uri)
{
if (file_exists($sitePath.'/pimcore')) {
return true;
return true;
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion cli/drivers/Typo3ValetDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function frontControllerPath($sitePath, $siteName, $uri)
// this folder can be served by index.html
return $absoluteFilePath . '/index.html';
}
} else if (pathinfo($absoluteFilePath, PATHINFO_EXTENSION) === 'php') {
} elseif (pathinfo($absoluteFilePath, PATHINFO_EXTENSION) === 'php') {
// this file can be served directly
return $this->serveScript($sitePath, $siteName, $uri);
}
Expand Down
2 changes: 1 addition & 1 deletion cli/drivers/WordPressMultisiteSubdirectoryValetDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function frontControllerPath($sitePath, $siteName, $uri)

// If URI contains one of the main WordPress directories, and it's not a request for the Network Admin,
// drop the subdirectory segment before routing the request
if (( stripos($uri, 'wp-admin') !== false || stripos($uri, 'wp-content') !== false || stripos($uri, 'wp-includes') !== false )) {
if ((stripos($uri, 'wp-admin') !== false || stripos($uri, 'wp-content') !== false || stripos($uri, 'wp-includes') !== false)) {
if (stripos($uri, 'wp-admin/network') === false) {
$uri = substr($uri, stripos($uri, '/wp-'));
}
Expand Down
3 changes: 3 additions & 0 deletions cli/includes/facades.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,6 @@ class Logs extends Facade
class Valet extends Facade
{
}
class Blackfire extends Facade
{
}
Loading