Beispiel #1
0
 /**
  * @depends     testExist
  * @return      void
  */
 public function testCopy()
 {
     // create file
     $filePath = __DIR__ . '/tmp/example_copy.txt';
     if (!is_file($filePath)) {
         fclose(fopen($filePath, 'x'));
     }
     // create target dir
     $newPath = __DIR__ . '/tmp/target';
     if (!is_dir($newPath)) {
         mkdir($newPath, 0777);
     }
     // remove copied file
     $filePathNew = $newPath . '/example_copy.txt';
     if (is_file($filePathNew)) {
         unlink($filePathNew);
     }
     $fileObject = new File($filePath);
     $this->assertTrue($fileObject->isFile());
     $this->assertTrue($fileObject->isWritable());
     $this->assertTrue($fileObject->copy($newPath));
     $this->assertEquals($filePathNew, $fileObject->getPathname());
     $this->assertTrue($fileObject->isFile());
     // create dir
     $filePath = __DIR__ . '/tmp/ExampleCopy';
     if (!is_dir($filePath)) {
         mkdir($filePath, 0777);
     }
     // remove copied file
     $filePathNew = $newPath . '/ExampleCopy';
     if (is_dir($filePathNew)) {
         rmdir($filePathNew);
     }
     $fileObject = new File($filePath);
     $this->assertTrue($fileObject->isDir());
     $this->assertTrue($fileObject->isWritable());
     $this->assertTrue($fileObject->copy($newPath));
     $this->assertEquals($filePathNew, $fileObject->getPathname());
     $this->assertTrue($fileObject->isDir());
 }
Beispiel #2
0
echo 'File permission: ' . $fileObject->getPermission();
echo '<br/>';
echo '<br/>';
echo 'Create new directory';
echo '<br/>';
$newDirectoryPath = __DIR__ . '/tmp/target/move/';
$newDirectoryFileObject = new File($newDirectoryPath);
if (!$newDirectoryFileObject->isDir()) {
    $newDirectoryFileObject->mkdirs();
}
echo 'Copy file to target directory';
echo '<br/>';
$examplePath = __DIR__ . '/example.txt';
$exampleCopyPath = __DIR__ . '/tmp/target/';
$fileObject = new File($examplePath);
$fileObject->copy($exampleCopyPath);
$fileObject->rename('example_copy.txt');
echo 'Move copied file to a new directory';
echo '<br/>';
$exampleMovePath = __DIR__ . '/tmp/target/move/';
$fileObject->move($exampleMovePath);
$fileObject->rename('example_move.txt');
echo 'Moved File ' . $fileObject->getPathname() . ' do' . ($fileObject->exists() ? '' : ' not') . ' exist.';
echo '<br/>';
echo 'File is' . ($fileObject->isReadable() ? '' : ' not') . ' readable.';
echo '<br/>';
echo 'File is' . ($fileObject->isWritable() ? '' : ' not') . ' writeable.';
echo '<br/>';
echo 'File permission: ' . $fileObject->getPermission();
echo '<br/>';
echo '<br/>';
Beispiel #3
0
 /**
  *
  */
 public function install()
 {
     // read configuration file
     $this->configuration = json_decode(file_get_contents($this->configurationFilepath), true);
     if (null == $this->configuration) {
         throw new \Exception('Configuration file contains invalid JSON.');
     }
     if (0 < count($this->configuration['version'])) {
         $requirements = array();
         foreach ($this->configuration['version'] as $version => $versionEntry) {
             $requirements = array_merge($requirements, $this->getRequirements($versionEntry['require']));
         }
         // set folder to install requirements
         if (isset($this->configuration['knowledge-directory'])) {
             $folderForRequirements = $this->defaultRootFolder . $this->configuration['knowledge-directory'] . '/';
         } else {
             $folderForRequirements = $this->defaultRootFolder . 'knowledge/';
         }
         // if there are requirements to install, create knowledge directory first
         if (0 < count($requirements) && false == file_exists($folderForRequirements)) {
             $fileObject = new File($folderForRequirements);
             $fileObject->mkdirs();
         }
         $nodeUtils = new NodeUtils();
         $curl = new Curl();
         $curl->setOpt(CURLOPT_ENCODING, 'gzip');
         $curl->setOpt(CURLOPT_FOLLOWLOCATION, true);
         foreach ($requirements as $name => $requirement) {
             // if a valid local file was given
             if (file_exists($requirement['file'])) {
                 $fileObject = new File($requirement['file']);
                 $fileObject->copy($targetPath);
                 // if a valid URL was given
             } elseif ($nodeUtils->simpleCheckURI($requirement['file'])) {
                 // split name to be able to create all folders
                 $name = explode('/', $name);
                 $vendor = $name[0];
                 $project = $name[1];
                 // remove all maybe existing files
                 if (file_exists($folderForRequirements . $vendor . '/' . $project . '.ttl')) {
                     $fileObject = new File($folderForRequirements . $vendor . '/' . $project . '.ttl');
                     $fileObject->delete();
                 }
                 if (false == file_exists($folderForRequirements . $vendor)) {
                     $fileObject = new File($folderForRequirements . $vendor);
                     $fileObject->mkdirs();
                 }
                 echo PHP_EOL . '- Download ' . $vendor . '/' . $project;
                 if (isset($requirement['file-format'])) {
                     $fileFormat = $requirement['file-format'];
                 } else {
                     $fileFormat = $this->getFileFormat($requirement['file']);
                 }
                 if (null !== $fileFormat) {
                     $curl->download($requirement['file'], $folderForRequirements . $vendor . '/' . $project . '.' . $fileFormat);
                     if ('xml' == $fileFormat) {
                         $fileFormatForParsing = 'rdf-xml';
                     } elseif ('ttl' == $fileFormat) {
                         $fileFormatForParsing = 'turtle';
                     } elseif ('n3' == $fileFormat || 'nt' == $fileFormat) {
                         $fileFormatForParsing = 'n-triples';
                     } else {
                         $fileFormatForParsing = $fileFormat;
                     }
                     if (isset($this->configuration['target-file-serialization']) && $this->configuration['target-file-serialization'] != $fileFormatForParsing) {
                         // get parser suiteable for the given file format
                         $parserFactory = new ParserFactory(new NodeFactoryImpl(), new StatementFactoryImpl());
                         $parser = $parserFactory->createParserFor($fileFormatForParsing);
                         if (null == $parser) {
                             echo ' - Unknown file format given: ' . $fileFormatForParsing . '; Leaving file at : ' . $fileFormat;
                             continue;
                         }
                         // parse file content and transform it into a statement
                         $statementIterator = $parser->parseStreamToIterator($folderForRequirements . $vendor . '/' . $project . '.' . $fileFormat);
                         /* go through iterator and output the first few statements
                                                     $i = 0;
                                                     foreach ($statementIterator as $statement) {
                                                         echo (string)$statement->getSubject()
                                                             . ' ' . (string)$statement->getPredicate()
                                                             . ' ' . (string)$statement->getObject()
                                                             . PHP_EOL;
                         
                                                         if ($i++ == 10) { break; }
                                                     }
                         
                                                     continue;*/
                         // get serializer for target file format
                         $serializerFactory = new SerializerFactory(new NodeFactoryImpl(), new StatementFactoryImpl());
                         $targetFormatForSerialization = $this->configuration['target-file-serialization'];
                         if ('rdf-xml' == $targetFormatForSerialization) {
                             $serializedFileFormat = 'xml';
                         } elseif ('turtle' == $targetFormatForSerialization) {
                             $serializedFileFormat = 'ttl';
                         } elseif ('n-triples' == $targetFormatForSerialization) {
                             $serializedFileFormat = 'n3';
                         } else {
                             $serializedFileFormat = $targetFormatForSerialization;
                         }
                         $targetFile = 'file://' . $folderForRequirements . $vendor . '/' . $project . '.' . $serializedFileFormat;
                         $serializer = $serializerFactory->createSerializerFor($targetFormatForSerialization);
                         $serializer->serializeIteratorToStream($statementIterator, fopen($targetFile, 'w'));
                         if (file_exists($targetFile)) {
                             unlink($folderForRequirements . $vendor . '/' . $project . '.' . $fileFormat);
                         }
                     }
                     echo ' - done';
                 } else {
                     echo ' - unknown file format for the ontology reference: ' . $requirement['file'];
                 }
             }
         }
         echo PHP_EOL;
     } else {
         return 'No version information found. Did you added elements to version array?';
     }
 }