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

Allow extending FileLoader implementation #219

Closed
daun opened this issue Mar 17, 2020 · 2 comments
Closed

Allow extending FileLoader implementation #219

daun opened this issue Mar 17, 2020 · 2 comments

Comments

@daun
Copy link

@daun daun commented Mar 17, 2020

Would it be possible to set the $baseDir property of the FileLoader class to protected instead of private? (see FileLoader.php@L23)

I'm trying to implement a custom file loader that allows prepending an alias to the root directory when including (e.g. {include '~partials/image.latte'} would render /path/to/views/dir/partials/image.latte).

The only change needed is in the getReferredName method, so I'd like to extend the base class and avoid re-implementing the whole thing. However, from my child class, I can't access the base dir property since it's private. See code below.

Let me know if there's a simpler method to accomplish this or any good reason to keep it as private.

Ideally, this would be part of the framework, but I'm okay with creating a custom loader for that and understand it might not make sense for every app.

Thanks.

BTW. If relevant, I'm not using Nette framework, this is on top of a third-party CMS.

This is what the custom loader would look like:

<?php namespace App;

use Latte\Loaders\FileLoader;

/**
 * Custom template file loader.
 *
 * Modified to allow including files from basedir directly by prepending ~ or /
 * Example:
 *   /partials/image.latte >> /path/to/view/dir/partials/image.latte
 *   ~partials/image.latte >> /path/to/view/dir/partials/image.latte
 */
class CustomFileLoader extends FileLoader
{
	/**
	 * Aliases that refer to the base dir when prepended to a filename
	 *
	 * @var array
	 */
	static protected $baseDirAliases = [
		'/',
		'~',
	];

	/**
	 * Returns referred template name.
	 *
	 * Check for prepended alias to baseDir and return path from there if found
	 * Otherwise, refer to default implementation
	 *
	 */
	public function getReferredName($file, $referringFile): string
	{
		if (
			$this->baseDir &&
			in_array(substr($file, 0, 1), static::$baseDirAliases)
		) {
			return $this->normalizePath(substr($file, 1));
		}
		return parent::getReferredName($file, $referringFile);
	}
}
@daun
Copy link
Author

@daun daun commented Mar 17, 2020

This would apply to FileLoader::normalizePath() as well.

@dg dg closed this as completed in 6904b31 Apr 1, 2020
@dg
Copy link
Member

@dg dg commented Apr 1, 2020

I changed it from private to protected.

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

No branches or pull requests

2 participants