Skip to content

Commit

Permalink
Remove dependency on Zippy
Browse files Browse the repository at this point in the history
  • Loading branch information
ohadschn authored and benbalter committed Aug 26, 2017
1 parent 399e03a commit 478c68b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"require": {
"mustangostang/spyc": "~0.5",
"pixel418/markdownify": "~2.1",
"alchemy/zippy": "~0.3"
},
"license": "GPLv3 or later",
"authors": [
Expand Down
49 changes: 39 additions & 10 deletions jekyll-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

require_once dirname( __FILE__ ) . '/lib/cli.php';
require_once dirname( __FILE__ ) . '/vendor/autoload.php';
use Alchemy\Zippy\Zippy;

/**
* Class Jekyll_Export
Expand Down Expand Up @@ -320,11 +319,11 @@ function init_temp_dir() {

WP_Filesystem();

// when on Azure Web App use d:/home/temp/ to avoid weird default temp folder behavior
// when on Azure Web App use d:/home/temp/ to avoid weird default temp folder behavior.
$temp_dir = (getenv( 'WEBSITE_SITE_NAME' ) !== false) ? 'd:/home/temp/' : get_temp_dir();

$this->dir = $temp_dir . 'wp-jekyll-' . md5( time() ) . '/';
$this->zip = $temp_dir . 'wp-jekyll.zip';
$this->dir = realpath( $temp_dir ) . 'wp-jekyll-' . md5( time() ) . '/';
$this->zip = realpath( $temp_dir ) . 'wp-jekyll.zip';
$wp_filesystem->mkdir( $this->dir );
$wp_filesystem->mkdir( $this->dir . '_posts/' );
$wp_filesystem->mkdir( $this->dir . 'wp-content/' );
Expand Down Expand Up @@ -415,17 +414,47 @@ function write( $output, $post ) {

}

/**
* Creates a zip archive of the given folder
*
* @param String $source the source directory to zip.
* @param String $destination the path to output the zip.
* @param Constant $flags flags to pass to ZipArchive->open().
*/
function zip_folder( $source, $destination, $flags = ZipArchive::CREATE | ZIPARCHIVE::OVERWRITE ) {

if ( ! file_exists( $source ) ) {
die( 'file does not exist: ' . esc_html( $source ) );
}

$zip = new ZipArchive();
if ( ! $zip->open( $destination, $flags ) ) {
die( 'Cannot open zip archive: ' . esc_html( $destination ) );
}

$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $source ), RecursiveIteratorIterator::SELF_FIRST );

foreach ( $files as $file ) {
// Ignore "." and ".." folders.
if ( in_array( substr( $file, strrpos( $file, DIRECTORY_SEPARATOR ) + 1 ), array( '.', '..' ), true ) ) {
continue;
}

if ( is_dir( $file ) === true ) {
$zip->addEmptyDir( substr( realpath( $file ), strlen( $source ) ) );
} elseif ( is_file( $file ) === true ) {
$zip->addFile( $file, substr( realpath( $file ) , strlen( $source ) ) );
}
}

return $zip->close();
}

/**
* Zip temp dir
*/
function zip() {
$zippy = Zippy::load();
$zippy->create(
$this->zip, array(
'./' => $this->dir,
), true
);
$this->zip_folder( $this->dir, $this->zip );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion phpcs.ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
</rule>

<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/vendor/autoload.php</exclude-pattern>
</ruleset>
15 changes: 9 additions & 6 deletions tests/test-wordpress-to-jekyll-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,17 @@ function test_zip() {
$jekyll_export->zip();
$this->assertTrue( file_exists( $jekyll_export->zip ) );

$zippy = Zippy::load();
$archive = $zippy->open( $jekyll_export->zip );

$temp_dir = get_temp_dir() . 'jekyll-export-extract';
array_map( 'unlink', glob( "$temp_dir/*.*" ) );
delete_dir( $temp_dir );
mkdir( $temp_dir );
$archive->extract( $temp_dir );
if ( file_exists( $temp_dir ) ) {
delete_dir( $temp_dir );
}

$zip = new ZipArchive();
$zip->open( $jekyll_export->zip );
$zip->extractTo( $temp_dir );
$zip->close();

$this->assertTrue( file_exists( $temp_dir . '/foo.txt' ) );
$this->assertEquals( 'bar', file_get_contents( $temp_dir . '/foo.txt' ) );
}
Expand Down

0 comments on commit 478c68b

Please sign in to comment.