forked from helhum/dotenv-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
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 |
---|---|---|
@@ -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": "[email protected]" | ||
} | ||
], | ||
"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" | ||
} | ||
} | ||
} |
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,105 @@ | ||
<?php | ||
namespace Helhum\DotEnvConnector; | ||
|
||
/* | ||
* This file is part of the class alias loader package. | ||
* | ||
* (c) Helmut Hummel <[email protected]> | ||
* | ||
* 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('<info>Inserting dotenv initialization into main autoload file</info>'); | ||
|
||
// 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); | ||
} | ||
|
||
} |