From e7ca8e4be221d5ee4953cbf7e8162e9f87746ab4 Mon Sep 17 00:00:00 2001 From: Helmut Hummel Date: Thu, 29 Oct 2015 23:09:39 +0100 Subject: [PATCH] WIP --- LICENSE | 19 +++++++++ composer.json | 29 +++++++++++++- src/Plugin.php | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 LICENSE create mode 100644 src/Plugin.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b9d1893 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2015 Helmut Hummel + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/composer.json b/composer.json index 39a5705..f6daac0 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,32 @@ { + "name": "helhum/dotenv-connector", + "type": "composer-plugin", + "license": "MIT", + "description": "Makes it possible to set environment variables for composer projects using dotenv", + "keywords": [ + "composer", "env", "12factor", "dotenv" + ], + "homepage": "http://github.com/helhum/dotenv-connector", + "authors": [ + { + "name": "Helmut Hummel", + "email": "info@helhum.io" + } + ], + "autoload": { + "psr-4": { + "Helhum\\DotEnvConnector\\": "src/" + } + }, "require": { - "josegonzalez/dotenv": "^1.0" + "php": ">=5.3.7", + "composer-plugin-api": "^1.0", + "vlucas/phpdotenv": "^2.0.1" + }, + "extra": { + "class": "Helhum\\DotEnvConnector\\Plugin", + "branch-alias": { + "dev-master": "1.0.x-dev" + } } } diff --git a/src/Plugin.php b/src/Plugin.php new file mode 100644 index 0000000..be2a9af --- /dev/null +++ b/src/Plugin.php @@ -0,0 +1,105 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Composer\Composer; +use Composer\Config; +use Composer\EventDispatcher\EventSubscriberInterface; +use Composer\IO\IOInterface; +use Composer\Plugin\PluginInterface; +use Composer\Script\ScriptEvents; +use TYPO3\CMS\Composer\Plugin\Util\Filesystem; + +/** + * Class Plugin + */ +class Plugin implements PluginInterface, EventSubscriberInterface +{ + /** + * @var Composer + */ + protected $composer; + + /** + * @var IOInterface + */ + protected $io; + + /** + * @var string + */ + protected $baseDir; + + /** + * Apply plugin modifications to composer + * + * @param Composer $composer + * @param IOInterface $io + */ + public function activate(Composer $composer, IOInterface $io) + { + $this->composer = $composer; + $this->io = $io; + $this->baseDir = substr($this->composer->getConfig()->get('vendor-dir'), 0, -strlen($this->composer->getConfig()->get('vendor-dir', Config::RELATIVE_PATHS))); + } + + /** + * {@inheritDoc} + */ + public static function getSubscribedEvents() { + return array( + ScriptEvents::POST_AUTOLOAD_DUMP => array('onPostAutoloadDump') + ); + } + + /** + * Plugin callback for this script event, which calls the previously implemented static method + * + * @param \Composer\Script\Event $event + * @return bool + */ + public function onPostAutoloadDump(\Composer\Script\Event $event) + { + $filesystem = new Filesystem(); + if (!file_exists($this->baseDir . '.env')) { + return; + } + $pathToRootCode = $filesystem->findShortestPathCode($this->composer->getConfig()->get('vendor-dir') . '/foo', $this->baseDir); + var_dump($pathToRootCode); + $code = "(new \\Dotenv\\Dotenv($pathToRootCode))->load();\n"; + $this->insertCode($code); + } + + /** + * @param string $code + */ + protected function insertCode($code) { + $composerConfig = $this->composer->getConfig(); + $vendorDir = $composerConfig->get('vendor-dir'); + $autoloadFile = $vendorDir . '/autoload.php'; + if (!file_exists($autoloadFile)) { + throw new \RuntimeException(sprintf( + 'Could not adjust autoloader: The file %s was not found.', + $autoloadFile + )); + } + + $this->io->write('Inserting dotenv initialization into main autoload file'); + + // Regex modifiers: + // "m": \s matches newlines + // "D": $ matches at EOF only + // Translation: insert before the last "return" in the file + $contents = preg_replace('/\n(?=return [^;]+;\s*$)/mD', "\n" . $code, file_get_contents($autoloadFile)); + file_put_contents($autoloadFile, $contents); + } + +} \ No newline at end of file