/** * @throws BuildException */ private function checkPreconditions() { if (is_null($this->destinationFile)) { throw new BuildException("destfile attribute must be set!", $this->getLocation()); } if ($this->destinationFile->exists() && $this->destinationFile->isDirectory()) { throw new BuildException("destfile is a directory!", $this->getLocation()); } if (!$this->destinationFile->canWrite()) { throw new BuildException("Can not write to the specified destfile!", $this->getLocation()); } if (!is_null($this->baseDirectory)) { if (!$this->baseDirectory->exists()) { throw new BuildException("basedir '" . (string) $this->baseDirectory . "' does not exist!", $this->getLocation()); } } if ($this->signatureAlgorithm == Phar::OPENSSL) { if (!extension_loaded('openssl')) { throw new BuildException("PHP OpenSSL extension is required for OpenSSL signing of Phars!", $this->getLocation()); } if (is_null($this->key)) { throw new BuildException("key attribute must be set for OpenSSL signing!", $this->getLocation()); } if (!$this->key->exists()) { throw new BuildException("key '" . (string) $this->key . "' does not exist!", $this->getLocation()); } if (!$this->key->canRead()) { throw new BuildException("key '" . (string) $this->key . "' cannot be read!", $this->getLocation()); } } }
/** * {@inheritDoc} */ public function parseFile(PhingFile $file) { if (!$file->canRead()) { throw new IOException("Unable to read file: " . $file); } try { // We load the Yaml class without the use of namespaces to prevent // parse errors in PHP 5.2. $parserClass = '\\Symfony\\Component\\Yaml\\Parser'; $parser = new $parserClass(); // Cast properties to array in case parse() returns null. $properties = (array) $parser->parse(file_get_contents($file->getAbsolutePath())); } catch (Exception $e) { if (is_a($e, '\\Symfony\\Component\\Yaml\\Exception\\ParseException')) { throw new IOException("Unable to parse contents of " . $file . ": " . $e->getMessage()); } throw $e; } $flattenedProperties = $this->flattenArray($properties); foreach ($flattenedProperties as $key => $flattenedProperty) { if (is_array($flattenedProperty)) { $flattenedProperties[$key] = implode(',', $flattenedProperty); } } return $flattenedProperties; }
public function checkFileExists() { //Get the correct path to asset switch ($this->type) { case Asset::ASSET_TYPE_CSS: $this->assetFolder = $this->paths['css']; break; case Asset::ASSET_TYPE_JS: $this->assetFolder = $this->paths['js']; break; case Asset::ASSET_TYPE_IMAGE: $this->assetFolder = $this->paths['images']; break; default: $folder = ''; } //Path to file $file = new PhingFile($this->assetsDir . '/' . $this->assetFolder . $this->file); //Check file exists if (!$file->exists()) { throw new BuildException("Unable to find asset file: " . $file->getAbsolutePath()); } //Check we can read it if (!$file->canRead()) { throw IOException("Unable to read asset file: " . $file->getPath()); } return $file; }
/** * Load properties from a file. * * @param PhingFile $file * @return void * @throws IOException - if unable to read file. */ function load(PhingFile $file) { if ($file->canRead()) { $this->parse($file->getPath(), false); } else { throw new IOException("Can not read file " . $file->getPath()); } }
/** * Run the task. * * @throws BuildException trouble, probably file IO */ public function main() { if ($this->prefix != null && $this->regex != null) { throw new BuildException("Please specify either prefix or regex, but not both", $this->getLocation()); } //copy the properties file $allProps = array(); /* load properties from file if specified, otherwise use Phing's properties */ if ($this->inFile == null) { // add phing properties $allProps = $this->getProject()->getProperties(); } elseif ($this->inFile != null) { if ($this->inFile->exists() && $this->inFile->isDirectory()) { $message = "srcfile is a directory!"; $this->failOnErrorAction(null, $message, Project::MSG_ERR); return; } if ($this->inFile->exists() && !$this->inFile->canRead()) { $message = "Can not read from the specified srcfile!"; $this->failOnErrorAction(null, $message, Project::MSG_ERR); return; } try { $props = new Properties(); $props->load(new PhingFile($this->inFile)); $allProps = $props->getProperties(); } catch (IOException $ioe) { $message = "Could not read file " . $this->inFile->getAbsolutePath(); $this->failOnErrorAction($ioe, $message, Project::MSG_WARN); return; } } $os = null; try { if ($this->destfile == null) { $os = Phing::getOutputStream(); $this->saveProperties($allProps, $os); $this->log($os, Project::MSG_INFO); } else { if ($this->destfile->exists() && $this->destfile->isDirectory()) { $message = "destfile is a directory!"; $this->failOnErrorAction(null, $message, Project::MSG_ERR); return; } if ($this->destfile->exists() && !$this->destfile->canWrite()) { $message = "Can not write to the specified destfile!"; $this->failOnErrorAction(null, $message, Project::MSG_ERR); return; } $os = new FileOutputStream($this->destfile); $this->saveProperties($allProps, $os); } } catch (IOException $ioe) { $this->failOnErrorAction($ioe); } }
private function yuiAddFile($file, $is_css = FALSE) { if (!$is_css) { $folder = '/js/'; } else { $folder = '/css/'; } $add = new PhingFile($this->assetDir . $folder . $file->file); if ($add->exists() && $add->canRead()) { $this->yui->addFile($add->getAbsolutePath()); } else { throw new BuildException("Unable to read asset file: " . $add->getPath(), $this->location); } }
/** * Tests whether a name should be selected. * * @param string $name The filename to check for selecting. * @param string $file The full file path. * @return boolean False when the selectors says that the file * should not be selected, True otherwise. */ protected function isSelected($name, $file) { if ($this->selectors !== null) { $basedir = new PhingFile($this->basedir); $file = new PhingFile($file); if (!$file->canRead()) { return false; } foreach ($this->selectors as $selector) { if (!$selector->isSelected($basedir, $name, $file)) { return false; } } } return true; }
/** * @return void */ protected function _verifyManifest() { if (!$this->_file->isFile() || !$this->_file->canRead()) { throw new BuildException( 'Failed reading from manifest file', $this->location ); } $manifest = array (); $fp = fopen($this->_file, 'r'); while (true == ($line = trim(fgets($fp)))) { list ($path, $hash) = explode("\t", $line); $manifest[$path] = $hash; } fclose($fp); $verified = true; // Check for files present which are not in the manifest $filesNotInManifest = array_keys(array_diff_key($manifest, $this->_hashes)); if (!empty ($filesNotInManifest)) { $verified = false; $this->log( 'There are ' . count($filesNotInManifest) . ' files present which are not listed in the manifest', PROJECT::MSG_WARN ); foreach ($filesNotInManifest as $path) { $this->log( 'Extra file: ' . $path, PROJECT::MSG_WARN ); } unset ($path); } unset ($filesNotInManifest); // Check for files listed in the manifest which are not present $filesNotPresent = array_keys(array_diff_key($this->_hashes, $manifest)); if (!empty ($filesNotPresent)) { $verified = false; $this->log( 'There are ' . count($filesNotPresent) . ' files listed in the manifest which are not present', PROJECT::MSG_WARN ); foreach ($filesNotPresent as $path) { $this->log( 'Missing file: ' . $path, PROJECT::MSG_WARN ); } unset ($path); } unset ($filesNotPresent); // Compare manifest hashes with the computed hashes $filesPresent = array_keys(array_intersect_key($manifest, $this->_hashes)); foreach ($filesPresent as $path) { if ($manifest[$path] != $this->_hashes[$path]) { $verified = false; $this->log( 'Hashes do not match: ' . $path, PROJECT::MSG_WARN ); } } unset ($filesPresent); if (!$verified) { throw new BuildException( 'Manifest verification failed' ); } $this->log('Manifest verification successful'); }
/** * @return boolean Whether contents of two files is the same. */ public function contentEquals(PhingFile $file1, PhingFile $file2) { if (!($file1->exists() || $file2->exists())) { return false; } if (!($file1->canRead() || $file2->canRead())) { return false; } $c1 = file_get_contents($file1->getAbsolutePath()); $c2 = file_get_contents($file2->getAbsolutePath()); return trim($c1) == trim($c2); }
/** * {@inheritdoc} * * @param PhingFile $basedir * @param string $filename * @param PhingFile $file * * @return bool */ public function isSelected(PhingFile $basedir, $filename, PhingFile $file) { return $file !== null && $file->canRead(); }