Commit ffb2e54a authored by Steevan BARBOYON's avatar Steevan BARBOYON
Browse files

Throw \Esception when extra/composer-overload-cache-dir is not defined, write infos when verbose

parent aa9488ec
Showing with 58 additions and 23 deletions
+58 -23
......@@ -3,6 +3,7 @@
namespace steevanb\ComposerOverloadClass;
use Composer\Script\Event;
use Composer\IO\IOInterface;
class OverloadClass
{
......@@ -14,6 +15,7 @@ class OverloadClass
/**
* @param Event $event
* @throws \Exception
*/
public static function overload(Event $event)
{
......@@ -21,14 +23,18 @@ class OverloadClass
if ($event->isDevMode()) {
$envs = [static::EXTRA_OVERLOAD_CLASS, static::EXTRA_OVERLOAD_CLASS_DEV];
$cacheDir = static::EXTRA_OVERLOAD_CACHE_DIR_DEV;
if (array_key_exists($cacheDir, $extra) === false) {
$cacheDir = static::EXTRA_OVERLOAD_CACHE_DIR;
$cacheDirKey = static::EXTRA_OVERLOAD_CACHE_DIR_DEV;
if (array_key_exists($cacheDirKey, $extra) === false) {
$cacheDirKey = static::EXTRA_OVERLOAD_CACHE_DIR;
}
} else {
$envs = [static::EXTRA_OVERLOAD_CLASS];
$cacheDir = static::EXTRA_OVERLOAD_CACHE_DIR;
$cacheDirKey = static::EXTRA_OVERLOAD_CACHE_DIR;
}
if (array_key_exists($cacheDirKey, $extra) === false) {
throw new \Exception('You must specify extra/' . $cacheDirKey . ' in composer.json');
}
$cacheDir = $extra[$cacheDirKey];
foreach ($envs as $extraKey) {
if (array_key_exists($extraKey, $extra)) {
......@@ -39,9 +45,10 @@ class OverloadClass
foreach ($extra[$extraKey] as $className => $infos) {
static::generateProxy(
$extra[$cacheDir],
$cacheDir,
$className,
$infos['original-file']
$infos['original-file'],
$event->getIO()
);
$autoload['classmap'][$className] = $infos['overload-file'];
}
......@@ -51,30 +58,48 @@ class OverloadClass
}
}
/**
* @param string $path
* @param IOInterface $io
*/
protected function createDirectories($path, IOInterface $io)
{
if (is_dir($path) === false) {
$io->write('Creating directory <info>' . $path . '</info>.', true, IOInterface::VERBOSE);
$createdPath = null;
foreach (explode(DIRECTORY_SEPARATOR, $path) as $directory) {
if (is_dir($createdPath . $directory) === false) {
mkdir($createdPath . $directory);
}
$createdPath .= $directory . DIRECTORY_SEPARATOR;
}
}
}
/**
* @param string $cacheDir
* @param string $fullyQualifiedClassName
* @param string $filePath
* @param IOInterface $io
* @return string
*/
protected static function generateProxy($cacheDir, $fullyQualifiedClassName, $filePath)
protected static function generateProxy($cacheDir, $fullyQualifiedClassName, $filePath, IOInterface $io)
{
if (is_dir($cacheDir) === false) {
mkdir($cacheDir);
}
$php = static::getPhpForDuplicatedFile($filePath, $fullyQualifiedClassName);
$classNameParts = array_merge(array(static::NAMESPACE_PREFIX), explode('\\', $fullyQualifiedClassName));
array_pop($classNameParts);
foreach ($classNameParts as $part) {
$cacheDir .= DIRECTORY_SEPARATOR . $part;
if (is_dir($cacheDir) === false) {
mkdir($cacheDir);
}
}
$finalCacheDir = $cacheDir . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $classNameParts);
static::createDirectories($finalCacheDir, $io);
$overloadedFilePath = $cacheDir . DIRECTORY_SEPARATOR . basename($filePath);
$overloadedFilePath = $finalCacheDir . DIRECTORY_SEPARATOR . basename($filePath);
file_put_contents($overloadedFilePath, $php);
$io->write(
'<info>' . $filePath . '</info> is overloaded by <comment>' . $overloadedFilePath . '</comment>',
true,
IOInterface::VERBOSE
);
}
/**
......
[![version](https://img.shields.io/badge/version-1.1.1-green.svg)](https://github.com/steevanb/composer-overload-class/tree/1.1.1)
[![version](https://img.shields.io/badge/version-1.1.2-green.svg)](https://github.com/steevanb/composer-overload-class/tree/1.1.2)
[![composer](https://img.shields.io/badge/composer-^1.0-blue.svg)](https://getcomposer.org)
![Lines](https://img.shields.io/badge/code lines-387-green.svg)
![Lines](https://img.shields.io/badge/code lines-431-green.svg)
![Total Downloads](https://poser.pugx.org/steevanb/composer-overload-class/downloads)
[![SensionLabsInsight](https://img.shields.io/badge/SensionLabsInsight-platinum-brightgreen.svg)](https://insight.sensiolabs.com/projects/a753e540-2863-444f-a174-d743ca475566/analyses/1)
[![SensionLabsInsight](https://img.shields.io/badge/SensionLabsInsight-platinum-brightgreen.svg)](https://insight.sensiolabs.com/projects/a753e540-2863-444f-a174-d743ca475566/analyses/8)
[![Scrutinizer](https://scrutinizer-ci.com/g/steevanb/composer-overload-class/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/steevanb/composer-overload-class/)
composer-overload-class
......@@ -21,11 +21,13 @@ Your class need to have exact same namespace as overloaded one, and you can exte
![schema](schema.png)
[Changelog](changelog.md)
Installation
------------
```bash
composer require steevanb/composer-overload-class 1.0.*
composer require steevanb/composer-overload-class ^1.1
```
Configuration
......@@ -65,6 +67,7 @@ Example taken from [steevanb/doctrine-stats](https://github.com/steevanb/doctrin
When configuration is finished, you need to re-generate Composer autoload :
```bash
composer dumpautoload
composer dumpautoload -v
```
That's all folks !
......
1.1.2 (2016-12-28)
------------------
- Throw \Exception if extra/composer-overload-cache-dir is not defined in composer.json
- Write Creating dir when composer is called with -v
- Write Foo.php is overloaded by Bar.php when composer is called with -v
1.1.1 (2016-11-19)
------------------
......@@ -14,7 +21,7 @@
- Fix add use to ComposerOverloadClass
1.0.0 (2016-07)17)
1.0.0 (2016-07-17)
------------------
- Create ComposerOverloadClass
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment