| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /****************************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) 2016 Richard Klees <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * This software is licensed under The MIT License. You should have received  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * a copy of the license along with the code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Lechimp\Dicto\App; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Lechimp\Dicto\App\RuleLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Lechimp\Dicto\Definition\RuleParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Lechimp\Dicto\Rules\Ruleset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Lechimp\Dicto\Rules as R; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Lechimp\Dicto\Variables as V; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Pimple\Container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use PhpParser\ParserFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * The App to be run from a script. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class App { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 4 |  |     public function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 4 |  |         ini_set('xdebug.max_nesting_level', 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Run the app. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param   array   $params     from commandline | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return  null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 2 |  |     public function run(array $params) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |         if (count($params) < 3) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             throw new \RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 "Expected path to rule-file as first parameter and path to config as second parameter."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         // drop program name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |         array_shift($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         $rules_file = array_shift($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         // the rest of the params are paths to configs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         $configs = $this->load_configs($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |         $dic = $this->create_dic($rules_file, $configs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |         $dic["engine"]->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Load extra configs from yaml files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param   array   $config_file_paths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return  array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 3 |  |     protected function load_configs(array $config_file_paths) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 3 |  |         $configs_array = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 3 |  |         foreach ($config_file_paths as $config_file) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 3 |  |             if (!file_exists($config_file)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                 throw new \RuntimeException("Unknown config-file '$config_file'"); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 3 |  |             $configs_array[] = Yaml::parse(file_get_contents($config_file)); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 3 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 3 |  |         return $configs_array; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Create and initialize the DI-container. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * TODO: move rule_file_path to config? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param   string      $rule_file_path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param   array       &$configs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return  Container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  |     protected function create_dic($rule_file_path, array &$configs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 3 |  |         array('is_string($rule_file_path)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  |         $container = new Container(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $container["config"] = function () use (&$configs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 2 |  |             return new Config($configs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $container["ruleset"] = function($c) use (&$rule_file_path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |             if (!file_exists($rule_file_path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 throw new \RuntimeException("Unknown rule-file '$rule_file_path'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |             $ruleset = $c["rule_loader"]->load_rules_from($rule_file_path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |             return $ruleset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $container["rule_loader"] = function($c) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |             return new RuleLoader($c["rule_parser"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $container["rule_parser"] = function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             // TODO: Move this stuff to the config. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             return new RuleParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |                 ( array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |                     ( new V\Classes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |                     , new V\Functions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |                     , new V\Globals() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |                     , new V\Files() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |                     , new V\Methods() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |                     , new V\LanguageConstruct("ErrorSuppressor", "@") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                     // TODO: Add some language constructs here... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |                 , array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |                     ( new R\ContainText() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |                     , new R\DependOn() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |                     , new R\Invoke() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |                 , array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |                     ( new V\Name() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $container["engine"] = function($c) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             return new Engine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |                 ( $c["log"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |                 , $c["config"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |                 , $c["database_factory"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |                 , $c["indexer_factory"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |                 , $c["analyzer_factory"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |                 , $c["source_status"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $container["log"] = function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 2 |  |             return new CLILogger(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $container["database_factory"] = function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 1 |  |             return new DBFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $container["indexer_factory"] = function($c) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             return new \Lechimp\Dicto\Indexer\IndexerFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 2 |  |                 ( $c["log"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 2 |  |                 , $c["php_parser"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 2 |  |                 , array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 2 |  |                     ( new \Lechimp\Dicto\Rules\ContainText() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 2 |  |                     , new \Lechimp\Dicto\Rules\DependOn() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 2 |  |                     , new \Lechimp\Dicto\Rules\Invoke() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 2 |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 2 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $container["analyzer_factory"] = function($c) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             return new \Lechimp\Dicto\Analysis\AnalyzerFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 1 |  |                 ( $c["log"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 1 |  |                 , $c["ruleset"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 1 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $container["php_parser"] = function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 2 |  |             return (new ParserFactory)->create(ParserFactory::PREFER_PHP7); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $container["report_generator"] = function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             return new CLIReportGenerator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 2 |  |         $container["source_status"] = function($c) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 2 |  |             return new SourceStatusGit($c["config"]->project_root()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 3 |  |         return $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 177 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 178 |  |  |  |