mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 01:31:45 +01:00
Bundle - getBundleViewPath() method - return path using namespaced syntax
This commit is contained in:
@@ -52,6 +52,40 @@ class Bundle
|
|||||||
$viewPath = sprintf('%s.%s', $viewPath, $extension);
|
$viewPath = sprintf('%s.%s', $viewPath, $extension);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sprintf('%s:%s', $bundleName, $viewPath);
|
/*
|
||||||
|
* Prepare short name of bundle and path of view / template with "/" (instead of ":")
|
||||||
|
*/
|
||||||
|
$shortBundleName = static::getShortBundleName($bundleName);
|
||||||
|
$viewPath = str_replace(':', '/', $viewPath);
|
||||||
|
|
||||||
|
return sprintf('@%s/%s', $shortBundleName, $viewPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns short name of bundle (without "Bundle")
|
||||||
|
*
|
||||||
|
* @param string $fullBundleName Full name of the bundle, e.g. "MyExtraBundle"
|
||||||
|
* @return string|null
|
||||||
|
*
|
||||||
|
* @throws IncorrectBundleNameException
|
||||||
|
*/
|
||||||
|
public static function getShortBundleName($fullBundleName)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Given name of bundle is invalid?
|
||||||
|
*/
|
||||||
|
if (!Regex::isValidBundleName($fullBundleName)) {
|
||||||
|
if (!is_string($fullBundleName)) {
|
||||||
|
$fullBundleName = gettype($fullBundleName);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IncorrectBundleNameException($fullBundleName);
|
||||||
|
}
|
||||||
|
|
||||||
|
$matches = [];
|
||||||
|
$pattern = Regex::getBundleNamePattern();
|
||||||
|
preg_match($pattern, $fullBundleName, $matches);
|
||||||
|
|
||||||
|
return $matches[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -737,8 +737,18 @@ class Regex
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$pattern = self::$patterns['bundleName'];
|
$pattern = self::getBundleNamePattern();
|
||||||
|
|
||||||
return (bool)preg_match($pattern, $bundleName);
|
return (bool)preg_match($pattern, $bundleName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns pattern used to validate / verify name of bundle
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function getBundleNamePattern()
|
||||||
|
{
|
||||||
|
return self::$patterns['bundleName'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,6 +83,42 @@ class BundleTest extends BaseTestCase
|
|||||||
self::assertEquals($expected, Bundle::getBundleViewPath($viewPath, $bundleName, $extension));
|
self::assertEquals($expected, Bundle::getBundleViewPath($viewPath, $bundleName, $extension));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $emptyValue Empty value, e.g. ""
|
||||||
|
*
|
||||||
|
* @throws IncorrectBundleNameException
|
||||||
|
* @dataProvider provideEmptyValue
|
||||||
|
*/
|
||||||
|
public function testGetShortBundleNameUsingEmptyValue($emptyValue)
|
||||||
|
{
|
||||||
|
$this->setExpectedException(IncorrectBundleNameException::class);
|
||||||
|
Bundle::getShortBundleName($emptyValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $bundleName Full name of the bundle, e.g. "MyExtraBundle"
|
||||||
|
*
|
||||||
|
* @throws IncorrectBundleNameException
|
||||||
|
* @dataProvider provideIncorrectBundleName
|
||||||
|
*/
|
||||||
|
public function testGetShortBundleNameUsingIncorrectBundleName($bundleName)
|
||||||
|
{
|
||||||
|
$this->setExpectedException(IncorrectBundleNameException::class);
|
||||||
|
Bundle::getShortBundleName($bundleName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $fullBundleName Full name of the bundle, e.g. "MyExtraBundle"
|
||||||
|
* @param string $shortBundleName Short name of bundle (without "Bundle")
|
||||||
|
*
|
||||||
|
* @throws IncorrectBundleNameException
|
||||||
|
* @dataProvider provideFullAndShortBundleName
|
||||||
|
*/
|
||||||
|
public function testGetShortBundleName($fullBundleName, $shortBundleName)
|
||||||
|
{
|
||||||
|
self::assertEquals($shortBundleName, Bundle::getShortBundleName($fullBundleName));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides empty path of the view / template and/or name of bundle
|
* Provides empty path of the view / template and/or name of bundle
|
||||||
*
|
*
|
||||||
@@ -137,21 +173,21 @@ class BundleTest extends BaseTestCase
|
|||||||
public function provideViewPathAndBundle()
|
public function provideViewPathAndBundle()
|
||||||
{
|
{
|
||||||
yield[
|
yield[
|
||||||
':User',
|
'User',
|
||||||
'MyExtraBundle',
|
'MyExtraBundle',
|
||||||
'MyExtraBundle::User.html.twig',
|
'@MyExtra/User.html.twig',
|
||||||
];
|
];
|
||||||
|
|
||||||
yield[
|
yield[
|
||||||
'User:Active',
|
'User:Active',
|
||||||
'MyExtraBundle',
|
'MyExtraBundle',
|
||||||
'MyExtraBundle:User:Active.html.twig',
|
'@MyExtra/User/Active.html.twig',
|
||||||
];
|
];
|
||||||
|
|
||||||
yield[
|
yield[
|
||||||
'User:Active',
|
'User:Active',
|
||||||
'MySuperExtraGorgeousBundle',
|
'MySuperExtraGorgeousBundle',
|
||||||
'MySuperExtraGorgeousBundle:User:Active.html.twig',
|
'@MySuperExtraGorgeous/User/Active.html.twig',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,7 +209,45 @@ class BundleTest extends BaseTestCase
|
|||||||
'User:Active',
|
'User:Active',
|
||||||
'MyExtraBundle',
|
'MyExtraBundle',
|
||||||
'js.twig',
|
'js.twig',
|
||||||
'MyExtraBundle:User:Active.js.twig',
|
'@MyExtra/User/Active.js.twig',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides incorrect name of bundle
|
||||||
|
*
|
||||||
|
* @return Generator
|
||||||
|
*/
|
||||||
|
public function provideIncorrectBundleName()
|
||||||
|
{
|
||||||
|
yield[
|
||||||
|
'myExtra',
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
'MyExtra',
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
'MySuperExtraGorgeous',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides full and short name of bundle
|
||||||
|
*
|
||||||
|
* @return Generator
|
||||||
|
*/
|
||||||
|
public function provideFullAndShortBundleName()
|
||||||
|
{
|
||||||
|
yield[
|
||||||
|
'MyExtraBundle',
|
||||||
|
'MyExtra',
|
||||||
|
];
|
||||||
|
|
||||||
|
yield[
|
||||||
|
'MySuperExtraGorgeousBundle',
|
||||||
|
'MySuperExtraGorgeous',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -294,6 +294,11 @@ class RegexTest extends BaseTestCase
|
|||||||
self::assertEquals($expected, Regex::isValidBundleName($bundleName));
|
self::assertEquals($expected, Regex::isValidBundleName($bundleName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetBundleNamePattern()
|
||||||
|
{
|
||||||
|
self::assertEquals('/^(([A-Z]{1}[a-z0-9]+)((?2))*)(Bundle)$/', Regex::getBundleNamePattern());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides name of bundle and information if it's valid name
|
* Provides name of bundle and information if it's valid name
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user