Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add View::exists() and View::getRegisteredEngines() #1707

Closed
aleemb opened this issue Dec 16, 2013 · 5 comments
Closed

Add View::exists() and View::getRegisteredEngines() #1707

aleemb opened this issue Dec 16, 2013 · 5 comments

Comments

@aleemb
Copy link

aleemb commented Dec 16, 2013

I have been extending the Phalcon framework lately with my own bits and pieces and ran into a minor niggle with the View class. I have added my own cascading behavior to views so that if a view doesn't exist, then I look for a view one folder higher (and some other stuff).

There is no function View::exists so I have had to define it as:

public function exists($view)
{
    $viewDir = $this->getViewsDir();

    foreach ($this->getRegisteredEngines() as $ext => $class)
    {
        if (file_exists($viewDir . $view . $ext)) {
            return true;
        }
    }

    return false;
}

This of course depends on a function called getRegisteredEngines() defined as:

public function getRegisteredEngines()
{
    return $this->_enginesInfo;
}

which in turn depends on $this->_enginesInfo populated by overriding the registerEngines method:

public function registerEngines($engines)
{
    $enginesInfo = [];

    foreach($engines as $extension => $engine)
    {
        $enginesInfo[$extension] = $engine;
    }

    $this->_enginesInfo = $enginesInfo;

    return call_user_func(array($this, 'parent::registerEngines'), $engines);
}

I think it would be beneficial to people who wish to extend the core framework with custom functionality since the current View implementation is a leaky abstraction (no way to enumerate a list of registered engines for example).

@nexik
Copy link

nexik commented Dec 17, 2013

+1

getRegisteredEngines() would be also helpful when one can unit test View configuration setted in DI. Without it I can't check if view has Volt engine configured in Dependency Injection.

@ghost
Copy link

ghost commented Dec 19, 2013

getRegisteredEngines() can be simplified to

public function getRegisteredEngines()
{
    return $this->_registeredEngines;
}

and no need to override registerEngines().

@ghost
Copy link

ghost commented Dec 20, 2013

Implemented in 1.3.0, could you please take a look?

@aleemb
Copy link
Author

aleemb commented Dec 23, 2013

@sjinks, thanks for the quick turn around, this works great.

@aleemb aleemb closed this as completed Dec 23, 2013
@iby
Copy link
Contributor

iby commented Jan 31, 2014

@sjinks, makes sense adding this to the ViewInterface as well. What do you think?
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants