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

Commit

Permalink
Merge pull request #45 from mikey179/master
Browse files Browse the repository at this point in the history
incorporate new rootpath and resource loader
  • Loading branch information
mikey179 committed Jun 7, 2014
2 parents 898aef4 + 825598f commit cc3c3df
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 184 deletions.
16 changes: 8 additions & 8 deletions composer.lock

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

2 changes: 1 addition & 1 deletion src/main/php/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
namespace stubbles\console;
use stubbles\input\Param;
use stubbles\input\ParamErrors;
use stubbles\input\ValueReader;
use stubbles\input\errors\ParamErrors;
use stubbles\streams\InputStream;
use stubbles\streams\OutputStream;
/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/creator/ClassFileCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ClassFileCreator extends FileCreator
public function create($className)
{
if (!class_exists($className)) {
$classFileName = $this->getClassFileName($className);
$classFileName = $this->fileNameforClass($className);
$this->createFile($classFileName, $className, 'class.tmpl');
$this->console->writeLine('Class ' . $className . ' created at ' . $classFileName);
} else {
Expand Down
57 changes: 57 additions & 0 deletions src/main/php/creator/ClassNameFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* This file is part of stubbles.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package stubbles\console
*/
namespace stubbles\console\creator;
use stubbles\input\Filter;
use stubbles\input\Param;
/**
* Filter for class names entered via user input.
*
* @since 3.0.0
*/
class ClassNameFilter implements Filter
{
/**
* apply filter on given param
*
* @param Param $param
* @return mixed filtered value
*/
public function apply(Param $param)
{
if ($param->isEmpty()) {
$param->addError('CLASSNAME_EMPTY');
return null;
}

$className = str_replace('\\\\', '\\', trim($param->value()));
if (! ((bool) preg_match('/^([a-zA-Z_]{1}[a-zA-Z0-9_\\\\]*)$/', $className))) {
$param->addError('CLASSNAME_INVALID');
return null;
}

if (! (bool) preg_match('/^([a-zA-Z_]{1}[a-zA-Z0-9_]*)$/', $this->nonQualifiedClassNameOf($className))) {
$param->addError('CLASSNAME_INVALID');
return null;
}

return $className;
}

/**
* returns non qualified part of class name
*
* @param string $className
* @return string
*/
private function nonQualifiedClassNameOf($className)
{
return substr($className, strrpos($className, '\\') + 1);
}
}
49 changes: 12 additions & 37 deletions src/main/php/creator/ConsoleAppCreator.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?php
/**
* Your license or something other here.
* This file is part of stubbles.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package stubbles\console
*/
namespace stubbles\console\creator;
use stubbles\console\Console;
use stubbles\console\ConsoleApp;
/**
* Your own console app.
* App to create initial source files for other console apps.
*/
class ConsoleAppCreator extends ConsoleApp
{
Expand Down Expand Up @@ -77,13 +80,13 @@ public function __construct(Console $console,
*/
public function run()
{
$this->console->writeLine('Stubbles ConsoleAppCreator')
->writeLine(' (c) 2012-2014 Stubbles Development Group')
->writeLine('')
->writeLine('Please enter the full qualified class name for the console app: ');
$className = str_replace('\\\\', '\\', trim($this->console->readLine()));
if (!$this->isValid($className)) {
$this->console->writeLine('The class name ' . $className . ' is not a valid class name');
$className = $this->console->writeLine('Stubbles ConsoleAppCreator')
->writeLine(' (c) 2012-2014 Stubbles Development Group')
->writeLine('')
->prompt('Please enter the full qualified class name for the console app: ')
->applyFilter(new ClassNameFilter());
if (null === $className) {
$this->console->writeLine('The entered class name is not a valid class name');
return -10;
}

Expand All @@ -92,32 +95,4 @@ public function run()
$this->testFile->create($className);
return 0;
}

/**
* checks if given class name is valid
*
* @param string $className
* @return bool
*/
private function isValid($className)
{
if (! (bool) preg_match('/^([a-zA-Z_]{1}[a-zA-Z0-9_\\\\]*)$/', $className)) {
return false;
}

return (bool) preg_match('/^([a-zA-Z_]{1}[a-zA-Z0-9_]*)$/',
$this->getNonQualifiedClassName($className)
);
}

/**
* returns non qualified part of class name
*
* @param string $className
* @return string
*/
private function getNonQualifiedClassName($className)
{
return substr($className, strrpos($className, '\\') + 1);
}
}
67 changes: 47 additions & 20 deletions src/main/php/creator/FileCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
namespace stubbles\console\creator;
use stubbles\console\Console;
use stubbles\lang\ResourceLoader;
use stubbles\lang\Rootpath;
use stubbles\lang\exception\FileNotFoundException;
/**
* Base class for file creation.
*/
Expand All @@ -23,22 +26,29 @@ abstract class FileCreator
/**
* path to project
*
* @type string
* @type Rootpath
*/
protected $projectPath;
protected $rootpath;
/**
* access to resources
*
* @type ResourceLoader
*/
private $resourceLoader;

/**
* constructor
*
* @param Console $console
* @param string $projectPath path to project
* @param Console $console
* @param Rootpath $rootpath
* @param ResourceLoader $resourceLoader
* @Inject
* @Named{projectPath}('stubbles.project.path')
*/
public function __construct(Console $console, $projectPath)
public function __construct(Console $console, Rootpath $rootpath, ResourceLoader $resourceLoader)
{
$this->console = $console;
$this->projectPath = $projectPath;
$this->console = $console;
$this->rootpath = $rootpath;
$this->resourceLoader = $resourceLoader;
}

/**
Expand All @@ -55,14 +65,14 @@ public abstract function create($className);
* @param string $type
* @return string
*/
protected function getClassFileName($className, $type = 'main')
protected function fileNameforClass($className, $type = 'main')
{
return $this->projectPath
. '/src/' . $type . '/php/'
. str_replace('\\', DIRECTORY_SEPARATOR, $this->getNamespace($className))
. DIRECTORY_SEPARATOR
. $this->getNonQualifiedClassName($className)
. '.php';
return $this->rootpath->to(
'src',
$type,
'php',
str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php'
);
}

/**
Expand All @@ -83,21 +93,38 @@ protected function createFile($fileName, $className, $template)
str_replace(['{NAMESPACE}',
'{CLASS}'
],
[$this->getNamespace($className),
$this->getNonQualifiedClassName($className)
[$this->namespaceOf($className),
$this->nonQualifiedClassNameOf($className)
],
file_get_contents(__DIR__ . '/' . $template)
$this->resourceLoader->load($this->pathForTemplate($template))
)
);
}

/**
* finds absolute path for given template file
*
* @param string $template
* @return string
* @throws FileNotFoundException
*/
private function pathForTemplate($template)
{
$pathes = $this->resourceLoader->availableResourceUris('creator/' . $template);
if (!isset($pathes[0])) {
throw new FileNotFoundException($template);
}

return $pathes[0];
}

/**
* returns namespace part of class name
*
* @param string $className
* @return string
*/
private function getNamespace($className)
private function namespaceOf($className)
{
return substr($className, 0, strrpos($className, '\\'));
}
Expand All @@ -108,7 +135,7 @@ private function getNamespace($className)
* @param string $className
* @return string
*/
private function getNonQualifiedClassName($className)
private function nonQualifiedClassNameOf($className)
{
return substr($className, strrpos($className, '\\') + 1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/creator/ScriptFileCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ScriptFileCreator extends FileCreator
public function create($className)
{
$this->console->writeLine('Please enter the script name for the console app: ');
$stubFileName = $this->projectPath . '/bin/' . $this->console->readLine();
$stubFileName = $this->rootpath->to('bin', $this->console->readLine());
if (!file_exists($stubFileName)) {
$this->createFile($stubFileName, $className, 'script.tmpl');
$this->console->writeLine('Script for ' . $className . ' created at ' . $stubFileName);
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/creator/TestFileCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TestFileCreator extends FileCreator
*/
public function create($className)
{
$testClassFile = $this->getClassFileName($className . 'TestCase', 'test');
$testClassFile = $this->fileNameforClass($className . 'Test', 'test');
if (!file_exists($testClassFile)) {
$this->createFile($testClassFile, $className, 'test.tmpl');
$this->console->writeLine('Test for ' . $className . ' created at ' . $testClassFile);
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use stubbles\lang;
/**
* Test for {NAMESPACE}\{CLASS}.
*/
class {CLASS}TestCase extends \PHPUnit_Framework_TestCase
class {CLASS}Test extends \PHPUnit_Framework_TestCase
{
/**
* instance to test
Expand All @@ -31,7 +31,9 @@ class {CLASS}TestCase extends \PHPUnit_Framework_TestCase
*/
public function annotationsPresentOnConstructor()
{
$this->assertTrue(lang\reflectConstructor($this->instance)->hasAnnotation('Inject'));
$this->assertTrue(
lang\reflectConstructor($this->instance)->hasAnnotation('Inject')
);
}

/**
Expand All @@ -48,7 +50,7 @@ class {CLASS}TestCase extends \PHPUnit_Framework_TestCase
public function canCreateInstance()
{
$this->assertInstanceOf('{NAMESPACE}\{CLASS}',
{CLASS}::create(\stubbles\lang\ResourceLoader::getRootPath())
{CLASS}::create(new lang\Rootpath())
);
}
}
2 changes: 1 addition & 1 deletion src/test/php/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @package stubbles\console
*/
namespace stubbles\console;
use stubbles\input\ParamErrors;
use stubbles\input\errors\ParamErrors;
use stubbles\lang;
/**
* Test for stubbles\console\Console.
Expand Down
Loading

0 comments on commit cc3c3df

Please sign in to comment.