-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
207 additions
and
7 deletions.
There are no files selected for viewing
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,66 @@ | ||
<?php | ||
|
||
namespace WPMVC\Commands; | ||
|
||
use Exception; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use WPMVC\Commands\Base\BaseCommand as Command; | ||
use Ayuco\Exceptions\NoticeException; | ||
use WPMVC\Commands\Core\Builder; | ||
|
||
/** | ||
* Prettifies PHP content using this package's pretty printer. | ||
* | ||
* @author Ale Mostajo | ||
* @copyright 10 Quality <http://www.10quality.com> | ||
* @license MIT | ||
* @package WPMVC\Commands | ||
* @version 1.1.9 | ||
*/ | ||
class PrettifyCommand extends Command | ||
{ | ||
/** | ||
* Command key. | ||
* @since 1.1.9 | ||
* @var string | ||
*/ | ||
protected $key = 'prettify'; | ||
|
||
/** | ||
* Command description. | ||
* @since 1.1.9 | ||
* @var string | ||
*/ | ||
protected $description = 'Prettifies the PHP code inside the "/app" directory.'; | ||
|
||
/** | ||
* Calls to command action. | ||
* @since 1.1.9 | ||
* | ||
* @param array $args Action arguments. | ||
*/ | ||
public function call($args = []) | ||
{ | ||
try { | ||
$path = $this->rootPath.'/app'; | ||
if (!is_dir($path)) return; | ||
$dir = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS); | ||
foreach (new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST) as $filename => $item) { | ||
if (!$item->isDir() | ||
&& !preg_match('/app(\/|\\\)(Boot|Config)(\/|\\\)/', $filename) | ||
&& $item->getExtension() === 'php' | ||
) { | ||
$builder = Builder::parser($filename); | ||
$builder->build(); | ||
} | ||
} | ||
} catch (Exception $e) { | ||
file_put_contents( | ||
$this->rootPath.'/error_log', | ||
$e->getMessage() | ||
); | ||
throw new NoticeException('Command "'.$this->key.'": Fatal error occurred.'); | ||
} | ||
} | ||
} |
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,120 @@ | ||
<?php | ||
/** | ||
* Tests the prettify command. | ||
* | ||
* @author Ale Mostajo | ||
* @copyright 10 Quality <http://www.10quality.com> | ||
* @license MIT | ||
* @package WPMVC\Commands | ||
* @version 1.1.9 | ||
*/ | ||
class PrettifyTest extends WpmvcAyucoTestCase | ||
{ | ||
/** | ||
* Tests path. | ||
*/ | ||
protected $path = [ | ||
TESTING_PATH.'/app/Models', | ||
TESTING_PATH.'/app/Classes', | ||
TESTING_PATH.'/app/Functions', | ||
TESTING_PATH.'/app/Boot', | ||
]; | ||
/** | ||
* Tests prettify on framework folders. | ||
*/ | ||
public function testPrettifyModels() | ||
{ | ||
// Prepare | ||
$dir = TESTING_PATH.'/app/Models/'; | ||
$filename = TESTING_PATH.'/app/Models/Custom.php'; | ||
// Execure | ||
if (!is_dir($dir)) mkdir($dir); | ||
file_put_contents($filename, '<?php class Custom { function artu($artu) {$array2=[5,6,\'7\'];} }'); | ||
exec('php '.WPMVC_AYUCO.' prettify'); | ||
// Assert | ||
$this->assertStringMatchContents('function artu( $artu )', $filename); | ||
$this->assertStringMatchContents('$array2 = [ 5, 6, \'7\' ];', $filename); | ||
} | ||
/** | ||
* Tests prettify on custom folders and files. | ||
*/ | ||
public function testPrettifyCustomClass() | ||
{ | ||
// Prepare | ||
$dir = TESTING_PATH.'/app/Classes/'; | ||
$filename = TESTING_PATH.'/app/Classes/Test.php'; | ||
// Execure | ||
if (!is_dir($dir)) mkdir($dir); | ||
file_put_contents($filename, '<?php class Test { function artu($artu) {$array2=[5,6,\'7\'];} }'); | ||
exec('php '.WPMVC_AYUCO.' prettify'); | ||
// Assert | ||
$this->assertStringMatchContents('function artu( $artu )', $filename); | ||
$this->assertStringMatchContents('$array2 = [ 5, 6, \'7\' ];', $filename); | ||
} | ||
/** | ||
* Tests prettify on function files. | ||
*/ | ||
public function testPrettifyFunctionFile() | ||
{ | ||
// Prepare | ||
$dir = TESTING_PATH.'/app/Functions/'; | ||
$filename = TESTING_PATH.'/app/Functions/functions.php'; | ||
// Execure | ||
if (!is_dir($dir)) mkdir($dir); | ||
file_put_contents($filename, '<?php function artu($artu) {$array2=[5,6,\'7\'];}'); | ||
exec('php '.WPMVC_AYUCO.' prettify'); | ||
// Assert | ||
$this->assertStringMatchContents('function artu( $artu )', $filename); | ||
$this->assertStringMatchContents('$array2 = [ 5, 6, \'7\' ];', $filename); | ||
} | ||
/** | ||
* Tests preserved configuretion files. | ||
*/ | ||
public function testNoPrettifyConfigFiles() | ||
{ | ||
// Prepare | ||
$dir = TESTING_PATH.'/app/Config/'; | ||
$filename = TESTING_PATH.'/app/Config/test.php'; | ||
// Execure | ||
if (!is_dir($dir)) mkdir($dir); | ||
file_put_contents($filename, '<?php return [\'test\' => 123,321];}'); | ||
exec('php '.WPMVC_AYUCO.' prettify'); | ||
// Assert | ||
$this->assertStringMatchContents('return [\'test\' => 123,321]', $filename); | ||
$this->assertNotStringMatchContents('[ \'test\' => 123, 321 ]', $filename); | ||
// Teardown | ||
unlink($filename); | ||
} | ||
/** | ||
* Tests preserved boot file. | ||
*/ | ||
public function testNoPrettifyBootFile() | ||
{ | ||
// Prepare | ||
$dir = TESTING_PATH.'/app/Boot/'; | ||
$filename = TESTING_PATH.'/app/Boot/bootstrap.php'; | ||
// Execure | ||
if (!is_dir($dir)) mkdir($dir); | ||
file_put_contents($filename, '<?php return [\'test\' => 123,321];}'); | ||
exec('php '.WPMVC_AYUCO.' prettify'); | ||
// Assert | ||
$this->assertStringMatchContents('return [\'test\' => 123,321]', $filename); | ||
$this->assertNotStringMatchContents('[ \'test\' => 123, 321 ]', $filename); | ||
} | ||
/** | ||
* Tests prettify on root file. | ||
*/ | ||
public function testPrettifyRootFile() | ||
{ | ||
// Prepare | ||
$filename = TESTING_PATH.'/app/Root.php'; | ||
// Execure | ||
file_put_contents($filename, '<?php class Root { function artu($artu) {$array2=[5,6,\'7\'];} }'); | ||
exec('php '.WPMVC_AYUCO.' prettify'); | ||
// Assert | ||
$this->assertStringMatchContents('function artu( $artu )', $filename); | ||
$this->assertStringMatchContents('$array2 = [ 5, 6, \'7\' ];', $filename); | ||
// Teardown | ||
unlink($filename); | ||
} | ||
} |
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