Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Commit

Permalink
Create console app should respect PSR-4, fixes #49
Browse files Browse the repository at this point in the history
  • Loading branch information
mikey179 committed Mar 7, 2015
1 parent 889447f commit 829ca29
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
4.1.0 (2015-??-??)
------------------

* implemented issue #49: Create console app should respect PSR-4


4.0.1 (2014-08-19)
------------------

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"bin": ["bin/stubcli", "bin/createConsoleApp"],
"extra": {
"branch-alias": {
"dev-master": "4.0.x-dev"
"dev-master": "4.1.x-dev"
}
},
"minimum-stability": "dev",
Expand Down
26 changes: 13 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 53 additions & 9 deletions src/main/php/creator/FileCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use stubbles\console\Console;
use stubbles\lang\ResourceLoader;
use stubbles\lang\Rootpath;
use stubbles\lang\exception\ConfigurationException;
use stubbles\lang\exception\FileNotFoundException;
/**
* Base class for file creation.
Expand Down Expand Up @@ -67,6 +68,18 @@ public abstract function create($className);
*/
protected function fileNameforClass($className, $type = 'main')
{
if (file_exists($this->rootpath->to('composer.json'))) {
$composer = json_decode(file_get_contents($this->rootpath->to('composer.json')), true);
if (isset($composer['autoload']['psr-4'])) {
return $this->fileNameForPsr4(
$composer['autoload']['psr-4'],
$className,
$type
);
}
}

// assume psr-0 with standard stubbles pathes
return $this->rootpath->to(
'src',
$type,
Expand All @@ -75,6 +88,35 @@ protected function fileNameforClass($className, $type = 'main')
);
}

/**
* retrieve psr-4 compatible file name
*
* @param array $psr4Pathes map of psr-4 pathes from composer.json
* @param string $className name of class to retrieve file name for
* @param string $type whether it a normal class or a test class
* @return string
* @throws \stubbles\lang\exception\ConfigurationException
*/
private function fileNameForPsr4(array $psr4Pathes, $className, $type)
{
foreach ($psr4Pathes as $prefix => $path) {
if (substr($className, 0, strlen($prefix)) === $prefix) {
return $this->rootpath->to(
str_replace('main', $type, $path),
str_replace(
'\\',
DIRECTORY_SEPARATOR,
str_replace($prefix, '', $className)
) . '.php'
);
}
}

throw new ConfigurationException(
'No PSR-4 path for class ' . $className . ' found in composer.json'
);
}

/**
* creates file of given type for given class
*
Expand All @@ -89,15 +131,17 @@ protected function createFile($fileName, $className, $template)
mkdir($directory, 0755, true);
}

file_put_contents($fileName,
str_replace(['{NAMESPACE}',
'{CLASS}'
],
[$this->namespaceOf($className),
$this->nonQualifiedClassNameOf($className)
],
$this->resourceLoader->load($this->pathForTemplate($template))
)
file_put_contents(
$fileName,
str_replace(
['{NAMESPACE}',
'{CLASS}'
],
[$this->namespaceOf($className),
$this->nonQualifiedClassNameOf($className)
],
$this->resourceLoader->load($this->pathForTemplate($template))
)
);
}

Expand Down
88 changes: 87 additions & 1 deletion src/test/php/creator/ClassFileCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ class ClassFileCreatorTest extends \PHPUnit_Framework_TestCase
* @type Rootpath
*/
private $rootpath;
/**
* @type \org\bovigo\vfs\vfsDirectory
*/
private $root;

/**
* set up test environment
*/
public function setUp()
{
$this->rootpath = new Rootpath(vfsStream::setup()->url());
$this->root = vfsStream::setup();
$this->rootpath = new Rootpath($this->root->url());
$this->mockConsole = $this->getMockBuilder('stubbles\console\Console')
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -122,4 +127,85 @@ public function skipsClassCreationIfClassAlreadyExists()
$this->classFileCreator->create('stubbles\console\creator\ClassFileCreator');
$this->assertFalse(file_exists($this->rootpath->to('src/main/php/stubbles/console/creator/ClassFileCreator.php')));
}

/**
* @test
* @expectedException stubbles\lang\exception\ConfigurationException
* @since 4.1.0
* @group issue_49
*/
public function throwsConfigurationExceptionWhenNoPsr4PathDefinedForNamespace()
{
vfsStream::newFile('composer.json')
->withContent('{"autoload": { "psr-4": { "stubbles\\\foo\\\": "src/main/php" } }}')
->at($this->root);;
$this->classFileCreator->create('example\console\ExampleConsoleApp');
}

/**
* @test
* @since 4.1.0
* @group issue_49
*/
public function createsClassInPsr4PathIfDoesNotExist()
{
vfsStream::newFile('composer.json')
->withContent('{"autoload": { "psr-4": { "example\\\console\\\": "src/main/php" } }}')
->at($this->root);
$this->mockConsole->expects($this->once())
->method('writeLine')
->with($this->equalTo('Class example\console\ExampleConsoleApp created at ' . $this->rootpath->to('src/main/php/ExampleConsoleApp.php')));
$this->classFileCreator->create('example\console\ExampleConsoleApp');
$this->assertTrue(file_exists($this->rootpath->to('src/main/php/ExampleConsoleApp.php')));
$this->assertEquals(
'<?php
/**
* Your license or something other here.
*
* @package example\console
*/
namespace example\console;
use stubbles\console\ConsoleApp;
/**
* Your own console app.
*
* @AppDescription(\'Description of what the app does\')
*/
class ExampleConsoleApp extends ConsoleApp
{
/**
* returns list of bindings used for this application
*
* @return \stubbles\ioc\module\BindingModule[]
*/
public static function __bindings()
{
return [self::argumentParser()];
}
/**
* constructor
*
* @Inject
*/
public function __construct()
{
// TODO add your constructor parameters and code
}
/**
* runs the command and returns an exit code
*
* @return int
*/
public function run()
{
// TODO add your application code
return 0;
}
}
',
file_get_contents($this->rootpath->to('src/main/php/ExampleConsoleApp.php'))
);
}
}
Loading

0 comments on commit 829ca29

Please sign in to comment.