neirda24/liipthemeprovider

Allow to create some custom providers for the theme list.

This package's canonical repository appears to be gone and the package has been frozen as a result.

Installs: 1 004

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 1

Open Issues: 0

Type:symfony-bundle

1.1.6 2015-12-11 14:35 UTC

This package is not auto-updated.

Last update: 2023-03-04 09:11:54 UTC


README

SensioLabsInsight Scrutinizer Code Quality Latest Stable Version Total Downloads Latest Unstable Version License Dependency Status

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require neirda24/liipthemeprovider "~1"

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Neirda\Bundle\LiipThemeProvider\LiipThemeProviderBundle(),
        );

        // ...
    }

    // ...
}

How to use it

Step 1: Create your provider class

<?php
// src/AppBundle/ThemeProvider

use Neirda\Bundle\LiipThemeProvider\ThemeProviderInterface;

// ...
class ThemeProvider implements ThemeProviderInterface
{
    /**
     * {@inheritdoc}
     */
    public function getThemeList()
    {
        // implement your logic here...
        // it should return an array event if it is empty
    }

    // ...
}

Step 2: Declare the service

Your service must be tagged with liip_theme_provider.theme_provider.

XML:

...

    <services>
        ...
        <service id="app.theme.provider" class="AppBundle\ThemeProvider\ThemeProvider">
            <tag name="liip_theme_provider.theme_provider" />
        </service>
        ...
    </services>
...

Done !!

Example

Create this class:

<?php
// src/AppBundle/ThemeProvider

use Neirda\Bundle\LiipThemeProvider\Theme\ThemeProviderInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

class ThemeProvider implements ThemeProviderInterface
{
    /**
     * @const THEMES_PATH
     */
    const THEMES_PATH = 'src/AppBundle/Resources/themes';

    /**
     * @var string
     */
    protected $rootDir;

    /**
     * @var null|array
     */
    protected $themeList = null;

    /**
     * Constructor.
     *
     * @param string $rootDir
     */
    public function __construct($rootDir)
    {
        $this->rootDir = $rootDir;
    }

    /**
     * {@inheritdoc}
     */
    public function getThemeList()
    {
        if (!is_array($this->themeList)) {
            $path   = rtrim($this->rootDir, '/') . '/' . ltrim(static::THEMES_PATH, '/');
            $themes = new Finder();
            $themes->directories()->depth('== 0')->in($path);
            $themeList = [];
            foreach ($themes as $theme) {
                /** @var SplFileInfo $theme */
                $themeList[] = $theme->getFilename();
            }

            $this->themeList = $themeList;
        }

        return $this->themeList;
    }
}

And declare your service:

xml

...
<services>
    <service id="app.theme.provider" class="AppBundle\Provider\ThemeProvider">
        <argument>%kernel.root_dir%/../</argument>
        <tag name="liip_theme_provider.theme_provider" />
    </service>
</services>
...

Now, all you have to do is just creating a new folder and it will be automatically added to the themes available.