Skip to content

Commit

Permalink
Created a composer file reader to grab the namespaces for command line
Browse files Browse the repository at this point in the history
  • Loading branch information
silvamfilipe committed Jan 27, 2017
1 parent e9c9b41 commit 51c9b5b
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 2 deletions.
31 changes: 31 additions & 0 deletions spec/Console/Command/Task/AskForNamespace/ComposerReaderSpec.php
@@ -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);
}
}
@@ -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());
}
}
28 changes: 28 additions & 0 deletions spec/Console/Command/Task/AskForWebRootSpec.php
Expand Up @@ -7,10 +7,12 @@
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Slick\Mvc\Console\Command\TaskInterface;
use Slick\Mvc\Console\Exception\OperationAbortedException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;

class AskForWebRootSpec extends ObjectBehavior
Expand Down Expand Up @@ -43,6 +45,32 @@ function it_asks_for_http_server_document_root_path(
$this->execute($input, $output)->shouldBe($expected);
}

function it_asks_for_override_if_index_file_exists(
Command $command,
QuestionHelper $helper,
InputInterface $input,
OutputInterface $output
) {
$command->getHelper('question')
->shouldBeCalled()
->willReturn($helper);

$helper->ask($input, $output, Argument::type(ConfirmationQuestion::class))
->shouldBeCalled()
->willReturn(false);

$this->shouldThrow(OperationAbortedException::class)
->during('check', ['features/app/webroot', $input, $output]);
}

function it_check_for_index_file_in_folder(
InputInterface $input,
OutputInterface $output
) {

$this->check('webroot', $input, $output)->shouldBe(true);
}

private function validateQuestion(Question $question)
{
$message = "What's the application document root? (webroot): ";
Expand Down
16 changes: 16 additions & 0 deletions src/Console/Command/Task/AskForNamespace.php
@@ -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 src/Console/Command/Task/AskForNamespace/ComposerReader.php
@@ -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 src/Console/Command/Task/AskForNamespace/NameSpaceCollection.php
@@ -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 src/Console/Command/Task/AskForNamespace/NameSpaceEntry.php
@@ -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;
}
}
37 changes: 35 additions & 2 deletions src/Console/Command/Task/AskForWebRoot.php
Expand Up @@ -10,10 +10,12 @@
namespace Slick\Mvc\Console\Command\Task;

use Slick\Mvc\Console\Command\TaskInterface;
use Slick\Mvc\Console\Exception\OperationAbortedException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;

/**
Expand Down Expand Up @@ -49,17 +51,48 @@ public function __construct(Command $command)
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*
* @return mixed
* @return mixed|false
*/
public function execute(InputInterface $input, OutputInterface $output)
{
/** @var QuestionHelper $helper */
$helper = $this->command->getHelper('question');
$default = 'webroot';
$question = "What's the application document root? ({$default}): ";
return $helper->ask(
$docRoot = $helper->ask(
$input,
$output,
new Question($question, $default));

return $this->check($docRoot, $input, $output) ? $docRoot : false;
}

/**
* Checks if the document root has an index.php file. If so overwrite?
*
* @param string $docRoot
* @param InputInterface $input
* @param OutputInterface $output
*
* @return bool
*/
public function check($docRoot, InputInterface $input, OutputInterface $output)
{
if (!file_exists(getcwd()."/$docRoot")) return true;

/** @var QuestionHelper $helper */
$helper = $this->command->getHelper('question');

$question = "There is an 'index.php' file in this folder, overwrite it? (y/N): ";
$default = false;
$question = new ConfirmationQuestion($question, $default, '/(y|yes)/i');

if (!$helper->ask($input, $output, $question)) {
throw new OperationAbortedException(
"Operation aborted! No file file will be overwritten."
);
}

return true;
}
}

0 comments on commit 51c9b5b

Please sign in to comment.