| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright 2017 Vladimir Jimenez | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace allejo\stakx\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use allejo\stakx\Exception\DependencyMissingException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use allejo\stakx\Document\DataItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use allejo\stakx\Exception\UnsupportedDataTypeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use allejo\stakx\Utilities\StrUtils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Class DataManager. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * This class handles everything in regards to DataItems and DataSets. This class supports reading the following data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * types: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *   - CSV | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  *   - JSON | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  *   - XML | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  *   - YAML | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class DataManager extends TrackingManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Get all of the DataItems and DataSets in this manager. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function &getDataItems() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return $this->trackedItems; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Loop through all of the DataItems specified in `$folders`. Each folder will have contain just DataItems. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string[] $folders An array of folders to be searched for to contain DataItems | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function parseDataItems($folders) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if ($folders === null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         foreach ($folders as $folder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $this->saveFolderDefinition($folder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $this->scanTrackableItems( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 $folder, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 array('/\.example$/') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Loop through all of the DataSets specified in `$dataSets`. Each DataSet contains a name and a folder location. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param string[] $dataSets An array of DataSets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function parseDataSets($dataSets) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if ($dataSets === null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |          * The information which each DataSet has from the configuration file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |          * $dataSet['name']   string The name of the collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |          *         ['folder'] string The folder where this collection has its ContentItems | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |          * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         foreach ($dataSets as $dataSet) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $this->saveFolderDefinition($dataSet['folder'], array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 'namespace' => $dataSet['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $this->scanTrackableItems( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 $dataSet['folder'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 array('namespace' => $dataSet['name']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 array('/\.example$/') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     protected function handleTrackableItem($filePath, $options = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         try | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $namespace = (isset($options['namespace'])) ? $options['namespace'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $dataItem = new DataItem($filePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $dataItem->setNamespace($namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $this->saveTrackerOptions($dataItem->getRelativeFilePath(), $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $this->addObjectToTracker($dataItem, $dataItem->getName(), $namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             return $dataItem->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         catch (DependencyMissingException $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             if ($e->getDependency() === 'XML') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 $this->output->critical('XML support is not available in your PHP installation. For XML support, please install the appropriate package for your system:'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 $this->output->critical('  e.g. php7.0-xml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         catch (UnsupportedDataTypeException $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $this->output->warning(StrUtils::interpolate('There is no function to handle {ext} file format.', array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 'ext' => $e->getDataType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             ))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         return $this->fs->getBaseName($filePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  |