From 2f58d425f79fe7c0ca71edaa6b8cfba9efdc6908 Mon Sep 17 00:00:00 2001 From: Thierry Geindre Date: Wed, 20 Jun 2018 16:04:29 +0200 Subject: [PATCH 1/9] Add PHP dynamic extension load --- README.md | 17 ++++++++++++++++- src/Console/Deployer.php | 30 +++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a87dc674f..4481bf52f 100644 --- a/README.md +++ b/README.md @@ -234,7 +234,7 @@ hooks: - 'npm install' ``` -## PHP binary +## PHP configuration If you need a specific PHP version, you can define it in a `.bref.yml` file: @@ -248,6 +248,21 @@ Here is the list of versions available: - 7.2.5 - 7.2.2 +You can also define PHP configuration and activate extensions: + +```yaml +php: + configuration: + max_execution_time: 300 + extensions: + - redis +``` + +Here is the list of extensions available: + +- Redis - `redis` +- MongoDB - `mongodb` + ## Contributing There are a lot of detailed `TODO` notes in the codebase. Feel free to work on these. diff --git a/src/Console/Deployer.php b/src/Console/Deployer.php index 4bc263d9c..1f60ddf1b 100644 --- a/src/Console/Deployer.php +++ b/src/Console/Deployer.php @@ -155,7 +155,14 @@ private function generateArchive(SymfonyStyle $io, ProgressBar $progress) : void // Set correct permissions on the file $this->fs->chmod('.bref/output/.bref/bin', 0755); // Install our custom php.ini - $this->fs->copy(__DIR__ . '/../../template/php.ini', '.bref/output/.bref/php.ini'); + $this->fs->copy(__DIR__ . '/../../template/php.ini', $phpConfigFile = '.bref/output/.bref/php.ini'); + // Inject additional config in php.ini + $this->injectPhpConfig( + $phpConfigFile, + $projectConfig['php']['configuration'] ?? [], + $projectConfig['php']['extensions'] ?? [] + ); + //file_put_contents($phpConfigFile, implode()) $progress->advance(); $progress->setMessage('Installing Bref files for NodeJS'); @@ -241,4 +248,25 @@ private function copyProjectToOutputDirectory() : void $directoryMirror = new DirectoryMirror($this->fs); $directoryMirror->mirror($source, $target); } + + private function injectPhpConfig(string $targetFile, array $configs, array $extensions = []) + { + array_walk($configs, function(&$value, $key) { + $value = $key . '=' .$value; + }); + + $extensions = array_map(function ($extension) { + if (!$this->fs->exists('.bref/output/.bref/bin/ext/' . $extension . '.so')) { + throw new \Exception('PHP Extension "'. $extension . '" does not exist'); + } + + return 'extension=' . $extension . '.so'; + }, $extensions); + + file_put_contents( + $targetFile, + implode("\n", array_merge($configs, $extensions)), + FILE_APPEND + ); + } } From 38847f195757e71856d8b32687da0d49c109879f Mon Sep 17 00:00:00 2001 From: Thierry Geindre Date: Wed, 20 Jun 2018 16:04:43 +0200 Subject: [PATCH 2/9] Add redis & mongodb extensions build --- bin/php/Dockerfile | 6 +++++- bin/php/build.sh | 3 +-- src/Console/Deployer.php | 1 - 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/bin/php/Dockerfile b/bin/php/Dockerfile index 5a23bb5e1..1411d9c3a 100644 --- a/bin/php/Dockerfile +++ b/bin/php/Dockerfile @@ -17,7 +17,9 @@ RUN yum --releasever=2017.03 install \ openssl-devel \ libpng-devel \ libjpeg-devel \ - curl-devel -y + curl-devel \ + findutils \ + php-pear -y RUN curl -sL https://github.com/php/php-src/archive/$PHP_VERSION.tar.gz | tar -zxv @@ -54,3 +56,5 @@ RUN ./configure \ RUN make -j 5 RUN make install + +RUN pecl install mongodb redis diff --git a/bin/php/build.sh b/bin/php/build.sh index 2450ffce4..6e6484f15 100755 --- a/bin/php/build.sh +++ b/bin/php/build.sh @@ -22,8 +22,7 @@ container=$(docker create php-build) docker -D cp $container:/php-src-$PHP_VERSION_GIT_BRANCH/sapi/cli/php . mkdir -p ext -docker -D cp $container:/usr/local/lib/php/extensions/no-debug-non-zts-20170718/opcache.a ext/opcache.a -docker -D cp $container:/usr/local/lib/php/extensions/no-debug-non-zts-20170718/opcache.so ext/opcache.so +docker -D cp $container:/usr/local/lib/php/extensions/no-debug-non-zts-20170718/. ext/ docker rm $container tar czf $PHP_VERSION_GIT_BRANCH.tar.gz php ext diff --git a/src/Console/Deployer.php b/src/Console/Deployer.php index 1f60ddf1b..f796a4ec8 100644 --- a/src/Console/Deployer.php +++ b/src/Console/Deployer.php @@ -162,7 +162,6 @@ private function generateArchive(SymfonyStyle $io, ProgressBar $progress) : void $projectConfig['php']['configuration'] ?? [], $projectConfig['php']['extensions'] ?? [] ); - //file_put_contents($phpConfigFile, implode()) $progress->advance(); $progress->setMessage('Installing Bref files for NodeJS'); From bdc24569190fa14e9f416f79a52ce2997581b3e1 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Wed, 20 Jun 2018 16:44:35 +0200 Subject: [PATCH 3/9] Improve exception message --- src/Console/Deployer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Deployer.php b/src/Console/Deployer.php index f796a4ec8..11b7aa31d 100644 --- a/src/Console/Deployer.php +++ b/src/Console/Deployer.php @@ -256,7 +256,7 @@ private function injectPhpConfig(string $targetFile, array $configs, array $exte $extensions = array_map(function ($extension) { if (!$this->fs->exists('.bref/output/.bref/bin/ext/' . $extension . '.so')) { - throw new \Exception('PHP Extension "'. $extension . '" does not exist'); + throw new \Exception("The PHP extension '$extension' is not available yet in Bref, please open an issue or a pull request on GitHub to add that extension'); } return 'extension=' . $extension . '.so'; From 40681e491971b7e9596959d2d52ff24ee72b86dd Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Wed, 20 Jun 2018 16:45:04 +0200 Subject: [PATCH 4/9] Fix my previous commit ^^ --- src/Console/Deployer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Deployer.php b/src/Console/Deployer.php index 11b7aa31d..5422fbe94 100644 --- a/src/Console/Deployer.php +++ b/src/Console/Deployer.php @@ -256,7 +256,7 @@ private function injectPhpConfig(string $targetFile, array $configs, array $exte $extensions = array_map(function ($extension) { if (!$this->fs->exists('.bref/output/.bref/bin/ext/' . $extension . '.so')) { - throw new \Exception("The PHP extension '$extension' is not available yet in Bref, please open an issue or a pull request on GitHub to add that extension'); + throw new \Exception("The PHP extension '$extension' is not available yet in Bref, please open an issue or a pull request on GitHub to add that extension"); } return 'extension=' . $extension . '.so'; From 1542209ff24c640db504ab95c1c149b094ded29d Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Wed, 20 Jun 2018 16:46:51 +0200 Subject: [PATCH 5/9] Reword documentation --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4481bf52f..ff3ffdf36 100644 --- a/README.md +++ b/README.md @@ -248,7 +248,7 @@ Here is the list of versions available: - 7.2.5 - 7.2.2 -You can also define PHP configuration and activate extensions: +You can also define `php.ini` configuration flags and activate extensions: ```yaml php: @@ -260,8 +260,8 @@ php: Here is the list of extensions available: -- Redis - `redis` -- MongoDB - `mongodb` +- Redis: `redis` +- MongoDB: `mongodb` ## Contributing From 709d39bf3e74ffb33f4e8c531d142f11aefa852b Mon Sep 17 00:00:00 2001 From: Thierry Geindre Date: Wed, 20 Jun 2018 17:12:00 +0200 Subject: [PATCH 6/9] Add Dockerfile comment --- bin/php/Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/php/Dockerfile b/bin/php/Dockerfile index 1411d9c3a..6add448d0 100644 --- a/bin/php/Dockerfile +++ b/bin/php/Dockerfile @@ -18,6 +18,7 @@ RUN yum --releasever=2017.03 install \ libpng-devel \ libjpeg-devel \ curl-devel \ + # The pecl command uses find findutils \ php-pear -y From e35b783fac6dce441ffed85ebcd19ee79d369f15 Mon Sep 17 00:00:00 2001 From: Thierry Geindre Date: Wed, 20 Jun 2018 17:46:23 +0200 Subject: [PATCH 7/9] Use matomo/ini to generate php.ini --- composer.json | 3 ++- composer.lock | 37 +++++++++++++++++++++++++++++++++++-- src/Console/Deployer.php | 38 +++++++++++++++++++++----------------- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/composer.json b/composer.json index e692231e7..4c39c0741 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ "aws/aws-sdk-php": "^3.44", "psr/http-server-handler": "^1.0", "zendframework/zend-diactoros": "^1.6", - "jolicode/jolinotif": "^2.0" + "jolicode/jolinotif": "^2.0", + "matomo/ini": "^2.0" }, "require-dev": { "phpunit/phpunit": "^6.5", diff --git a/composer.lock b/composer.lock index 025d3281d..4c804b1c0 100644 --- a/composer.lock +++ b/composer.lock @@ -1,10 +1,10 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2fbbe4d1492deb55142041bea5201f99", + "content-hash": "1d65b1c4163e18ec2c91133d9e5fd586", "packages": [ { "name": "aws/aws-sdk-php", @@ -780,6 +780,39 @@ ], "time": "2018-04-04T15:31:01+00:00" }, + { + "name": "matomo/ini", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/matomo-org/component-ini.git", + "reference": "ac25c899aefdbaca7e00a86367af24bd72aa1b4d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/matomo-org/component-ini/zipball/ac25c899aefdbaca7e00a86367af24bd72aa1b4d", + "reference": "ac25c899aefdbaca7e00a86367af24bd72aa1b4d", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "athletic/athletic": "0.1.*", + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Matomo\\Ini\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0" + ], + "time": "2018-02-14T18:57:44+00:00" + }, { "name": "mnapoli/silly", "version": "1.7.0", diff --git a/src/Console/Deployer.php b/src/Console/Deployer.php index 5422fbe94..c0032e793 100644 --- a/src/Console/Deployer.php +++ b/src/Console/Deployer.php @@ -6,6 +6,8 @@ use Bref\Filesystem\DirectoryMirror; use Joli\JoliNotif\Notification; use Joli\JoliNotif\NotifierFactory; +use Matomo\Ini\IniReader; +use Matomo\Ini\IniWriter; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Filesystem\Filesystem; @@ -248,24 +250,26 @@ private function copyProjectToOutputDirectory() : void $directoryMirror->mirror($source, $target); } - private function injectPhpConfig(string $targetFile, array $configs, array $extensions = []) + private function injectPhpConfig(string $targetFile, array $flags, array $extensions = []) { - array_walk($configs, function(&$value, $key) { - $value = $key . '=' .$value; - }); - - $extensions = array_map(function ($extension) { - if (!$this->fs->exists('.bref/output/.bref/bin/ext/' . $extension . '.so')) { - throw new \Exception("The PHP extension '$extension' is not available yet in Bref, please open an issue or a pull request on GitHub to add that extension"); - } - - return 'extension=' . $extension . '.so'; - }, $extensions); - - file_put_contents( - $targetFile, - implode("\n", array_merge($configs, $extensions)), - FILE_APPEND + $config = array_merge( + ['flags' => array_merge( + (new IniReader())->readFile($targetFile), + $flags + )], + array_combine( + $extensions, + array_map(function ($extension) { + if (!$this->fs->exists('.bref/output/.bref/bin/ext/' . $extension . '.so')) { + throw new \Exception("The PHP extension '$extension' is not available yet in Bref, please open an issue or a pull request on GitHub to add that extension"); + } + + return ['extension' => $extension . '.so']; + }, + $extensions + )) ); + + (new IniWriter())->writeToFile($targetFile, $config); } } From f4880c7102fcdf03429723a81d56c18e22b493f9 Mon Sep 17 00:00:00 2001 From: Thierry Geindre Date: Wed, 20 Jun 2018 23:39:40 +0200 Subject: [PATCH 8/9] Avoid useless php.ini file copy --- src/Console/Deployer.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Console/Deployer.php b/src/Console/Deployer.php index c0032e793..8da6e42a5 100644 --- a/src/Console/Deployer.php +++ b/src/Console/Deployer.php @@ -156,11 +156,10 @@ private function generateArchive(SymfonyStyle $io, ProgressBar $progress) : void ->mustRun(); // Set correct permissions on the file $this->fs->chmod('.bref/output/.bref/bin', 0755); - // Install our custom php.ini - $this->fs->copy(__DIR__ . '/../../template/php.ini', $phpConfigFile = '.bref/output/.bref/php.ini'); - // Inject additional config in php.ini - $this->injectPhpConfig( - $phpConfigFile, + // Install our custom php.ini and merge it with user configuration + $this->buildPhpConfig( + __DIR__ . '/../../template/php.ini', + '.bref/output/.bref/php.ini', $projectConfig['php']['configuration'] ?? [], $projectConfig['php']['extensions'] ?? [] ); @@ -250,11 +249,11 @@ private function copyProjectToOutputDirectory() : void $directoryMirror->mirror($source, $target); } - private function injectPhpConfig(string $targetFile, array $flags, array $extensions = []) + private function buildPhpConfig(string $sourceFile, string $targetFile, array $flags, array $extensions = []) { $config = array_merge( ['flags' => array_merge( - (new IniReader())->readFile($targetFile), + (new IniReader())->readFile($sourceFile), $flags )], array_combine( From da4f11799dfe959118f45c0f8874298b02efe007 Mon Sep 17 00:00:00 2001 From: Thierry Geindre Date: Wed, 20 Jun 2018 23:44:53 +0200 Subject: [PATCH 9/9] CS fixes --- src/Console/Deployer.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Console/Deployer.php b/src/Console/Deployer.php index 8da6e42a5..793147151 100644 --- a/src/Console/Deployer.php +++ b/src/Console/Deployer.php @@ -264,9 +264,8 @@ private function buildPhpConfig(string $sourceFile, string $targetFile, array $f } return ['extension' => $extension . '.so']; - }, - $extensions - )) + }, $extensions) + ) ); (new IniWriter())->writeToFile($targetFile, $config);