]> git.mxchange.org Git - friendica.git/blob - library/dddbl2/dddbl.php
Merge pull request #3678 from tobiasd/20170904-langdet
[friendica.git] / library / dddbl2 / dddbl.php
1 <?php
2
3 namespace DDDBL;
4
5 require_once __DIR__ . '/config.inc.php';
6
7 /**
8   * @throws \Exception                       - if no parameter are given
9   * @throws UnexpectedParameterTypeException - if first parameter is not a string
10   *
11   * @returns (mixed) - the result of the query-definition execution
12   *
13   * expect a list of parameter with at least one value. the
14   * list is handled over to the queue, which will executed
15   * with them
16   *
17   * in the end a result of the execution of the query-definition
18   * through the stored handler is returned
19   *
20   **/
21 function get() {
22
23   $arrParameter = func_get_args();
24   
25   if(empty($arrParameter))
26     throw new \Exception ("no parameter given for execution");
27   
28   if(!is_string($arrParameter[0]))
29     throw new UnexpectedParameterTypeException('string', $arrParameter[0]);
30
31   # get instance of queue and work with a copy of it
32   $objQueue = Singleton::getInstance('\DDDBL\Queue');
33   $objQueue = $objQueue->getClone();
34   
35   return $objQueue->execute($arrParameter);
36
37 }
38
39 /**
40   * @param $strFile - the file with the query definitions to store
41   *
42   * store all query-definitions from the given file
43   *
44   **/
45 function storeQueryFileContent($strFile) {
46
47   storeDefinitionsFromFileInGroup($strFile, 'Query-Definition');
48
49 }
50
51 /**
52   * @param $strDir   - the dir with query-definitions files
53   * @param $strMatch - a rule files in the dir have to match
54   *
55   * iterate through all files in the given dir. if a file matches
56   * the rule in $strMatch, the definitions in the file will be stored
57   * as query-definitions. all files are match in default.
58   *
59   **/
60 function loadQueryDefinitionsInDir($strDir, $strMatch = '*') {
61
62   walkDirForCallback($strDir, '\DDDBL\storeQueryFileContent', $strMatch);
63
64 }
65
66 /**
67   * @param $strFile - the file with the database definitions to store
68   *
69   * store all database definition from the given file
70   *
71   **/
72 function storeDBFileContent($strFile) {
73
74   $cloAdditionalHandler = function ($objDataObjectPool, $arrDefinition) {
75     if(!empty($arrDefinition['DEFAULT'])  && true == (boolean) $arrDefinition['DEFAULT'])
76       $objDataObjectPool->add('DEFAULT', $arrDefinition);
77   };
78
79   storeDefinitionsFromFileInGroup($strFile, 'Database-Definition', $cloAdditionalHandler);
80
81 }
82
83 /**
84   * @param $strDir   - the dir with query-definitions files
85   * @param $strMatch - a rule files in the dir have to match
86   *
87   * iterate through all files in the given dir. if a file matches
88   * the rule in $strMatch, the definitions in the file will be stored
89   * as database-definitions. all files are matched in default.
90   *
91   **/
92 function loadDBDefinitionsInDir($strDir, $strMatch = '*') {
93
94   walkDirForCallback($strDir, '\DDDBL\loadDBDefinitionsInDir', $strMatch);
95
96 }
97
98 /**
99   * @param $strPath          - the path to the dir to handle
100   * @param $strCallback      - the callback to call when a matching file is found
101   * @param $strFilenameMatch - the rule to filename has to match
102   *
103   * @throws UnexpectedParameterTypeException - if the given path or filematch-rule is not a string
104   * @throws UnexpectedParameterTypeException - if the given callback is not a callable
105   * @throws \Exception - if given path is not a directory
106   * @throws \Exception - if the directory is not readable
107   *
108   * reads all files of the given directory (just directory, not recursive)
109   * and checks, if the filename matches against the given rule. if
110   * a match is found the given callback is called with the full
111   * path to the file
112   *
113   **/
114 function walkDirForCallback($strPath, $strCallback, $strFilenameMatch) {
115
116   if(!is_string($strPath))
117     throw new UnexpectedParameterTypeException('string', $strPath);
118   
119   if(!is_callable($strCallback))
120     throw new UnexpectedParameterTypeException('callable', $strCallback);
121     
122   if(!is_string($strFilenameMatch))
123     throw new UnexpectedParameterTypeException('string', $strFilenameMatch);
124   
125   if(!is_dir($strPath))
126     throw new \Exception ('given path is not an directory: ' . $strPath);
127   
128   $resDirHandle = opendir($strPath);
129   
130   if(!is_resource($resDirHandle))
131     throw new \Exception ('could not read directory: ' . $strPath);
132   
133   while($strFile = readdir($resDirHandle))
134     if(is_file($strPath.$strFile) && fnmatch($strFilenameMatch, $strFile)) 
135       call_user_func_array($strCallback, array($strPath.$strFile));
136   
137   closedir($resDirHandle);
138   
139 }
140
141 /**
142   * @param $strFile  - the file with definitions
143   * @param $strGroup - the group the definitions should be stored in
144   *
145   * @throws UnexpectedParameterTypeException - if the given file is not a string
146   * @throws UnexpectedParameterTypeException - if the given optional handler is not a callable
147   * @throws \Exception - if the given file is not a file or do not exists
148   * @throws \Exception - if the given file is not readable
149   *
150   * generic function to store all definitions in a given file
151   * in the specified group.
152   *
153   * if an additional handler is given, it is called AFTER the storage of the
154   * definition. when called it will get the reference to the DataObjectPool and the 
155   * found definition as parameter.
156   *
157   **/
158 function storeDefinitionsFromFileInGroup($strFile, $strGroup, $cloAdditionalHandler = null) {
159
160   if(!is_string($strGroup))
161     throw new UnexpectedParameterTypeException('string', $strGroup);
162     
163   if(!is_null($cloAdditionalHandler) && !is_callable($cloAdditionalHandler))
164     throw new UnexpectedParameterTypeException('callable', $cloAdditionalHandler);
165
166   if(!is_file($strFile) || !file_exists($strFile))
167     throw new \Exception ("given file is not a file or doesn't exists: $strFile");
168
169   if(!is_readable($strFile))
170     throw new \Exception ("given file is not readable: $strFile");
171
172   $arrDefinitions = parse_ini_file($strFile, true);
173   
174   $objDataObjectPool = new DataObjectPool($strGroup);
175   
176   foreach($arrDefinitions AS $strDefinitionAlias => $arrDefinition) {
177     $objDataObjectPool->add($strDefinitionAlias, $arrDefinition);
178     
179     if(!is_null($cloAdditionalHandler))
180       $cloAdditionalHandler($objDataObjectPool, $arrDefinition);
181
182   }
183
184 }