diff --git a/cli/Valet/Binaries.php b/cli/Valet/Binaries.php index 26a68747..260d1eba 100644 --- a/cli/Valet/Binaries.php +++ b/cli/Valet/Binaries.php @@ -11,7 +11,6 @@ */ class Binaries { - const N98_MAGERUN = 'magerun'; const N98_MAGERUN_2 = 'magerun2'; const DRUSH_LAUNCHER = 'drush'; diff --git a/cli/Valet/Blackfire.php b/cli/Valet/Blackfire.php new file mode 100644 index 00000000..7a4b390d --- /dev/null +++ b/cli/Valet/Blackfire.php @@ -0,0 +1,190 @@ + 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; + } +} diff --git a/cli/Valet/Brew.php b/cli/Valet/Brew.php index a65624e7..09c0d8ce 100644 --- a/cli/Valet/Brew.php +++ b/cli/Valet/Brew.php @@ -6,7 +6,6 @@ class Brew { - public $cli; public $files; diff --git a/cli/Valet/Logs.php b/cli/Valet/Logs.php index 2b7fe792..a671f2f2 100644 --- a/cli/Valet/Logs.php +++ b/cli/Valet/Logs.php @@ -10,7 +10,6 @@ public function __construct(CommandLine $cli) public function open($file) { - $this->cli->quietly('open ' . $this->resolvePath($file)); } diff --git a/cli/Valet/Pecl.php b/cli/Valet/Pecl.php index 8491dfd3..869c485f 100644 --- a/cli/Valet/Pecl.php +++ b/cli/Valet/Pecl.php @@ -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'; @@ -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 ] ]; @@ -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; diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index 6bd53e0f..ae1f1a12 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -319,7 +319,6 @@ public function disableExtension($extension) */ public function isExtensionEnabled($extension) { - $currentPhpVersion = $this->brew->linkedPhp(); if (!$this->brew->installed($currentPhpVersion . '-' . $extension)) { diff --git a/cli/drivers/PimcoreValetDriver.php b/cli/drivers/PimcoreValetDriver.php index 0498633f..6d851cab 100644 --- a/cli/drivers/PimcoreValetDriver.php +++ b/cli/drivers/PimcoreValetDriver.php @@ -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; diff --git a/cli/drivers/Typo3ValetDriver.php b/cli/drivers/Typo3ValetDriver.php index 07a948a7..2d2dccae 100644 --- a/cli/drivers/Typo3ValetDriver.php +++ b/cli/drivers/Typo3ValetDriver.php @@ -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); } diff --git a/cli/drivers/WordPressMultisiteSubdirectoryValetDriver.php b/cli/drivers/WordPressMultisiteSubdirectoryValetDriver.php index 0039f35d..4aea3e93 100644 --- a/cli/drivers/WordPressMultisiteSubdirectoryValetDriver.php +++ b/cli/drivers/WordPressMultisiteSubdirectoryValetDriver.php @@ -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-')); } diff --git a/cli/includes/facades.php b/cli/includes/facades.php index 0762838f..c09df4ad 100644 --- a/cli/includes/facades.php +++ b/cli/includes/facades.php @@ -95,3 +95,6 @@ class Logs extends Facade class Valet extends Facade { } +class Blackfire extends Facade +{ +} diff --git a/cli/stubs/magento2/env.php b/cli/stubs/magento2/env.php index 4948fcd9..0d61b157 100644 --- a/cli/stubs/magento2/env.php +++ b/cli/stubs/magento2/env.php @@ -1,15 +1,15 @@ - array ( + array( 'frontName' => 'admin', ), 'db' => - array ( + array( 'connection' => - array ( + array( 'indexer' => - array ( + array( 'host' => '127.0.0.1', 'dbname' => 'DBNAME', 'username' => 'root', @@ -21,7 +21,7 @@ 'initStatements' => 'SET NAMES utf8;', ), 'default' => - array ( + array( 'host' => '127.0.0.1', 'dbname' => 'DBNAME', 'username' => 'root', @@ -35,14 +35,14 @@ 'table_prefix' => '', ), 'crypt' => - array ( + array( 'key' => 'JkeEumwvvQBCDxypLPBozvrpF2rFNhNL', ), 'session' => - array ( + array( 'save' => 'redis', 'redis' => - array ( + array( 'host' => '/tmp/redis.sock', 'port' => '6379', 'password' => '', @@ -64,24 +64,24 @@ ), ), 'cache' => - array ( + array( 'frontend' => - array ( + array( 'default' => - array ( + array( 'backend' => 'Cm_Cache_Backend_Redis', 'backend_options' => - array ( + array( 'server' => '/tmp/redis.sock', 'port' => '6379', 'database' => '2', ), ), 'page_cache' => - array ( + array( 'backend' => 'Cm_Cache_Backend_Redis', 'backend_options' => - array ( + array( 'server' => '/tmp/redis.sock', 'port' => '6379', 'database' => '1', @@ -91,16 +91,16 @@ ), ), 'resource' => - array ( + array( 'default_setup' => - array ( + array( 'connection' => 'default', ), ), 'x-frame-options' => 'SAMEORIGIN', 'MAGE_MODE' => 'developer', 'cache_types' => - array ( + array( 'config' => 1, 'layout' => 1, 'block_html' => 1, @@ -118,13 +118,13 @@ 'customer_notification' => 1, ), 'install' => - array ( + array( 'date' => 'Wed, 19 Jul 2017 00:00:00 +0000', ), 'queue' => - array ( + array( 'amqp' => - array ( + array( 'host' => '', 'port' => '', 'user' => '', diff --git a/cli/stubs/z-performance.ini b/cli/stubs/z-performance.ini index 4a2cbaa8..1ca9463f 100644 --- a/cli/stubs/z-performance.ini +++ b/cli/stubs/z-performance.ini @@ -67,3 +67,6 @@ xdebug.remote_host=localhost xdebug.remote_port=9000 xdebug.remote_autostart=1 xdebug.idekey=PHPSTORM + +[blackfire] +blackfire.agent_timeout = 0.25 diff --git a/cli/valet.php b/cli/valet.php index ed282f29..3eebe2de 100644 --- a/cli/valet.php +++ b/cli/valet.php @@ -729,6 +729,45 @@ throw new Exception('Sub-command not found. Available: install'); })->descriptions('Enable / disable Elasticsearch'); + $app->command('blackfire [mode]', function ($mode) { + $modes = ['install', 'on', 'enable', 'off', 'disable']; + + if (!in_array($mode, $modes)) { + throw new Exception('Mode not found. Available modes: '.implode(', ', $modes)); + } + + switch ($mode) { + case 'install': + Blackfire::install(); + return; + case 'enable': + case 'on': + Blackfire::enable(); + if (Pecl::isEnabled('xdebug') === true) { + info("[PECL] Disabling xdebug extension."); + info("If XDebug is installed, the PHP engine may not behave as expected"); + $restart = true; + Pecl::disable('xdebug'); + } + return; + case 'test': + passthru('blackfire-agent --test'); + // no break + case 'register': + passthru('blackfire-agent --register'); + // no break + case 'config': + passthru('blackfire config'); + // no break + case 'disable': + case 'off': + Blackfire::disable(); + return; + } + + throw new Exception('Sub-command not found. Available: install'); + })->descriptions('Enable / disable Blackfire'); + $app->command('rabbitmq [mode]', function ($mode) { $modes = ['install', 'on', 'enable', 'off', 'disable']; diff --git a/server.php b/server.php index 75497a34..1b4504cb 100644 --- a/server.php +++ b/server.php @@ -11,7 +11,8 @@ * Load the Valet configuration. */ $valetConfig = json_decode( - file_get_contents(VALET_HOME_PATH.'/config.json'), true + file_get_contents(VALET_HOME_PATH.'/config.json'), + true ); /** @@ -51,7 +52,7 @@ $valetSitePath = apcu_fetch('valet_site_path'.$siteName); $domain = array_slice(explode('.', $siteName), -1)[0]; -if(!$valetSitePath) { +if (!$valetSitePath) { foreach ($valetConfig['paths'] as $path) { if (is_dir($path.'/'.$siteName)) { $valetSitePath = $path.'/'.$siteName; @@ -115,7 +116,9 @@ * Attempt to dispatch to a front controller. */ $frontControllerPath = $valetDriver->frontControllerPath( - $valetSitePath, $siteName, $uri + $valetSitePath, + $siteName, + $uri ); if (! $frontControllerPath) { diff --git a/tests/NginxTest.php b/tests/NginxTest.php index 41877c16..2b912fd4 100644 --- a/tests/NginxTest.php +++ b/tests/NginxTest.php @@ -86,5 +86,4 @@ public function test_install_nginx_directories_rewrites_secure_nginx_files() $site->shouldHaveReceived('resecureForNewDomain', ['test', 'test']); } - } diff --git a/tests/PhpFpmTest.php b/tests/PhpFpmTest.php index 3b7ec903..b3fed58c 100644 --- a/tests/PhpFpmTest.php +++ b/tests/PhpFpmTest.php @@ -38,7 +38,7 @@ public function test_fpm_is_configured_with_the_correct_user_group_and_port() class StubForUpdatingFpmConfigFiles extends PhpFpm { - function fpmConfigPath() + public function fpmConfigPath() { return __DIR__.'/output/fpm.conf'; } diff --git a/tests/SiteTest.php b/tests/SiteTest.php index f0e7316a..dc652077 100644 --- a/tests/SiteTest.php +++ b/tests/SiteTest.php @@ -69,7 +69,7 @@ public function test_prune_links_removes_broken_symlinks_in_sites_path() class StubForRemovingLinks extends Site { - function sitesPath() + public function sitesPath() { return __DIR__.'/output'; }