Skip to content

Commit

Permalink
Use files instead of classmap, add non used files into exclude-from-…
Browse files Browse the repository at this point in the history
…classmap
  • Loading branch information
steevanb committed Jul 12, 2017
1 parent e90e59b commit bc944e0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
64 changes: 56 additions & 8 deletions OverloadClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,40 @@ class OverloadClass
* @throws \Exception
*/
public static function overload(Event $event)
{
static::defineAutoloadExcludeFromClassmap($event);
static::defineAutoloadFiles($event);
}

protected static function defineAutoloadExcludeFromClassmap(Event $event)
{
$originalFiles = ($event->isDevMode())
? [static::EXTRA_OVERLOAD_CLASS, static::EXTRA_OVERLOAD_CLASS_DEV]
: [static::EXTRA_OVERLOAD_CLASS];
$overloadFiles = ($event->isDevMode()) ? [] : [static::EXTRA_OVERLOAD_CLASS_DEV];
$autoload = static::getAutoload($event);
$extra = $event->getComposer()->getPackage()->getExtra();

foreach ($originalFiles as $env) {
if (array_key_exists($env, $extra)) {
foreach ($extra[$env] as $className => $infos) {
$autoload['exclude-from-classmap'][] = $infos['original-file'];
}
}
}

foreach ($overloadFiles as $env) {
if (array_key_exists($env, $extra)) {
foreach ($extra[$env] as $className => $infos) {
$autoload['exclude-from-classmap'][] = $infos['overload-file'];
}
}
}

$event->getComposer()->getPackage()->setAutoload($autoload);
}

protected static function defineAutoloadFiles(Event $event)
{
$extra = $event->getComposer()->getPackage()->getExtra();

Expand All @@ -37,13 +71,10 @@ public static function overload(Event $event)
}
$cacheDir = $extra[$cacheDirKey];

$autoload = static::getAutoload($event);

foreach ($envs as $extraKey) {
if (array_key_exists($extraKey, $extra)) {
$autoload = $event->getComposer()->getPackage()->getAutoload();
if (array_key_exists('classmap', $autoload) === false) {
$autoload['classmap'] = array();
}

foreach ($extra[$extraKey] as $className => $infos) {
if (
array_key_exists(static::EXTRA_OVERLOAD_DUPLICATE_ORIGINAL_FILE, $infos) === false
Expand All @@ -56,12 +87,29 @@ public static function overload(Event $event)
$event->getIO()
);
}
$autoload['classmap'][$className] = $infos['overload-file'];
$autoload['files'][$className] = $infos['overload-file'];
}

$event->getComposer()->getPackage()->setAutoload($autoload);
}
}

$event->getComposer()->getPackage()->setAutoload($autoload);
}

/**
* @param Event $event
* @return array
*/
protected static function getAutoload(Event $event)
{
$return = $event->getComposer()->getPackage()->getAutoload();
if (array_key_exists('files', $return) === false) {
$return['files'] = array();
}
if (array_key_exists('exclude-from-classmap', $return) === false) {
$return['exclude-from-classmap'] = array();
}

return $return;
}

/**
Expand Down
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[![version](https://img.shields.io/badge/version-1.2.0-green.svg)](https://github.com/steevanb/composer-overload-class/tree/1.2.0)
[![version](https://img.shields.io/badge/version-1.3.0-green.svg)](https://github.com/steevanb/composer-overload-class/tree/1.3.0)
[![composer](https://img.shields.io/badge/composer-^1.0-blue.svg)](https://getcomposer.org)
![Lines](https://img.shields.io/badge/code%20lines-444-green.svg)
![Lines](https://img.shields.io/badge/code%20lines-495-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/15)
[![SensionLabsInsight](https://img.shields.io/badge/SensionLabsInsight-platinum-brightgreen.svg)](https://insight.sensiolabs.com/projects/a753e540-2863-444f-a174-d743ca475566/analyses/16)
[![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
-----------------------

Allow to overload autoloaded classes, to include your files instead of supposed ones.

Sometimes, you need to overload a class from a dependency. But you can't, cause you've found a nice "new Foo\Bar()" somewhere in this dependency...
Expand All @@ -27,7 +28,7 @@ Installation
------------

```bash
composer require steevanb/composer-overload-class ^1.2
composer require steevanb/composer-overload-class ^1.3
```

Configuration
Expand Down Expand Up @@ -90,20 +91,17 @@ namespace Doctrine\ORM\Internal\Hydration;
use Doctrine\ORM\EntityManagerInterface;
use steevanb\DoctrineStats\Doctrine\ORM\Event\HydrationEventsTrait;

# extends cloned ObjectHydrator class, i just want to change hydrateAllData() code
# extends cloned ObjectHydrator class, I just want to change hydrateAllData() code
class ObjectHydrator extends \ComposerOverloadClass\Doctrine\ORM\Internal\Hydration\ObjectHydrator
{
use HydrationEventsTrait;

/**
* @return EntityManagerInterface
*/
protected function getEntityManager()
protected function getEntityManager(): EntityManagerInterface
{
return $this->_em;
}

protected function hydrateAllData()
protected function hydrateAllData(): void
{
$eventId = $this->dispatchPreHydrationEvent();
parent::hydrateAllData();
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### [1.3.0](../../compare/1.2.0...1.3.0) (2017-07-12)

- Use _files_ instead of _classmap_ Composer configuration to overload classes
- Add non used files into _exclude-from-classmap_ Composer configuration to fix _Ambiguous class resolution_ warning

### [1.2.0](../../compare/1.1.3...1.2.0) (2017-05-29)

- Add ```duplicate-original-file``` configuration to indicate if you want to duplicate original classe or not
Expand Down

0 comments on commit bc944e0

Please sign in to comment.