CRUDlex\ServiceProvider

class ServiceProvider

The ServiceProvider setups and initializes the whole CRUD system. After adding it to your Silex-setup, it offers access to {@see AbstractData} instances, one for each defined entity off the CRUD YAML file.

property datas

protected

Holds the {@see AbstractData} instances.

property manageI18n

protected

Holds whether we manage the i18n.

formatTime($value, $timezone, $pattern)

Formats the given time value to a timestring defined by the $pattern parameter.

If the value is false (like null), an empty string is returned. Else, the value is tried to be parsed as datetime via the given pattern. If that fails, it is tried to be parsed with the pattern ‘Y-m-d H:i:s’. If that fails, the value is returned unchanged. Else, it is returned formatted with the given pattern. The effect is to shorten ‘Y-m-d H:i:s’ to ‘Y-m-d’ for example.

Parameters:
  • $value (string) – the value to be formatted
  • $timezone (string) – the timezone of the value
  • $pattern (string) – the pattern with which the value is parsed and formatted
Returns:

string the formatted value

readYaml($fileName)

Reads and returns the contents of the given Yaml file. If it goes wrong, it throws an exception.

Parameters:
  • $fileName (string) – the file to read
Returns:

array the file contents

initMissingServiceProviders(Application $app)

Initializes needed but yet missing service providers.

Parameters:
  • $app (Application) – the application container
initLocales(Application $app)

Initializes the available locales.

Parameters:
  • $app (Application) – the application container
Returns:

array the available locales

initChildren()

Initializes the children of the data entries.

getLocaleLabels($locales, $crud)

Gets a map with localized entity labels from the CRUD YML.

Parameters:
  • $locales (array) – the available locales
  • $crud (array) – the CRUD entity map
Returns:

array the map with localized entity labels

configureDefinition(EntityDefinition $definition, $crud)

Configures the EntityDefinition according to the given CRUD entity map.

Parameters:
  • $definition (EntityDefinition) – the definition to configure
  • $crud (array) – the CRUD entity map
createDefinition(Application $app, $locales, $crud, $name)

Creates and setups an EntityDefinition instance.

Parameters:
  • $app (Application) – the application container
  • $locales (array) – the available locales
  • $crud (array) – the parsed YAML of a CRUD entity
  • $name (string) – the name of the entity
Returns:

EntityDefinition the EntityDefinition good to go

init(DataFactoryInterface $dataFactory, $crudFile, FileProcessorInterface $fileProcessor, $manageI18n, Application $app)

Initializes the instance.

Parameters:
  • $dataFactory (DataFactoryInterface) – the factory to create the concrete AbstractData instances
  • $crudFile (string) – the CRUD YAML file to parse
  • $fileProcessor (FileProcessorInterface) – the file processor used for file fields
  • $manageI18n (boolean) – holds whether we manage the i18n
  • $app (Application) – the application container
register(Application $app)

Implements ServiceProviderInterface::register() registering $app[‘crud’]. $app[‘crud’] contains an instance of the ServiceProvider afterwards.

Parameters:
  • $app (Application) – the Application instance of the Silex application
boot(Application $app)

Implements ServiceProviderInterface::boot().

Parameters:
  • $app (Application) – the Application instance of the Silex application
getData($name)

Getter for the {@see AbstractData} instances.

Parameters:
  • $name (string) – the entity name of the desired Data instance
Returns:

AbstractData the AbstractData instance or null on invalid name

getEntities()

Getter for all available entity names.

Returns:string[] a list of all available entity names
formatDate($value, $isUTC)

Formats the given value to a date of the format ‘Y-m-d’.

Parameters:
  • $value (string) – the value, might be of the format ‘Y-m-d H:i’ or ‘Y-m-d’
  • $isUTC (boolean) – whether the given value is in UTC
Returns:

string the formatted result or an empty string on null value

formatDateTime($value, $isUTC)

Formats the given value to a date of the format ‘Y-m-d H:i’.

Parameters:
  • $value (string) – the value, might be of the format ‘Y-m-d H:i’
  • $isUTC (boolean) – whether the given value is in UTC
Returns:

string the formatted result or an empty string on null value

basename($value)

Calls PHPs {@link http://php.net/manual/en/function.basename.php basename} and returns it’s result.

Parameters:
  • $value (string) – the value to be handed to basename
Returns:

string the result of basename

getTemplate(Application $app, $section, $action, $entity)

Determines the Twig template to use for the given parameters depending on the existance of certain keys in the Application $app in this order:

crud.$section.$action.$entity crud.$section.$action crud.$section

If nothing exists, this string is returned: “@crud/<action>.twig”

Parameters:
  • $app (Application) – the Silex application
  • $section (string) – the section of the template, either “layout” or “template”
  • $action (string) – the current calling action like “create” or “show”
  • $entity (string) – the current calling entity
Returns:

string the best fitting template

isManagingI18n()

Gets whether CRUDlex manages the i18n system.

Returns:boolean true if CRUDlex manages the i18n system
setLocale($locale)

Sets the locale to be used.

Parameters:
  • $locale (string) – the locale to be used.
getLocales()

Gets the available locales.

Returns:array the available locales
getLanguageName($language)

Gets a language name in the given language.

Parameters:
  • $language (string) – the language code of the desired language name
Returns:

string the language name in the given language or null if not available

formatFloat($float)

Formats a float to not display in scientific notation.

Parameters:
  • $float (float) – the float to format
Returns:

double|string the formated float