Skip to content

Commit

Permalink
Merge pull request #7 from BeAPI/fix/block-default-namespace
Browse files Browse the repository at this point in the history
Validate theme's namespace
  • Loading branch information
asadowski10 authored Oct 15, 2021
2 parents e624a31 + b6c3904 commit ff4f861
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/Command/ScaffoldThemeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Composer\Command\BaseCommand;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Package\Package;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -85,7 +86,7 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
* @author Julien Maury
* @source https://stackoverflow.com/a/7775949
*/
protected static function recursive_copy( $source, $destination ) {
protected function recursive_copy( $source, $destination ) {
foreach (
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator( $source, \RecursiveDirectoryIterator::SKIP_DOTS ),
Expand All @@ -109,7 +110,7 @@ protected static function recursive_copy( $source, $destination ) {
*
* @return bool
*/
protected static function doStrReplace( $path, $search, $replace = '', $extension = 'php' ) {
protected function doStrReplace( $path, $search, $replace = '', $extension = 'php' ) {

if ( empty( $path ) || empty( $search ) ) {
return false;
Expand All @@ -125,6 +126,7 @@ protected static function doStrReplace( $path, $search, $replace = '', $extensio
}
}

return true;
}

/**
Expand Down Expand Up @@ -202,7 +204,7 @@ protected function generateTheme( $composer, $io, $themeName, $themePath, $theme

$this->recursive_copy( $downloadPath, $themePath );

$themeNamespace = $this->askAndConfirm( $io, "\nWhat is your theme's namespace ? (e.g: 'BEA\\Theme\\Framework')" );
$themeNamespace = $this->askForThemeNamespace( $io, $output );

$this->doStrReplace( $themePath, 'BEA\\Theme\\Framework', $themeNamespace );
$this->replaceHeaderStyle( $themePath, static::$search, $themeCompleteName );
Expand All @@ -222,7 +224,7 @@ protected function generateTheme( $composer, $io, $themeName, $themePath, $theme

$composerFile->write( $composerJson );
$output->writeln( "The namespace have been added to the composer.json file !" );
} catch ( RuntimeException $e ) {
} catch ( \RuntimeException $e ) {
$output->writeln( "<error>An error occurred</error>" );
$output->writeln( sprintf( "<error>%s</error>", $e->getMessage() ) );
exit;
Expand Down Expand Up @@ -290,4 +292,35 @@ protected function getInstallPath( $themeName, $composer ) {

return \rtrim( $path, '/' ) . '/';
}

/**
* Get theme namespace.
*
* Check that the namespace provided by the user is not the same
* as the default one.
*
* @param IOInterface $io
* @param OutputInterface $output
*
* @return string
*/
protected function askForThemeNamespace( $io, $output ) {

$reserved_namespace = [ 'bea\\theme\\framework', 'beapi\\theme\\framework' ];
$themeNamespace = '';
while( empty( $themeNamespace ) ) {
$themeNamespace = $this->askAndConfirm( $io, "\nWhat is your theme's namespace ? (e.g: 'BEA\\Theme\\Framework')" );
if ( in_array( mb_strtolower( trim( $themeNamespace ) ), $reserved_namespace, true ) ) {
$themeNamespace = '';
$output->writeln(
[
"<error>The namespace you chose is not allowed.</error>",
"<error>Please choose a namespace matching your project like ClientName\Theme\MyThemeName.</error>"
]
);
}
}

return $themeNamespace;
}
}

0 comments on commit ff4f861

Please sign in to comment.