2
2
3
3
namespace whikloj \BagItTools ;
4
4
5
+ use Archive_Tar ;
6
+ use Normalizer ;
5
7
use whikloj \BagItTools \Exceptions \BagItException ;
6
8
use whikloj \BagItTools \Exceptions \FilesystemException ;
9
+ use ZipArchive ;
7
10
8
11
/**
9
12
* Bag class as normal interface for all actions and holder of supporting constructs.
@@ -253,7 +256,7 @@ class Bag
253
256
*
254
257
* @var boolean
255
258
*/
256
- private $ loaded = false ;
259
+ private $ loaded ;
257
260
258
261
/**
259
262
* Bag constructor.
@@ -367,7 +370,7 @@ public function validate(): bool
367
370
public function update (): void
368
371
{
369
372
if (!file_exists ($ this ->makeAbsolute ("data " ))) {
370
- BagUtils::checkedMkdir ($ this ->makeAbsolute ("data " ), 0777 );
373
+ BagUtils::checkedMkdir ($ this ->makeAbsolute ("data " ));
371
374
}
372
375
$ this ->updateBagIt ();
373
376
$ this ->updatePayloadManifests ();
@@ -438,16 +441,16 @@ public function addFile(string $source, string $dest): void
438
441
if (file_exists ($ source )) {
439
442
$ dest = BagUtils::baseInData ($ dest );
440
443
if (!$ this ->pathInBagData ($ dest )) {
441
- throw new BagItException ("Path { $ dest} resolves outside the bag. " );
444
+ throw new BagItException ("Path $ dest resolves outside the bag. " );
442
445
} elseif ($ this ->reservedFilename ($ dest )) {
443
446
throw new BagItException ("The filename requested is reserved on Windows OSes. " );
444
447
} elseif (isset ($ this ->fetchFile ) && $ this ->fetchFile ->reservedPath ($ dest )) {
445
- throw new BagItException ("The path ( { $ dest} ) is used in the fetch.txt file. " );
448
+ throw new BagItException ("The path ( $ dest) is used in the fetch.txt file. " );
446
449
} else {
447
450
$ fullDest = $ this ->makeAbsolute ($ dest );
448
- $ fullDest = \ Normalizer::normalize ($ fullDest );
451
+ $ fullDest = Normalizer::normalize ($ fullDest );
449
452
if (file_exists ($ fullDest )) {
450
- throw new BagItException ("File { $ dest} already exists in the bag. " );
453
+ throw new BagItException ("File $ dest already exists in the bag. " );
451
454
}
452
455
$ dirname = dirname ($ fullDest );
453
456
if (substr ($ this ->makeRelative ($ dirname ), 0 , 5 ) == "data/ " ) {
@@ -460,7 +463,7 @@ public function addFile(string $source, string $dest): void
460
463
$ this ->changed = true ;
461
464
}
462
465
} else {
463
- throw new BagItException ("{ $ source} does not exist " );
466
+ throw new BagItException ("$ source does not exist " );
464
467
}
465
468
}
466
469
@@ -669,7 +672,7 @@ public function addBagInfoTag(string $tag, string $value): void
669
672
}
670
673
$ internal_tag = self ::trimLower ($ tag );
671
674
if (in_array ($ internal_tag , self ::BAG_INFO_GENERATED_ELEMENTS )) {
672
- throw new BagItException ("Field { $ tag} is auto-generated and cannot be manually set. " );
675
+ throw new BagItException ("Field $ tag is auto-generated and cannot be manually set. " );
673
676
}
674
677
if (!$ this ->bagInfoTagExists ($ internal_tag )) {
675
678
$ this ->bagInfoTagIndex [$ internal_tag ] = [];
@@ -695,7 +698,7 @@ public function setFileEncoding(string $encoding): void
695
698
$ encoding = self ::trimLower ($ encoding );
696
699
$ charset = BagUtils::getValidCharset ($ encoding );
697
700
if (is_null ($ charset )) {
698
- throw new BagItException ("Character set { $ encoding} is not supported. " );
701
+ throw new BagItException ("Character set $ encoding is not supported. " );
699
702
} else {
700
703
if ($ encoding == self ::trimLower (self ::DEFAULT_FILE_ENCODING )) {
701
704
// go back to default.
@@ -786,7 +789,7 @@ public function addAlgorithm(string $algorithm): void
786
789
}
787
790
$ this ->changed = true ;
788
791
} else {
789
- throw new BagItException ("Algorithm { $ algorithm} is not supported. " );
792
+ throw new BagItException ("Algorithm $ algorithm is not supported. " );
790
793
}
791
794
}
792
795
@@ -819,7 +822,7 @@ public function removeAlgorithm(string $algorithm): void
819
822
}
820
823
$ this ->changed = true ;
821
824
} else {
822
- throw new BagItException ("Algorithm { $ algorithm} is not supported. " );
825
+ throw new BagItException ("Algorithm $ algorithm is not supported. " );
823
826
}
824
827
}
825
828
@@ -848,7 +851,7 @@ public function setAlgorithm(string $algorithm): void
848
851
}
849
852
$ this ->changed = true ;
850
853
} else {
851
- throw new BagItException ("Algorithm { $ algorithm} is not supported. " );
854
+ throw new BagItException ("Algorithm $ algorithm is not supported. " );
852
855
}
853
856
}
854
857
@@ -1139,7 +1142,7 @@ private function loadBag(): void
1139
1142
{
1140
1143
$ root = $ this ->getBagRoot ();
1141
1144
if (!file_exists ($ root )) {
1142
- throw new BagItException ("Path { $ root} does not exist, could not load Bag. " );
1145
+ throw new BagItException ("Path $ root does not exist, could not load Bag. " );
1143
1146
}
1144
1147
$ this ->resetErrorsAndWarnings ();
1145
1148
// Reset these or we end up with double manifests in a validate() situation.
@@ -1166,7 +1169,7 @@ private function createNewBag(): void
1166
1169
{
1167
1170
$ this ->resetErrorsAndWarnings ();
1168
1171
if (file_exists ($ this ->bagRoot )) {
1169
- throw new BagItException ("New bag directory { $ this ->bagRoot } exists " );
1172
+ throw new BagItException ("New bag directory $ this ->bagRoot exists " );
1170
1173
}
1171
1174
BagUtils::checkedMkdir ($ this ->bagRoot . DIRECTORY_SEPARATOR . "data " , 0777 , true );
1172
1175
$ this ->updateBagIt ();
@@ -1215,7 +1218,7 @@ private function loadBagInfo(): bool
1215
1218
if (file_exists ($ fullPath )) {
1216
1219
$ file_contents = file_get_contents ($ fullPath );
1217
1220
if ($ file_contents === false ) {
1218
- throw new FilesystemException ("Unable to access { $ info_file} " );
1221
+ throw new FilesystemException ("Unable to access $ info_file " );
1219
1222
}
1220
1223
$ bagData = [];
1221
1224
$ lineCount = 0 ;
@@ -1248,18 +1251,18 @@ private function loadBagInfo(): bool
1248
1251
if ($ this ->mustNotRepeatBagInfoExists ($ current_tag )) {
1249
1252
$ this ->addBagError (
1250
1253
$ info_file ,
1251
- "Tag { $ current_tag} MUST not be repeated. (Line { $ lineCount } ) "
1254
+ "Line $ lineCount : Tag $ current_tag MUST not be repeated. "
1252
1255
);
1253
1256
} elseif ($ this ->shouldNotRepeatBagInfoExists ($ current_tag )) {
1254
1257
$ this ->addBagWarning (
1255
1258
$ info_file ,
1256
- "Tag { $ current_tag} SHOULD NOT be repeated. (Line { $ lineCount } ) "
1259
+ "Line $ lineCount : Tag $ current_tag SHOULD NOT be repeated. "
1257
1260
);
1258
1261
}
1259
1262
if (($ this ->compareVersion ('1.0 ' ) <= 0 ) && (!empty ($ matches [1 ]) || !empty ($ matches [3 ]))) {
1260
1263
$ this ->addBagError (
1261
1264
$ info_file ,
1262
- "Labels cannot begin or end with a whitespace. (Line { $ lineCount } ) "
1265
+ "Line $ lineCount : Labels cannot begin or end with a whitespace. "
1263
1266
);
1264
1267
}
1265
1268
if ($ lineLength >= Bag::BAGINFO_AUTOWRAP_GUESS_LENGTH ) {
@@ -1365,7 +1368,7 @@ private function updateBagInfo(): void
1365
1368
// We don't guarantee newlines remain once you edit a bag.
1366
1369
$ value = str_replace ("\r\n" , " " , $ value );
1367
1370
$ value = str_replace ("\n" , " " , $ value );
1368
- $ data = self ::wrapBagInfoText ("{ $ tag} : { $ value} " );
1371
+ $ data = self ::wrapBagInfoText ("$ tag: $ value " );
1369
1372
foreach ($ data as $ line ) {
1370
1373
$ line = $ this ->encodeText ($ line );
1371
1374
BagUtils::checkedFwrite ($ fp , $ line . PHP_EOL );
@@ -1513,7 +1516,7 @@ function ($o) {
1513
1516
private static function wrapAtLength (string $ text , int $ length ): array
1514
1517
{
1515
1518
$ text = str_replace ("\n" , "" , $ text );
1516
- $ wrapped = wordwrap ($ text , $ length, "\n" );
1519
+ $ wrapped = wordwrap ($ text , $ length );
1517
1520
return explode ("\n" , $ wrapped );
1518
1521
}
1519
1522
@@ -1537,7 +1540,7 @@ private function loadTagManifests(): bool
1537
1540
if (isset ($ tagManifests [$ hash ])) {
1538
1541
$ this ->addBagError (
1539
1542
$ this ->makeRelative ($ file ),
1540
- "More than one tag manifest for hash ( { $ hash} ) found. "
1543
+ "More than one tag manifest for hash ( $ hash) found. "
1541
1544
);
1542
1545
} else {
1543
1546
$ tagManifests [$ hash ] = new TagManifest ($ this , $ hash , true );
@@ -1653,7 +1656,7 @@ private function loadPayloadManifests(): void
1653
1656
$ hash = self ::determineHashFromFilename ($ manifest );
1654
1657
$ relative_filename = $ this ->makeRelative ($ manifest );
1655
1658
if (!is_null ($ hash ) && !in_array ($ hash , array_keys (self ::HASH_ALGORITHMS ))) {
1656
- throw new BagItException ("We do not support the algorithm { $ hash} " );
1659
+ throw new BagItException ("We do not support the algorithm $ hash " );
1657
1660
} elseif (is_null ($ hash )) {
1658
1661
$ this ->addBagError (
1659
1662
$ relative_filename ,
@@ -1662,7 +1665,7 @@ private function loadPayloadManifests(): void
1662
1665
} elseif (isset ($ this ->payloadManifests [$ hash ])) {
1663
1666
$ this ->addBagError (
1664
1667
$ relative_filename ,
1665
- "More than one payload manifest for hash ( { $ hash} ) found. "
1668
+ "More than one payload manifest for hash ( $ hash) found. "
1666
1669
);
1667
1670
} else {
1668
1671
$ temp = new PayloadManifest ($ this , $ hash , true );
@@ -1761,7 +1764,7 @@ private function loadBagIt(): void
1761
1764
} else {
1762
1765
$ contents = file_get_contents ($ fullPath );
1763
1766
if ($ contents === false ) {
1764
- throw new FilesystemException ("Unable to read { $ fullPath} " );
1767
+ throw new FilesystemException ("Unable to read $ fullPath " );
1765
1768
}
1766
1769
$ lines = BagUtils::splitFileDataOnLineEndings ($ contents );
1767
1770
// remove blank lines.
@@ -1891,14 +1894,14 @@ private function makePackage(string $filename): void
1891
1894
*/
1892
1895
private function makeZip (string $ filename ): void
1893
1896
{
1894
- $ zip = new \ ZipArchive ();
1895
- $ res = $ zip ->open ($ filename , \ ZipArchive::CREATE );
1897
+ $ zip = new ZipArchive ();
1898
+ $ res = $ zip ->open ($ filename , ZipArchive::CREATE );
1896
1899
if ($ res === true ) {
1897
1900
$ files = BagUtils::getAllFiles ($ this ->bagRoot );
1898
1901
$ parentPrefix = basename ($ this ->bagRoot );
1899
1902
foreach ($ files as $ file ) {
1900
1903
$ relative = $ this ->makeRelative ($ file );
1901
- $ zip ->addFile ($ file , "{ $ parentPrefix} / { $ relative} " );
1904
+ $ zip ->addFile ($ file , "$ parentPrefix/ $ relative " );
1902
1905
}
1903
1906
$ zip ->close ();
1904
1907
} else {
@@ -1918,14 +1921,14 @@ private function makeTar(string $filename): void
1918
1921
{
1919
1922
$ extension = self ::getExtensions ($ filename );
1920
1923
$ compression = self ::extensionTarCompression ($ extension );
1921
- $ tar = new \ Archive_Tar ($ filename , $ compression );
1924
+ $ tar = new Archive_Tar ($ filename , $ compression );
1922
1925
if ($ tar === false ) {
1923
1926
throw new FilesystemException ("Error creating Tar file. " );
1924
1927
}
1925
1928
$ parent = $ this ->getParentDir ();
1926
1929
$ files = BagUtils::getAllFiles ($ this ->bagRoot );
1927
1930
if (!$ tar ->createModify ($ files , "" , $ parent )) {
1928
- throw new FilesystemException ("Error adding files to { $ filename} . " );
1931
+ throw new FilesystemException ("Error adding files to $ filename. " );
1929
1932
}
1930
1933
}
1931
1934
@@ -1955,7 +1958,7 @@ private function getParentDir(): string
1955
1958
private static function uncompressBag (string $ filepath ): string
1956
1959
{
1957
1960
if (!file_exists ($ filepath )) {
1958
- throw new BagItException ("File { $ filepath} does not exist. " );
1961
+ throw new BagItException ("File $ filepath does not exist. " );
1959
1962
}
1960
1963
$ extension = self ::getExtensions ($ filepath );
1961
1964
if (in_array ($ extension , self ::ZIP_EXTENSIONS )) {
@@ -1981,7 +1984,7 @@ private static function uncompressBag(string $filepath): string
1981
1984
*/
1982
1985
private static function unzipBag (string $ filename ): string
1983
1986
{
1984
- $ zip = new \ ZipArchive ();
1987
+ $ zip = new ZipArchive ();
1985
1988
$ res = $ zip ->open ($ filename );
1986
1989
if ($ res === false ) {
1987
1990
throw new FilesystemException ("Unable to unzip $ filename " );
@@ -2008,10 +2011,10 @@ private static function untarBag(string $filename, string $extension): string
2008
2011
{
2009
2012
$ compression = self ::extensionTarCompression ($ extension );
2010
2013
$ directory = self ::extractDir ();
2011
- $ tar = new \ Archive_Tar ($ filename , $ compression );
2014
+ $ tar = new Archive_Tar ($ filename , $ compression );
2012
2015
$ res = $ tar ->extract ($ directory );
2013
2016
if ($ res === false ) {
2014
- throw new FilesystemException ("Usable to untar { $ filename} " );
2017
+ throw new FilesystemException ("Unable to untar $ filename " );
2015
2018
}
2016
2019
return $ directory ;
2017
2020
}
0 commit comments