* // Returns the bundle path for the "admin" bundle
* $path = Bundle::path('admin');
*
* // Returns the APP_PATH constant as the default bundle
* $path = Bundle::path('application');
*
*
* @param string $bundle
* @return string
*/
public static function path($bundle)
{
return ($bundle != DEFAULT_BUNDLE) ? BUNDLE_PATH.strtolower($bundle).DS : APP_PATH;
}
/**
* Return the root asset path for the given bundle.
*
* @param string $bundle
* @return string
*/
public static function assets($bundle)
{
return ($bundle != DEFAULT_BUNDLE) ? PUBLIC_PATH."bundles/{$bundle}/" : PUBLIC_PATH;
}
/**
* Get the bundle name from a given identifier.
*
*
* // Returns "admin" as the bundle name for the identifier
* $bundle = Bundle::name('admin::home.index');
*
*
* @param string $identifier
* @return string
*/
public static function name($identifier)
{
list($bundle, $element) = static::parse($identifier);
return $bundle;
}
/**
* Get the element name from a given identifier.
*
*
* // Returns "home.index" as the element name for the identifier
* $bundle = Bundle::bundle('admin::home.index');
*
*
* @param string $identifier
* @return string
*/
public static function element($identifier)
{
list($bundle, $element) = static::parse($identifier);
return $element;
}
/**
* Reconstruct an identifier from a given bundle and element.
*
*
* // Returns "admin::home.index"
* $identifier = Bundle::identifier('admin', 'home.index');
*
* // Returns "home.index"
* $identifier = Bundle::identifier('application', 'home.index');
*
*
* @param string $bundle
* @param string $element
* @return string
*/
public static function identifier($bundle, $element)
{
return (is_null($bundle) or $bundle == DEFAULT_BUNDLE) ? $element : $bundle.'::'.$element;
}
/**
* Return the bundle name if it exists, else return the default bundle.
*
* @param string $bundle
* @return string
*/
public static function resolve($bundle)
{
return (static::exists($bundle)) ? $bundle : DEFAULT_BUNDLE;
}
/**
* Parse a element identifier and return the bundle name and element.
*
*
* // Returns array(null, 'admin.user')
* $element = Bundle::parse('admin.user');
*
* // Parses "admin::user" and returns array('admin', 'user')
* $element = Bundle::parse('admin::user');
*
*
* @param string $identifier
* @return array
*/
public static function parse($identifier)
{
// The parsed elements are cached so we don't have to reparse them on each
// subsequent request for the parsed element. So, if we've already parsed
// the given element, we'll just return the cached copy.
if (isset(static::$elements[$identifier]))
{
return static::$elements[$identifier];
}
if (strpos($identifier, '::') !== false)
{
$element = explode('::', strtolower($identifier));
}
// If no bundle is in the identifier, we will insert the default bundle
// since classes like Config and Lang organize their items by bundle.
// The "application" folder essentially behaves as a bundle.
else
{
$element = array(DEFAULT_BUNDLE, strtolower($identifier));
}
return static::$elements[$identifier] = $element;
}
/**
* Detect all of the existing bundles in the application.
*
* The names of the bundles are cached so this operation will be only be
* performed once and then the same array will be returned on each later
* request for the bundle names.
*
* @return array
*/
public static function all()
{
if (is_array(static::$bundles)) return static::$bundles;
$bundles = array();
foreach (array_filter(glob(BUNDLE_PATH.'*'), 'is_dir') as $bundle)
{
$bundles[] = basename($bundle);
}
return static::$bundles = $bundles;
}
}