Skip to content

Instantly share code, notes, and snippets.

@Herzult
Created May 23, 2011 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Herzult/986628 to your computer and use it in GitHub Desktop.
Save Herzult/986628 to your computer and use it in GitHub Desktop.
POC of a customizable file locator (ease the share of a Symfony project)
<?php
/**
* File locator that first look for a "filename.local.ext" when the name of the
* file to locate matches the "filename.customizable.ext" pattern, then it look
* for a "filename.ext" file.
*/
class CustomizableFileLocator extends FileLocator
{
/**
* {@inheritDoc}
*/
public function locate($name, $currentPath = null, $first = true)
{
if (preg_match('/^(?<basename>.*)\.customizable\.(?<extension>\w+)$/', $name, $matches)) {
try {
return parent::locate(sprintf('%s.custom.%s', $matches['basename'], $matches['extension']), $currentPath, $first);
} catch (\InvalidArgumentException $e) {
return parent::locate(sprintf('%s.%s', $matches['basename'], $matches['extension']), $currentPath, $first);
}
}
return parent::locate($name, $currentPath, $first);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment