Skip to content

Commit

Permalink
Fix for psr-0.
Browse files Browse the repository at this point in the history
Added 'prepare' filter for running code when a package is being prepped.
Updated sample.config.php and patches.config.php to reflect new 'prepare' filter
  • Loading branch information
jawngee committed Aug 26, 2020
1 parent 90b0bcb commit fd2f1cf
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Source/Commands/RenamespaceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
foreach($project->getPackages() as $packageName => $package) {
$packageSection->overwrite("Processing package $packageName ...");
$packageProgress->advance();
$package->process($packagePrefix, $namespacePrefix, $libraryOutputPath, $project->getPackages());
$package->process($configuration, $packagePrefix, $namespacePrefix, $libraryOutputPath, $project->getPackages());
$allNamespaces = array_merge($allNamespaces, $package->getNamespaces());
$sourceFileCount += count($package->getSourceFiles());
}
Expand Down
10 changes: 10 additions & 0 deletions Source/Models/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ public function __construct(string $configFile = null) {
}
}

public function prepare(string $package, array $config, string $path, string $namespacePrefix) {
if (isset($this->config['prepare'])) {
foreach($this->config['prepare'] as $func) {
$config = call_user_func($func, $package, $config, $path, $namespacePrefix);
}
}

return $config;
}

public function start(string $source, ?string $currentNamespace, string $namespacePrefix, string $package, string $file) {
if (isset($this->config['start'])) {
foreach($this->config['start'] as $func) {
Expand Down
9 changes: 6 additions & 3 deletions Source/Models/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ public function getSourceFiles(): array {
//region Processing

/**
* @param Configuration $configuration
* @param string $packagePrefix
* @param string $namespacePrefix
* @param string $outputPath
* @param Package[] $packages
*/
public function process(string $packagePrefix, string $namespacePrefix, string $outputPath, array $packages) {
public function process($configuration, string $packagePrefix, string $namespacePrefix, string $outputPath, array $packages) {
$outputPath = trailingslashit($outputPath);
$this->outputPath = $outputPath;

Expand All @@ -90,6 +91,8 @@ public function process(string $packagePrefix, string $namespacePrefix, string $

$config = json_decode(file_get_contents($composerFile), true);

$config = $configuration->prepare($this->name, $config, $this->path, $namespacePrefix);

$config['name'] = $packagePrefix.'-'.$config['name'];
$config['version'] = $this->getVersion();

Expand Down Expand Up @@ -173,8 +176,8 @@ public function process(string $packagePrefix, string $namespacePrefix, string $
$this->sourceFiles[] = $file->getRealPath();
}

$namespaceRegex = '/^\s*namespace\s+([^;]+)/m';
$idiotNamespaceRegex = '/^\s*\<\?php\s+namespace\s+([^;]+)/m';
$namespaceRegex = '/^\s*namespace\s+([^\s;]+)/m';
$idiotNamespaceRegex = '/^\s*\<\?php\s+namespace\s+([^\s;]+)/m';
foreach($this->sourceFiles as $file) {
$matches = [];
preg_match_all($namespaceRegex, file_get_contents($file), $matches, PREG_SET_ORDER, 0);
Expand Down
27 changes: 27 additions & 0 deletions patches.config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
<?php

return [
"prepare" => [
function(string $package, array $config, string $path, string $namespacePrefix) {
if (($package == 'kraken-io/kraken-php') && isset($config['autoload']['psr-0']['Kraken'])) {
$srcDir = trailingslashit($path.$config['autoload']['psr-0']['Kraken']);
$finder = new Symfony\Component\Finder\Finder();
$files = [];
foreach($finder->in($srcDir) as $fileInfo) {
$files[] = $fileInfo->getRealPath();
}

if (file_exists($srcDir.'Kraken.php')) {
$source = file_get_contents($srcDir.'Kraken.php');
$source = str_replace('<?php', "<?php\n\nnamespace Kraken;", $source);
file_put_contents($srcDir.'Kraken.php', $source);
}

$namespacedDir = trailingslashit($srcDir.'Kraken');
mkdir($namespacedDir, 0755, true);
foreach($files as $file) {
rename($file, $namespacedDir.pathinfo($file, PATHINFO_BASENAME));
}
}

return $config; // You should always return the $config after manipulating it
}
],

"start" => [
function(string $source, ?string $currentNamespace, string $namespacePrefix, string $package, string $file) {
if ($package === 'duncan3dc/blade') {
Expand Down
14 changes: 14 additions & 0 deletions sample.config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
<?php

return [
/** These functions are called before a package is processed */
"prepare" => [
function(string $package, array $config, string $path, string $namespacePrefix) {
/**
* @var string $package The name of the composer package
* @var array $config The parsed composer.json config for the package
* @var string $path The full path to the package
* @var string $namespacePrefix The namespace prefix
*/

return $config; // You should always return the $config after manipulating it
}
],

/** These functions are called once the source file has been loaded but before all of the namespace changes are processed. */
"start" => [
function(string $source, ?string $currentNamespace, string $namespacePrefix, string $package, string $file) {
Expand Down

0 comments on commit fd2f1cf

Please sign in to comment.