Skip to content

Commit f7b20c9

Browse files
committed
Update the save() method to set relative paths for resources. Fixes #41.
1 parent 48e73fc commit f7b20c9

2 files changed

Lines changed: 86 additions & 18 deletions

File tree

src/Datapackages/BaseDatapackage.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,20 +183,27 @@ public function save($zip_filename)
183183
{
184184
Package::isZipPresent();
185185
$zip = new ZipArchive();
186+
187+
$packageCopy = $this->copy();
188+
186189
$base = tempnam(sys_get_temp_dir(), 'datapackage-zip-');
187190
$files = [
188191
'datapackage.json' => $base.'datapackage.json',
189192
];
190193
$ri = 0;
191-
foreach ($this as $resource) {
194+
foreach ($packageCopy->resources() as $resource) {
195+
$resourceFiles = [];
192196
$fileNames = $resource->save($base.'resource-'.$ri);
193197
foreach ($fileNames as $fileName) {
194198
$relname = str_replace($base.'resource-'.$ri, '', $fileName);
195199
$files['resource-'.$ri.$relname] = $fileName;
200+
$resourceFiles[] = 'resource-'.$ri.$relname;
196201
}
202+
$resource->descriptor()->path = count($resourceFiles) == 1 ? $resourceFiles[0] : $resourceFiles;
197203
++$ri;
198204
}
199-
$this->saveDescriptor($files['datapackage.json']);
205+
$packageCopy->saveDescriptor($files['datapackage.json']);
206+
200207
register_shutdown_function(function () use ($base) {
201208
Utils::removeDir($base);
202209
});
@@ -210,19 +217,24 @@ public function save($zip_filename)
210217
}
211218
}
212219

220+
protected function copy()
221+
{
222+
return new static($this->descriptor, $this->basePath, true);
223+
}
224+
213225
protected $descriptor;
214226
protected $currentResourcePosition = 0;
215227
protected $basePath;
216228
protected $skipValidations = false;
217229

218-
/**
219-
* called by the resources iterator for each iteration.
220-
*
221-
* @param object $descriptor
222-
*
223-
* @return \frictionlessdata\datapackage\Resources\BaseResource
224-
* @throws \frictionlessdata\datapackage\Exceptions\ResourceValidationFailedException
225-
*/
230+
/**
231+
* called by the resources iterator for each iteration.
232+
*
233+
* @param object $descriptor
234+
*
235+
* @return \frictionlessdata\datapackage\Resources\BaseResource
236+
* @throws \frictionlessdata\datapackage\Exceptions\ResourceValidationFailedException
237+
*/
226238
protected function initResource($descriptor)
227239
{
228240
return Factory::resource($descriptor, $this->basePath, $this->skipValidations);

tests/DatapackageTest.php

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,29 @@ public function testCreateEditDatapackageDescriptor()
476476
$zip->close();
477477
unlink($filename);
478478
$tempdir = $tempdir.DIRECTORY_SEPARATOR;
479+
480+
//after saving to disk, the paths are updated
481+
$expectedDatapackageDescriptor = (object) [
482+
'name' => 'my-datapackage-name',
483+
'resources' => [
484+
(object) [
485+
'name' => 'my-default-resource',
486+
'path' => ["resource-0-data-0", "resource-0-data-1"],
487+
],
488+
(object) [
489+
'name' => 'my-renamed-tabular-resource',
490+
'path' => "resource-1.csv",
491+
'profile' => 'tabular-data-resource',
492+
'schema' => (object) [
493+
'fields' => [
494+
(object) ['name' => 'id', 'type' => 'integer'],
495+
(object) ['name' => 'name', 'type' => 'string'],
496+
],
497+
],
498+
],
499+
],
500+
];
501+
479502
$this->assertEquals($expectedDatapackageDescriptor, json_decode(file_get_contents($tempdir.'datapackage.json')));
480503
$this->assertEquals('foo', file_get_contents($tempdir.'resource-0-data-0'));
481504
$this->assertEquals("testing 改善\n", file_get_contents($tempdir.'resource-0-data-1'));
@@ -484,6 +507,39 @@ public function testCreateEditDatapackageDescriptor()
484507
Utils::removeDir($tempdir);
485508
}
486509

510+
public function testSaveAndLoadZip()
511+
{
512+
//create example csv
513+
file_put_contents('/tmp/example.csv', "name,email\nJohn Doe,john@example.com");
514+
515+
//create a new datapackage object
516+
$package = Package::create(['name' => 'csv-example','profile' => 'tabular-data-package']);
517+
518+
//add a csv file
519+
$package->addResource('example.csv', [
520+
"profile" => "tabular-data-resource",
521+
"schema" => ["fields" => [["name" => "name", "type" => "string"],["name" => "email", "type" => "string"]]],
522+
"path" => '/tmp/example.csv'
523+
]);
524+
525+
//save the datapackage
526+
if (is_file('datapackage.zip')) {
527+
unlink('datapackage.zip');
528+
}
529+
$package->save("datapackage.zip");
530+
531+
//delete example csv
532+
unlink('/tmp/example.csv');
533+
534+
//load the new package
535+
$package2 = Package::load('datapackage.zip');
536+
537+
//assert you get expected content back out
538+
$this->assertEquals([['name' => 'John Doe', 'email' => 'john@example.com']], $package2->resource('example.csv')->read());
539+
540+
unlink('datapackage.zip');
541+
}
542+
487543
public function testLoadDatapackageZip()
488544
{
489545
$package = Package::load(dirname(__FILE__).'/fixtures/datapackage_zip.zip');
@@ -626,21 +682,21 @@ public function testCsvDialect()
626682
'CommitteeTypeDesc' => 'ועדה משותפת',
627683
'Email' => null,
628684
'StartDate' => Carbon::__set_state(array(
629-
'date' => '2004-08-12 00:00:00.000000',
630-
'timezone_type' => 3,
631-
'timezone' => 'UTC',
632-
)),
685+
'date' => '2004-08-12 00:00:00.000000',
686+
'timezone_type' => 3,
687+
'timezone' => 'UTC',
688+
)),
633689
'FinishDate' => null,
634690
'AdditionalTypeID' => null,
635691
'AdditionalTypeDesc' => null,
636692
'ParentCommitteeID' => null,
637693
'CommitteeParentName' => null,
638694
'IsCurrent' => true,
639695
'LastUpdatedDate' => Carbon::__set_state(array(
640-
'date' => '2015-03-20 12:02:57.000000',
641-
'timezone_type' => 3,
642-
'timezone' => 'UTC',
643-
)),
696+
'date' => '2015-03-20 12:02:57.000000',
697+
'timezone_type' => 3,
698+
'timezone' => 'UTC',
699+
)),
644700
), $row);
645701
}
646702
++$rowNum;

0 commit comments

Comments
 (0)