1: <?php
2: /**
3: * Luxor_Lang:: defines an API for the different programming languages Luxor
4: * is able to parse.
5: *
6: * @author Jan Schneider <jan@horde.org>
7: * @since Luxor 0.1
8: * @package Luxor
9: */
10: class Luxor_Lang
11: {
12: /**
13: * Attempts to return a concrete Luxor_Lang instance based on $driver.
14: *
15: * @param string $driver The type of concrete Luxor_Lang subclass
16: * to return. The is based on the repository
17: * driver ($driver). The code is dynamically
18: * included.
19: * @param array $params (optional) A hash containing any additional
20: * configuration or connection parameters a
21: * subclass might need.
22: *
23: * @return mixed The newly created concrete Luxor_Lang instance, or
24: * false on an error.
25: */
26: function factory($driver, $params = array())
27: {
28: $driver = basename($driver);
29: $class = 'Luxor_Lang_' . $driver;
30: if (class_exists($class)) {
31: return new $class($params);
32: } else {
33: return false;
34: }
35: }
36:
37: /**
38: * Attempts to determine a files programming language and returns
39: * a parser instance for this language.
40: *
41: * @param Luxor_Files $files An instance of Luxor_Files to use for file
42: * operations and path name resolution.
43: * @param string $pathname The path name of the file to create a
44: * parser for.
45: *
46: * @return mixed The created concrete Luxor_Lang instance, or false
47: * on error.
48: */
49: function builder($files, $pathname)
50: {
51: global $languages;
52: $languages = Horde::loadConfiguration('languages.php', 'languages', 'luxor');
53:
54: /* First, check the 'filetype' hash for a matching file extension. */
55: foreach ($languages['filetype'] as $type) {
56: if (preg_match('/' . $type[1] . '/', $pathname)) {
57: return Luxor_Lang::factory($type[2], $type);
58: }
59: }
60:
61: /* Next, try to detect the shebang line. */
62: $fh = $files->getFileHandle($pathname);
63: if (!$fh || is_a($fh, 'PEAR_Error')) {
64: return $fh;
65: }
66: $line = fgets($fh);
67: if (!preg_match('/^\#!\s*(\S+)/s', $line, $match)) {
68: return false;
69: }
70: if (isset($languages['interpreters'][$match[1]])) {
71: $lang = $languages['filetype'][$languages['interpreters'][$match[1]]];
72: return Luxor_Lang::factory($lang[2], $lang);
73: }
74:
75: return false;
76: }
77:
78: function processInclude($frag, $dir)
79: {
80: return preg_replace(array('/([\'"])(.*?)([\'"])/e',
81: '/(\\0<)(.*?)(\\0>)/e'),
82: array('stripslashes(\'$1\') . Luxor::incRef(\'$2\', "fixed", \'$2\', $dir) . stripslashes(\'$3\')',
83: 'stripslashes(\'$1\') . Luxor::incRef(\'$2\', "fixed", \'$2\') . stripslashes(\'$3\')'),
84: $frag);
85: }
86: }
87: