CreateExtensionFilesTask.php 6.67 KB
Newer Older
1
<?php
2
3
namespace T3o\TerFe2\Task;

flyguide's avatar
flyguide committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*******************************************************************
 *  Copyright notice
 *
 *  (c) 2011 Kai Vogel <kai.vogel@speedprogs.de>, Speedprogs.de
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ******************************************************************/

/**
 * Create zip archives from t3x files and generate images
 */
31
class CreateExtensionFilesTask extends \T3o\TerFe2\Task\AbstractTask
32
33
34
{

    /**
35
     * @var \T3o\TerFe2\Domain\Repository\VersionRepository
36
37
38
39
     */
    protected $versionRepository;

    /**
40
     * @var \T3o\TerFe2\Provider\ProviderManager
41
42
43
44
     */
    protected $providerManager;

    /**
45
     * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
     */
    protected $persistenceManager;

    /**
     * @var string
     */
    protected $mediaRootPath = '';

    /**
     * @var array
     */
    protected $imageSizes = array('small', 'large');


    /**
     * Initialize task
     *
     * @return void
     */
    public function initializeTask()
    {
67
68
        $this->versionRepository = $this->objectManager->get(\T3o\TerFe2\Domain\Repository\VersionRepository::class);
        $this->providerManager = $this->objectManager->get(\T3o\TerFe2\Provider\ProviderManager::class);
69
        $this->persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
70
71

        if (empty($this->setup['settings.']['mediaRootPath'])) {
72
            throw new \Exception('Please configure "plugin.tx_terfe2.settings.mediaRootPath" in TypoScript setup');
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        }
        $this->mediaRootPath = $this->setup['settings.']['mediaRootPath'];
    }


    /**
     * Execute the task
     *
     * @param integer $lastRun Timestamp of the last run
     * @param integer $offset Starting point
     * @param integer $count Element count to process at once
     * @return boolean TRUE on success
     */
    protected function executeTask($lastRun, $offset, $count)
    {
        // Get all unprocessed versions
        $versions = $this->versionRepository->findForMediaCreation($offset, $count);
        if ($versions->count() === 0) {
            return FALSE;
        }

        // Simulate working directory "htdocs", required for file_exists check
95
        // in \TYPO3\CMS\Core\Imaging\GraphicalFunctions::getImageDimensions
96
97
98
99
100
101
102
103
104
105
        $currentDir = getcwd();
        chdir(PATH_site);

        // Create ZIP file only for last version
        $lastVersionOnly = !empty($this->setup['settings.']['onlyLatestVersionZip']);

        foreach ($versions as $version) {
            // Get media path for the extension
            $extKey = $version->getExtension()->getExtKey();
            $lastVersion = $version->getExtension()->getLastVersion();
106
            $extensionMediaPath = \T3o\TerFe2\Utility\File::getAbsoluteDirectory($this->mediaRootPath . $extKey);
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

            // Create zip file
            if (!$lastVersionOnly || ($lastVersion === $version)) {
                $zipFile = $this->createZipFile($version, $extensionMediaPath);
                if (!empty($zipFile)) {
                    $version->setZipFileSize(filesize($zipFile));
                    $version->setHasZipFile(TRUE);
                }
            }

            // Create images
            if ($this->createImages($version, $extensionMediaPath)) {
                $version->setHasImages(TRUE);
            }

            $this->persistenceManager->persistAll();
        }

        // Revert working directory
        chdir($currentDir);

        return TRUE;
    }


    /**
     * Create a zip file for given version
     *
135
     * @param \T3o\TerFe2\Domain\Model\Version $version Path to t3x file
136
137
138
     * @param string $extensionMediaPath Path to media files
     * @return string Name of the zip file
     */
139
    protected function createZipFile(\T3o\TerFe2\Domain\Model\Version $version, $extensionMediaPath)
140
141
142
143
144
145
146
147
148
149
    {
        if (empty($extensionMediaPath)) {
            return '';
        }

        $provider = $this->providerManager->getProvider($version->getExtensionProvider());
        $t3xFileName = $provider->getFileUrl($version, 't3x');
        $zipFileName = $extensionMediaPath . basename($provider->getFileName($version, 'zip'));

        // Check if zip file already exists
150
        if (\T3o\TerFe2\Utility\File::fileExists($zipFileName)) {
151
152
153
154
            return $zipFileName;
        }

        // Check file hash
155
        $fileHash = \T3o\TerFe2\Utility\File::getFileHash($t3xFileName);
156
157
158
159
160
        if ($fileHash != $version->getFileHash()) {
            return '';
        }

        // Create zip file
161
        $result = \T3o\TerFe2\Utility\Archive::convertT3xToZip($t3xFileName, $zipFileName);
162
163
164
165
166
167
168
169
170
171
172
        if (!empty($result)) {
            return $zipFileName;
        }

        return '';
    }


    /**
     * Create images for given version
     *
173
     * @param \T3o\TerFe2\Domain\Model\Version $version Path to t3x file
174
175
176
     * @param string $extensionMediaPath Path to media files
     * @return boolean TRUE if success
     */
177
    protected function createImages(\T3o\TerFe2\Domain\Model\Version $version, $extensionMediaPath)
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
    {
        if (empty($extensionMediaPath)) {
            return FALSE;
        }

        // Get image files
        $imageFiles = array();
        $mediaObjects = $version->getMedia();
        foreach ($mediaObjects as $media) {
            if ($media->getType() === 'image') {
                // Source must contains only image file name without path
                $imageFiles[] = $extensionMediaPath . $media->getSource();
            }
        }

        if (empty($imageFiles)) {
            return TRUE;
        }

        foreach ($this->imageSizes as $imageSize) {
            if (empty($this->setup['settings.'][$imageSize . 'Image'])) {
                continue;
            }
201
            $images = \T3o\TerFe2\Service\Image::processImages($imageFiles, $this->setup['settings.'][$imageSize . 'Image']);
202
            if (empty($images)) {
203
                throw new \Exception('Could not create image files');
204
205
206
207
208
            }
        }

        return TRUE;
    }
flyguide's avatar
flyguide committed
209
210

}