Skip to content

Commit

Permalink
Merge pull request #1136 from YesWiki/fix/extension-import-namespace
Browse files Browse the repository at this point in the history
fix(extension): allow import class from extension with dash in folder
  • Loading branch information
mrflos authored Mar 6, 2024
2 parents b93eb9c + 15a876b commit e932e73
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion includes/services/Performer.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function createPerformable(array $object, array &$vars, &$output)
/* extract extension name from path to allow namespace */
if (preg_match('/(?:tools[\\\\\\/]([A-Za-z0-9_\\-]+)|(custom))[\\\\\/][a-zA-Z0-9_\\\\\/\\-]+.php$/', $object['filePath'], $matches)) {
$extensionName = empty($matches[1]) ? $matches[2]:$matches[1];
$classNameWithNamespace = "YesWiki\\".ucfirst(strtolower($extensionName))."\\".$object['baseName'];
$classNameWithNamespace = "YesWiki\\".StringUtilService::folderToNamespace($extensionName)."\\".$object['baseName'];
if (class_exists($classNameWithNamespace)) {
$className = $classNameWithNamespace;
}
Expand Down
14 changes: 14 additions & 0 deletions includes/services/StringUtilService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace YesWiki\Core\Service;

class StringUtilService
{
public static function folderToNamespace(string $folder): string
{
if(preg_match_all('/[a-zA-Z0-9]+/', $folder, $matches) === false) {
return '';
}
return implode('', array_map(function ($input) {return ucfirst(strtolower($input));}, $matches[0]));
}
}
39 changes: 39 additions & 0 deletions tests/includes/services/StringUtilServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace YesWiki\Test\Core\Service;

use PHPUnit\Framework\TestCase;
use YesWiki\Core\Service\StringUtilService;

require_once 'includes/services/StringUtilService.php';

class StringUtilServiceTest extends TestCase
{
/**
* @dataProvider folderToNamespaceProvider
*/
public function testFolderToNamespace(string $input, string $expected)
{
$this->assertEquals(
$expected,
StringUtilService::folderToNamespace($input),
'Unable to convert : ' . $input
);
}

public function folderToNamespaceProvider()
{
return [
['', ''],
['.', ''],
['foo', 'Foo'],
['Foo', 'Foo'],
['foo1', 'Foo1'],
['foO', 'Foo'],
['foo.bar', 'FooBar'],
['foo-bar', 'FooBar'],
['foo_bar', 'FooBar'],
['foo~bar', 'FooBar'],
];
}
}

0 comments on commit e932e73

Please sign in to comment.