Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a composer file reader to grab the namespaces for command line
- Loading branch information
1 parent
e9c9b41
commit 51c9b5b
Showing
11 changed files
with
412 additions
and
2 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
spec/Console/Command/Task/AskForNamespace/ComposerReaderSpec.php
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,31 @@ | ||
<?php | ||
|
||
namespace spec\Slick\Mvc\Console\Command\Task\AskForNamespace; | ||
|
||
use Slick\Mvc\Console\Command\Task\AskForNamespace\ComposerReader; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Slick\Mvc\Console\Command\Task\AskForNamespace\NameSpaceCollection; | ||
use Slick\Mvc\Console\Command\Task\AskForNamespace\NameSpaceEntry; | ||
|
||
class ComposerReaderSpec extends ObjectBehavior | ||
{ | ||
function let() | ||
{ | ||
$composerFile = getcwd(). '/composer.json'; | ||
$this->beConstructedWith($composerFile); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(ComposerReader::class); | ||
} | ||
|
||
function it_reads_the_composer_file_to_retrieve_a_namespace_collection() | ||
{ | ||
$collection = $this->nameSpaces(); | ||
$collection->shouldBeAnInstanceOf(NameSpaceCollection::class); | ||
$collection->offsetGet(0) | ||
->shouldBeAnInstanceOf(NameSpaceEntry::class); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
spec/Console/Command/Task/AskForNamespace/NameSpaceCollectionSpec.php
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,28 @@ | ||
<?php | ||
|
||
namespace spec\Slick\Mvc\Console\Command\Task\AskForNamespace; | ||
|
||
use Slick\Common\Utils\CollectionInterface; | ||
use Slick\Mvc\Console\Command\Task\AskForNamespace\NameSpaceCollection; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Slick\Mvc\Console\Command\Task\AskForNamespace\NameSpaceEntry; | ||
|
||
class NameSpaceCollectionSpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(NameSpaceCollection::class); | ||
} | ||
|
||
function its_a_collection_of_namespace_entries() | ||
{ | ||
$this->shouldBeAnInstanceOf(CollectionInterface::class); | ||
} | ||
|
||
function it_only_accepts_namespace_entries() | ||
{ | ||
$nameSpace = new NameSpaceEntry('Foo\\Bar', 'src'); | ||
$this->add($nameSpace)->shouldBe($this->getWrappedObject()); | ||
} | ||
} |
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
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,16 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of slick/mvc | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Slick\Mvc\Console\Command\Task; | ||
|
||
|
||
class AskForNamespace | ||
{ | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
src/Console/Command/Task/AskForNamespace/ComposerReader.php
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,97 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of slick/mvc package | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Slick\Mvc\Console\Command\Task\AskForNamespace; | ||
|
||
/** | ||
* ComposerReader | ||
* | ||
* @package Slick\Mvc\Console\Command\Task\AskForNamespace | ||
* @author Filipe Silva <silvam.filipe@gmail.com> | ||
*/ | ||
class ComposerReader | ||
{ | ||
/** | ||
* @var NameSpaceCollection | ||
*/ | ||
private $collection; | ||
|
||
/** | ||
* @var object | ||
*/ | ||
private $data; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $basePath; | ||
|
||
/** | ||
* Creates a composer reader | ||
* | ||
* @param string $composerFile | ||
*/ | ||
public function __construct($composerFile) | ||
{ | ||
$this->read($composerFile); | ||
$this->collection = new NameSpaceCollection(); | ||
} | ||
|
||
/** | ||
* Get the namespaces collection | ||
* | ||
* @return NameSpaceCollection|NameSpaceEntry[] | ||
*/ | ||
public function nameSpaces() | ||
{ | ||
foreach ($this->data['autoload'] as $item) { | ||
$this->add($item); | ||
} | ||
foreach ($this->data['autoload-dev'] as $item) { | ||
$this->add($item); | ||
} | ||
return $this->collection; | ||
} | ||
|
||
/** | ||
* Read composer file | ||
* | ||
* @param string $composerFile | ||
*/ | ||
private function read($composerFile) | ||
{ | ||
$this->basePath = dirname($composerFile); | ||
$this->data = json_decode(file_get_contents($composerFile), true); | ||
} | ||
|
||
/** | ||
* Adds a new item to the collection | ||
* | ||
* @param array $item | ||
*/ | ||
private function add(array $item) | ||
{ | ||
foreach ($item as $nameSpace => $path) { | ||
$entry = new NameSpaceEntry($nameSpace, $this->composePath($path)); | ||
$this->collection->offsetSet(null, $entry); | ||
} | ||
} | ||
|
||
/** | ||
* Set the base path to the namespace path | ||
* | ||
* @param string $path | ||
* | ||
* @return string | ||
*/ | ||
private function composePath($path) | ||
{ | ||
return $this->basePath .'/'. $path; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Console/Command/Task/AskForNamespace/NameSpaceCollection.php
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,42 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of slick/mvc package | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Slick\Mvc\Console\Command\Task\AskForNamespace; | ||
|
||
use Slick\Common\Utils\Collection\AbstractCollection; | ||
use Slick\Common\Utils\CollectionInterface; | ||
|
||
/** | ||
* NameSpaceCollection | ||
* | ||
* @package Slick\Mvc\Console\Command\Task\AskForNamespace | ||
* @author Filipe Silva <silvam.filipe@gmail.com> | ||
*/ | ||
class NameSpaceCollection extends AbstractCollection implements | ||
CollectionInterface | ||
{ | ||
|
||
public function offsetSet($offset, $value) | ||
{ | ||
$this->add($value); | ||
} | ||
|
||
/** | ||
* Adds an item to the collection | ||
* | ||
* @param NameSpaceEntry $nameSpace | ||
* | ||
* @return NameSpaceCollection | ||
*/ | ||
public function add(NameSpaceEntry $nameSpace) | ||
{ | ||
$this->data[] = $nameSpace; | ||
return $this; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Console/Command/Task/AskForNamespace/NameSpaceEntry.php
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,62 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of slick/mvc | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Slick\Mvc\Console\Command\Task\AskForNamespace; | ||
|
||
/** | ||
* NameSpaceEntry | ||
* | ||
* @package Slick\Mvc\Console\Command\Task\AskForNamespace | ||
* @author Filipe Silva <silvam.filipe@gmail.com> | ||
*/ | ||
class NameSpaceEntry | ||
{ | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $nameSpace; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $path; | ||
|
||
/** | ||
* Creates a Namespace Entry | ||
* | ||
* @param string $nameSpace | ||
* @param string $path | ||
*/ | ||
public function __construct($nameSpace, $path) | ||
{ | ||
$this->nameSpace = $nameSpace; | ||
$this->path = $path; | ||
} | ||
|
||
/** | ||
* Get the entry name space | ||
* | ||
* @return string | ||
*/ | ||
public function getNameSpace() | ||
{ | ||
return $this->nameSpace; | ||
} | ||
|
||
/** | ||
* Get the path for this name space entry | ||
* | ||
* @return string | ||
*/ | ||
public function getPath() | ||
{ | ||
return $this->path; | ||
} | ||
} |
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
Oops, something went wrong.