Skip to content

Commit

Permalink
Merge pull request #4 from BaronVonPerko/master
Browse files Browse the repository at this point in the history
Allow user to change the namespace
  • Loading branch information
Alecaddd authored Mar 13, 2018
2 parents 0433e61 + a335e83 commit d9c959a
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use GuzzleHttp\ClientInterface;
use Symfony\Component\Console\Question\Question;
use ZipArchive;

class NewCommand extends Command
Expand All @@ -30,18 +31,24 @@ public function configure()

public function execute(InputInterface $input, OutputInterface $output)
{
$directory = getcwd().'/'.$input->getArgument('name');
$themeName = $input->getArgument('name');
$directory = getcwd().'/'.$themeName;

$output->writeln('<info>Summoning application..</info>');

$this->assertApplicationDoesNotExists($directory, $output);

$this->assertLocationInsideWordPress($directory, $output);

$helper = $this->getHelper("question");
$question = new Question("Change Namespace? <info>(Awps)</info> ", "Awps");
$namespace = $helper->ask($input, $output, $question);

$output->writeln('<info>Downloading Package..</info>');

$this->download($ZipFile = $this->makeFileName())
->extract($ZipFile, $directory, $output)
->renameNamespaces($directory, $output, $themeName, $namespace)
->cleanUp($ZipFile);

$output->writeln('<info>Updating config files..</info>');
Expand Down Expand Up @@ -103,6 +110,45 @@ private function extract($ZipFile, $directory, OutputInterface $output)
return $this;
}

private function renameNamespaces($directory, OutputInterface $output, $themeName = null, $namespace = null)
{
if(is_null($themeName) && is_null($namespace)) return $this;

$file_info = array();

$this->recursiveScanFiles($directory, $file_info);

foreach($file_info as $file) {
$str = file_get_contents($file);

if(!is_null($namespace)) {
$str = str_replace( "Awps", $namespace, $str );
$str = str_replace( "awps", $namespace, $str );
}

if(!is_null($themeName)) {
$str = str_replace( "Alecaddd WordPress Starter theme", $themeName, $str );
}

file_put_contents($file, $str);
}

return $this;
}

private function recursiveScanFiles($path, &$file_info){
$path = rtrim($path, '/');
if(!is_dir($path)) $file_info[] = $path;
else {
$files = scandir($path);
foreach($files as $file) {
if($file != '.' && $file != '..') {
$this->recursiveScanFiles($path . '/' . $file, $file_info);
}
}
}
}

private function cleanUp($ZipFile)
{
@chmod($ZipFile, 0777);
Expand Down

0 comments on commit d9c959a

Please sign in to comment.