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 |
리턴 | object | With extension and name keys. |
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(); } } }
public function testGetFileInfo() { $test = FileHelper::getFileInfo('/path/to/myfile.png'); $this->assertSame('png', $test->extension); $this->assertSame('myfile', $test->name); $test = FileHelper::getFileInfo('/path/to/myfile.'); $this->assertSame(false, $test->extension); $this->assertSame('myfile', $test->name); $test = FileHelper::getFileInfo('/path/to/myfile'); $this->assertSame(false, $test->extension); $this->assertSame('myfile', $test->name); $test = FileHelper::getFileInfo('/path/to/'); $this->assertSame(false, $test->extension); $this->assertSame('to', $test->name); }
/** * @todo its a copy from the old colde, refactor code * @param string $fileSource * @param string $fileName * @param int $folderId */ public function addFile($fileSource, $fileName, $folderId = 0, $isHidden = false) { if (empty($fileSource) || empty($fileName)) { throw new Exception("Unable to create file where file source and/or file name is empty."); } $fileInfo = FileHelper::getFileInfo($fileName); $baseName = Inflector::slug($fileInfo->name, '-'); $fileHashName = Storage::createFileHash($fileName); $fileHash = FileHelper::getFileHash($fileSource); $mimeType = FileHelper::getMimeType($fileSource); $newName = implode([$baseName . '_' . $fileHashName, $fileInfo->extension], '.'); $savePath = $this->serverPath . '/' . $newName; if (is_uploaded_file($fileSource)) { if (!@move_uploaded_file($fileSource, $savePath)) { throw new Exception("error while moving uploaded file from {$fileSource} to {$savePath}"); } } else { if (!@copy($fileSource, $savePath)) { throw new Exception("error while copy file from {$fileSource} to {$savePath}."); } } $model = new StorageFile(); $model->setAttributes(['name_original' => $fileName, 'name_new' => $baseName, 'name_new_compound' => $newName, 'mime_type' => $mimeType, 'extension' => strtolower($fileInfo->extension), 'folder_id' => (int) $folderId, 'hash_file' => $fileHash, 'hash_name' => $fileHashName, 'is_hidden' => $isHidden ? 1 : 0, 'file_size' => @filesize($savePath)]); if ($model->validate()) { if ($model->save()) { $this->deleteHasCache($this->fileCacheKey); $this->_filesArray[$model->id] = $model->toArray(); return $this->getFile($model->id); } } return false; }