luya\helpers\FileHelper::getFileInfo PHP Method

getFileInfo() public static method

Get extension and name from a file for the provided source/path of the file.
public static getFileInfo ( string $sourceFile ) : object
$sourceFile string The path of the file
return object With extension and name keys.
    public static function getFileInfo($sourceFile)
    {
        $path = pathinfo($sourceFile);
        return (object) ['extension' => isset($path['extension']) ? empty($path['extension']) ? false : $path['extension'] : false, 'name' => isset($path['filename']) ? $path['filename'] : false];
    }

Usage Example

Ejemplo n.º 1
0
 public function run()
 {
     $cmslayouts = Yii::getAlias('@app/views/cmslayouts');
     $layoutFiles = [];
     if (file_exists($cmslayouts)) {
         $files = FileHelper::findFiles($cmslayouts, ['recursive' => false, 'filter' => function ($path) {
             return !in_array(substr(basename($path), 0, 1), $this->ignorePrefix);
         }]);
         foreach ($files as $file) {
             $fileinfo = FileHelper::getFileInfo($file);
             $fileBaseName = $fileinfo->name . '.' . $fileinfo->extension;
             $readableFileName = $this->generateReadableName($fileinfo->name);
             $oldTwigName = $fileinfo->name . '.twig';
             if ($fileinfo->extension !== 'php') {
                 throw new Exception("layout file '{$file}': Since 1.0.0-beta6, cms layouts must be a php file with '<?= \$placeholders['content']; ?>' instead of a twig '{{placeholders.content}}'");
             }
             $layoutFiles[] = $fileBaseName;
             $layoutFiles[] = $oldTwigName;
             $content = file_get_contents($file);
             preg_match_all("/placeholders\\[[\\'\"](.*?)[\\'\"]\\]/", $content, $results);
             $_placeholders = [];
             foreach (array_unique($results[1]) as $holderName) {
                 if (!$this->verifyVariable($holderName)) {
                     throw new Exception("Wrong variable name detected '" . $holderName . "'. Special chars are not allowed in placeholder variables, allowed chars are a-zA-Z0-9");
                 }
                 $_placeholders[] = ['label' => $this->generateReadableName($holderName), 'var' => $holderName];
             }
             $_placeholders = ['placeholders' => $_placeholders];
             $layoutItem = Layout::find()->where(['or', ['view_file' => $fileBaseName], ['view_file' => $oldTwigName]])->one();
             if ($layoutItem) {
                 $match = $this->comparePlaceholders($_placeholders, json_decode($layoutItem->json_config, true));
                 if ($match) {
                     $layoutItem->updateAttributes(['name' => $readableFileName, 'view_file' => $fileBaseName]);
                 } else {
                     $layoutItem->updateAttributes(['name' => $readableFileName, 'view_file' => $fileBaseName, 'json_config' => json_encode($_placeholders)]);
                     $this->addLog('existing cmslayout ' . $readableFileName . ' updated');
                 }
             } else {
                 // add item into the database table
                 $data = new Layout();
                 $data->scenario = 'restcreate';
                 $data->setAttributes(['name' => $readableFileName, 'view_file' => $fileBaseName, 'json_config' => json_encode($_placeholders)]);
                 $data->save(false);
                 $this->addLog('new cmslayout ' . $readableFileName . ' found and added to database.');
             }
         }
         foreach (Layout::find()->where(['not in', 'view_file', $layoutFiles])->all() as $layoutItem) {
             $layoutItem->delete();
         }
     }
 }
All Usage Examples Of luya\helpers\FileHelper::getFileInfo