Exemple #1
0
 /**
  * @return mixed
  */
 public function isReadable()
 {
     if (!$this->_isReadable) {
         $this->_isReadable = IOHelper::isReadable($this->getRealPath());
     }
     return $this->_isReadable;
 }
 /**
  * Gets the source code of a template.
  *
  * @param  string $name The name of the template to load, or a StringTemplate object.
  *
  * @throws Exception
  * @return string The template source code.
  */
 public function getSource($name)
 {
     if (is_string($name)) {
         $template = $this->_findTemplate($name);
         if (IOHelper::isReadable($template)) {
             return IOHelper::getFileContents($template);
         } else {
             throw new Exception(Craft::t('Tried to read the template at {path}, but could not. Check the permissions.', array('path' => $template)));
         }
     } else {
         return $name->template;
     }
 }
 /**
  * @inheritDoc IZip::add()
  *
  * @param string $sourceZip
  * @param string $pathToAdd
  * @param string $basePath
  * @param null   $pathPrefix
  *
  * @return bool
  */
 public function add($sourceZip, $pathToAdd, $basePath, $pathPrefix = null)
 {
     $zip = new \ZipArchive();
     $zipContents = $zip->open($sourceZip);
     if ($zipContents !== true) {
         Craft::log('Unable to open zip file: ' . $sourceZip, LogLevel::Error);
         return false;
     }
     if (IOHelper::fileExists($pathToAdd)) {
         $folderContents = array($pathToAdd);
     } else {
         $folderContents = IOHelper::getFolderContents($pathToAdd, true);
     }
     foreach ($folderContents as $itemToZip) {
         if (IOHelper::isReadable($itemToZip)) {
             // Figure out the relative path we'll be adding to the zip.
             $relFilePath = mb_substr($itemToZip, mb_strlen($basePath));
             if ($pathPrefix) {
                 $pathPrefix = IOHelper::normalizePathSeparators($pathPrefix);
                 $relFilePath = $pathPrefix . $relFilePath;
             }
             if (IOHelper::folderExists($itemToZip)) {
                 if (IOHelper::isFolderEmpty($itemToZip)) {
                     $zip->addEmptyDir($relFilePath);
                 }
             } elseif (IOHelper::fileExists($itemToZip)) {
                 // We can't use $zip->addFile() here but it's a terrible, horrible, POS method that's buggy on Windows.
                 $fileContents = IOHelper::getFileContents($itemToZip);
                 if (!$zip->addFromString($relFilePath, $fileContents)) {
                     Craft::log('There was an error adding the file ' . $itemToZip . ' to the zip: ' . $itemToZip, LogLevel::Error);
                 }
             }
         }
     }
     $zip->close();
     return true;
 }
Exemple #4
0
 /**
  * Will add either a file or a folder to an existing zip file.  If it is a folder, it will add the contents recursively.
  *
  * @param string $sourceZip     The zip file to be added to.
  * @param string $pathToAdd     A file or a folder to add.  If it is a folder, it will recursively add the contents of the folder to the zip.
  * @param string $basePath      The root path of the file(s) to be added that will be removed before adding.
  * @param string $pathPrefix    A path to be prepended to each file before it is added to the zip.
  * @return bool
  */
 public function add($sourceZip, $pathToAdd, $basePath, $pathPrefix = null)
 {
     $zip = new \PclZip($sourceZip);
     if (IOHelper::fileExists($pathToAdd)) {
         $folderContents = array($pathToAdd);
     } else {
         $folderContents = IOHelper::getFolderContents($pathToAdd, true);
     }
     $filesToAdd = array();
     foreach ($folderContents as $itemToZip) {
         if (IOHelper::isReadable($itemToZip)) {
             if (IOHelper::folderExists($itemToZip) && IOHelper::isFolderEmpty($itemToZip) || IOHelper::fileExists($itemToZip)) {
                 $filesToAdd[] = $itemToZip;
             }
         }
     }
     if (!$pathPrefix) {
         $pathPrefix = '';
     }
     $result = $zip->add($filesToAdd, PCLZIP_OPT_ADD_PATH, $pathPrefix, PCLZIP_OPT_REMOVE_PATH, $basePath);
     if ($result == 0) {
         Craft::log('Unable to add to zip file: ' . $sourceZip, LogLevel::Error);
         return false;
     }
     return true;
 }
 /**
  * Gets migrations that have no been applied yet AND have a later timestamp than the current Craft release.
  *
  * @param $plugin
  *
  * @return array
  */
 public function getNewMigrations($plugin = null)
 {
     $migrations = array();
     $migrationPath = $this->getMigrationPath($plugin);
     if (IOHelper::folderExists($migrationPath) && IOHelper::isReadable($migrationPath)) {
         $applied = array();
         foreach ($this->getMigrationHistory($plugin) as $migration) {
             $applied[] = $migration['version'];
         }
         $handle = opendir($migrationPath);
         while (($file = readdir($handle)) !== false) {
             if ($file[0] === '.') {
                 continue;
             }
             $path = IOHelper::normalizePathSeparators($migrationPath . $file);
             $class = IOHelper::getFileName($path, false);
             // Have we already run this migration?
             if (in_array($class, $applied)) {
                 continue;
             }
             if (preg_match('/^m(\\d\\d)(\\d\\d)(\\d\\d)_(\\d\\d)(\\d\\d)(\\d\\d)_\\w+\\.php$/', $file, $matches)) {
                 $migrations[] = $class;
             }
         }
         closedir($handle);
         sort($migrations);
     }
     return $migrations;
 }
 /**
  * Gets migrations that have no been applied yet AND have a later timestamp than the current Craft release.
  *
  * @param $plugin
  *
  * @return array
  */
 public function getNewMigrations($plugin = null)
 {
     $migrations = array();
     $migrationPath = $this->getMigrationPath($plugin);
     if (IOHelper::folderExists($migrationPath) && IOHelper::isReadable($migrationPath)) {
         $applied = array();
         foreach ($this->getMigrationHistory($plugin) as $migration) {
             $applied[] = $migration['version'];
         }
         $handle = opendir($migrationPath);
         if ($plugin) {
             $pluginInfo = craft()->plugins->getPluginInfo($plugin);
             $storedDate = $pluginInfo['installDate']->getTimestamp();
         } else {
             $storedDate = Craft::getReleaseDate()->getTimestamp();
         }
         while (($file = readdir($handle)) !== false) {
             if ($file[0] === '.') {
                 continue;
             }
             $path = IOHelper::normalizePathSeparators($migrationPath . $file);
             $class = IOHelper::getFileName($path, false);
             // Have we already run this migration?
             if (in_array($class, $applied)) {
                 continue;
             }
             if (preg_match('/^m(\\d\\d)(\\d\\d)(\\d\\d)_(\\d\\d)(\\d\\d)(\\d\\d)_\\w+\\.php$/', $file, $matches)) {
                 // Check the migration timestamp against the Craft release date
                 $time = strtotime('20' . $matches[1] . '-' . $matches[2] . '-' . $matches[3] . ' ' . $matches[4] . ':' . $matches[5] . ':' . $matches[6]);
                 if ($time > $storedDate) {
                     $migrations[] = $class;
                 }
             }
         }
         closedir($handle);
         sort($migrations);
     }
     return $migrations;
 }