/** * Create a new file in the template called $filename. A new empty file * is added to the file system, empty, and a File record created. The * file is also added to the manifest if it's a type where that's * required. * * On error, an exception is thrown, such as if the file type is not * supported. * * @param String $filename Name of file. Should not contain * slashes, the location will be determined * automatically. * @param Boolean $editable If true, the file must be an editable type. If false, * can be used to add images and non-editable files. * @param String $sourcePath If provided, this file is copied to create the new * file contents. * @returns File */ public function addNewFile($filename, $editable = true, $sourcePath = null) { if (strpos($filename, '/') !== FALSE) { throw new Exception('addNewFile expects a file with no path'); } $extension = DynamicTemplate::get_extension($filename); $subdir = $this->getSubdirByExtension($extension, $editable); // Create the physical target folder and the Folder objects in the DB $dir = BASE_PATH . "/" . $this->RelativePath . $subdir; @Filesystem::makeFolder($dir); $p = $this->RelativePath; if (substr($p, 0, 7) == "assets/") { $p = substr($p, 7); } $subFolder = Folder::findOrMake($p . $subdir); // If the file already exists in the template, we figure out a new name until we get a name that // doesn't exist $filename = $this->getUniqueName($filename, $dir); // if(file_exists("{$dir}/{$filename}")) throw new Exception("file $filename already exists in the template"); // Now create the physical file if ($sourcePath) { copy($sourcePath, "{$dir}/{$filename}"); } else { file_put_contents("{$dir}/{$filename}", ""); } $result = $this->constructChildInFolder(basename("{$dir}/{$filename}"), $subFolder); $this->addFileToManifest($filename); return $result; }