Created
May 23, 2011 12:38
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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