Skip to content

Commit

Permalink
add remove function #7
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Jun 9, 2019
1 parent c0edb53 commit 4fd05f8
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## 1.3.0

+ [#6](https://github.com/nadar/php-composer-reader/issues/6) Added add method for require and require-dev section.
+ [#7](https://github.com/nadar/php-composer-reader/issues/7) Added remove() method for require(-dev) and autoload(-dev) sections.
+ [#6](https://github.com/nadar/php-composer-reader/issues/6) Added add() method for require and require-dev sections.
+ Moved interfaces into interface folder.
+ Added AutoloadDevSection
+ Fixed bug in composer reader when file is unable to write.
Expand Down
8 changes: 8 additions & 0 deletions src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class Autoload implements SectionInstanceInterface

public $type;

/**
* Autoload Section
*
* @param ComposerReaderInterface $reader
* @param string $namespace The namespace like \\foo\\bar\\
* @param string $source The path which is root for the namespace like path/to
* @param string $type The type psr-4 or psr-0
*/
public function __construct(ComposerReaderInterface $reader, $namespace, $source, $type)
{
$this->reader = $reader;
Expand Down
30 changes: 28 additions & 2 deletions src/AutoloadSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nadar\PhpComposerReader;

use Exception;
use Nadar\PhpComposerReader\Interfaces\ManipulationInterface;
use Nadar\PhpComposerReader\Interfaces\ComposerReaderInterface;
use Nadar\PhpComposerReader\Interfaces\SectionInstanceInterface;
Expand All @@ -24,6 +25,12 @@ class AutoloadSection extends DataIterator implements ManipulationInterface

const SECTION_KEY = 'autoload';

/**
* Constuctor
*
* @param ComposerReaderInterface $reader
* @param string $type
*/
public function __construct(ComposerReaderInterface $reader, $type = self::TYPE_PSR4)
{
$this->reader = $reader;
Expand Down Expand Up @@ -62,11 +69,30 @@ public function add(SectionInstanceInterface $sectionInstance): ComposerReaderIn

$data = $this->ensureDoubleBackslash($data);

$sectionInstance->reader->updateSection(static::SECTION_KEY, $data);
$this->reader->updateSection(static::SECTION_KEY, $data);

return $sectionInstance->reader;
return $this->reader;
}

/**
* {@inheritDoc}
*/
public function remove($sectionIdentifier): ComposerReaderInterface
{
/** @var array $data */
$data = $this->reader->contentSection(static::SECTION_KEY, []);

foreach ($data as $type => $values) {
if (array_key_exists($sectionIdentifier, $values)) {
unset($data[$type][$sectionIdentifier]);
}
}

$this->reader->updateSection(static::SECTION_KEY, $data);

return $this->reader;
}

/**
* Ensure double black slashes of input data.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Interfaces/ComposerReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function removeSection($section);
* Save the current Data.
*
* Saves the current json data into the composer.json of the given reader.
*
* @return boolean Whether saving was successfull or not.
*/
public function save();
}
8 changes: 8 additions & 0 deletions src/Interfaces/ManipulationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ interface ManipulationInterface
* @return ComposerReaderInterface
*/
public function add(SectionInstanceInterface $sectionInstance): ComposerReaderInterface;

/**
* Removes a given section entry based on the identifier.
*
* @param string $sectionIdentifier The section identifer which is the namespace or the package name
* @return ComposerReaderInterface
*/
public function remove($sectionIdentifier): ComposerReaderInterface;
}
7 changes: 7 additions & 0 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class Package implements SectionInstanceInterface

public $constraint;

/**
* Constructor
*
* @param ComposerReaderInterface $reader
* @param string $name
* @param string $constraint
*/
public function __construct(ComposerReaderInterface $reader, $name, $constraint)
{
$this->reader = $reader;
Expand Down
26 changes: 24 additions & 2 deletions src/RequireSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class RequireSection extends DataIterator implements ManipulationInterface

protected $reader;

/**
* Constructor
*
* @param ComposerReaderInterface $reader
*/
public function __construct(ComposerReaderInterface $reader)
{
$this->reader = $reader;
Expand Down Expand Up @@ -49,8 +54,25 @@ public function add(SectionInstanceInterface $sectionInstance): ComposerReaderIn
/** @var Package $sectionInstance */
$data[$sectionInstance->name] = $sectionInstance->constraint;

$sectionInstance->reader->updateSection(static::SECTION_KEY, $data);
$this->reader->updateSection(static::SECTION_KEY, $data);

return $sectionInstance->reader;
return $this->reader;
}

/**
* {@inheritDoc}
*/
public function remove($sectionIdentifier): ComposerReaderInterface
{
/** @var array $data */
$data = $this->reader->contentSection(static::SECTION_KEY, []);

if (!array_key_exists($sectionIdentifier, $data)) {
throw new Exception("Unable to find the given section key '{$sectionIdentifier}' to remove.");
}
unset($data[$sectionIdentifier]);
$this->reader->updateSection(static::SECTION_KEY, $data);

return $this->reader;
}
}
11 changes: 11 additions & 0 deletions tests/AutoloadSectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,15 @@ public function testSave()

$this->removeTemporaryJson($json);
}

public function testAddAndRemove()
{
$reader = new ComposerReader($this->getValidJson());

$section = new AutoloadSection($reader);
$new = new Autoload($reader, 'Foo\\Bar\\', 'src/foo/bar', AutoloadSection::TYPE_PSR4);

$this->assertTrue($section->add($new)->save());
$this->assertTrue($section->remove($new->namespace)->save());
}
}
11 changes: 11 additions & 0 deletions tests/RequireSectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,15 @@ public function testAddFunctionOfRequireSection()

$this->removeTemporaryJson($json);
}

public function testAddAndRemove()
{
$reader = new ComposerReader($this->getValidJson());
$section = new RequireSection($reader);

$new = new Package($reader, 'xyz', '1.0.0');

$this->assertTrue($section->add($new)->save());
$this->assertTrue($section->remove($new->name)->save());
}
}

0 comments on commit 4fd05f8

Please sign in to comment.