Skip to content

Commit c9d132f

Browse files
committed
Rename StorageLegacy back to Storage
1 parent 8add3fd commit c9d132f

8 files changed

Lines changed: 2034 additions & 246 deletions

File tree

composer-setup.php

Lines changed: 1788 additions & 0 deletions
Large diffs are not rendered by default.

composer.phar

3.13 MB
Binary file not shown.

src/DiagramGenerator/Board.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use DiagramGenerator\Config;
66
use DiagramGenerator\Fen;
77
use DiagramGenerator\Image\Storage;
8-
use DiagramGenerator\Image\StorageLegacy;
8+
use DiagramGenerator\Image\StorageNew;
99
use DiagramGenerator\Image\Image;
1010

1111
/**
@@ -78,9 +78,9 @@ public function getImage()
7878
protected function generateImage()
7979
{
8080
if ($this->config->hasThemeUrls()) {
81-
$storage = new Storage($this->cacheDir, $this->pieceThemeUrl, $this->boardTextureUrl);
81+
$storage = new StorageNew($this->cacheDir, $this->pieceThemeUrl, $this->boardTextureUrl);
8282
} else {
83-
$storage = new StorageLegacy($this->cacheDir, $this->pieceThemeUrl, $this->boardTextureUrl);
83+
$storage = new Storage($this->cacheDir, $this->pieceThemeUrl, $this->boardTextureUrl);
8484
}
8585
$image = new Image($storage, $this->config);
8686
$topPadding = $storage->getMaxPieceHeight($this->fen, $this->config) - $this->config->getSize()->getCell();

src/DiagramGenerator/Image/Image.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ class Image
1818
/** @var BaseImage */
1919
protected $image;
2020

21-
/** @var Storage|StorageLegacy */
21+
/** @var Storage|StorageNew */
2222
protected $storage;
2323

2424
/** @var Config */
2525
protected $config;
2626

2727
/**
28-
* @param Storage|StorageLegacy $storage
28+
* @param Storage|StorageNew $storage
2929
* @param Config $config
3030
*/
3131
public function __construct($storage, Config $config)

src/DiagramGenerator/Image/Storage.php

Lines changed: 89 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,10 @@ public function __construct($cacheDirectory, $pieceThemeUrl, $boardTextureUrl)
4242
*/
4343
public function getPieceImage(Piece $piece, Config $config)
4444
{
45-
return $this->getPieceImageFromTheme($piece, $config);
46-
}
47-
48-
/**
49-
* Gets piece image from theme URLs.
50-
*
51-
* @return Image
52-
*/
53-
protected function getPieceImageFromTheme(Piece $piece, Config $config)
54-
{
55-
$themeUrls = $config->getThemeUrls();
56-
$pieceShortName = $piece->getShortName();
57-
58-
if (!isset($themeUrls[$pieceShortName])) {
59-
throw new RuntimeException(sprintf('Piece URL not found in theme for piece: %s', $pieceShortName));
60-
}
61-
62-
$pieceUrl = $themeUrls[$pieceShortName];
63-
$cacheKey = $pieceUrl;
45+
$cacheKey = implode('.', [$piece->getColor(), $piece->getKey(), $piece->getColumn(), $piece->getRow()]);
6446

6547
if (!isset($this->pieces[$cacheKey])) {
66-
$this->pieces[$cacheKey] = $this->fetchRemotePieceImageFromTheme($piece, $config);
48+
$this->pieces[$cacheKey] = $this->fetchRemotePieceImage($piece, $config);
6749
}
6850

6951
return $this->pieces[$cacheKey];
@@ -74,30 +56,25 @@ protected function getPieceImageFromTheme(Piece $piece, Config $config)
7456
*/
7557
public function getBackgroundTextureImage(Config $config)
7658
{
77-
return $this->getBackgroundTextureImageFromTheme($config);
78-
}
79-
80-
/**
81-
* Gets background texture image from theme URL.
82-
*
83-
* @return Image|null
84-
*/
85-
protected function getBackgroundTextureImageFromTheme(Config $config)
86-
{
87-
$themeUrls = $config->getThemeUrls();
88-
89-
if (!isset($themeUrls['board'])) {
59+
if (!$config->getTexture()) {
9060
return null;
9161
}
9262

93-
$boardUrl = $themeUrls['board'];
94-
$boardCachedPath = $this->getCachedTextureFilePathFromTheme($boardUrl);
63+
$boardCachedPath = $this->getCachedTextureFilePath($config);
9564

9665
try {
9766
return ImageManagerStatic::make($boardCachedPath);
9867
} catch (NotReadableException $exception) {
99-
@mkdir(dirname($boardCachedPath), 0777, true);
100-
$this->cacheImage($boardUrl, $boardCachedPath);
68+
@mkdir($this->cacheDirectory.'/board/'.$config->getTexture()->getImageUrlFolderName(), 0777, true);
69+
70+
$boardTextureUrl = str_replace(
71+
'__BOARD_TEXTURE__', $config->getTexture()->getImageUrlFolderName(), $this->boardTextureUrl
72+
);
73+
$boardTextureUrl = str_replace('__SIZE__', $config->getSize()->getCell(), $boardTextureUrl);
74+
$boardTextureUrl .= '.'.$config->getTexture()->getImageFormat();
75+
76+
$this->cacheImage($boardTextureUrl, $boardCachedPath);
77+
10178
return ImageManagerStatic::make($boardCachedPath);
10279
}
10380
}
@@ -125,58 +102,93 @@ public function getMaxPieceHeight(Fen $fen, Config $config)
125102
}
126103

127104
/**
128-
* Fetches piece image from theme URL.
105+
* In piece image is not found in local storage, passes control to self::cacheImage()
106+
*
129107
*
130108
* @return Image
131109
*/
132-
protected function fetchRemotePieceImageFromTheme(Piece $piece, Config $config)
110+
protected function fetchRemotePieceImage(Piece $piece, Config $config)
133111
{
134-
$themeUrls = $config->getThemeUrls();
135-
$pieceShortName = $piece->getShortName();
136-
137-
if (!isset($themeUrls[$pieceShortName])) {
138-
throw new RuntimeException(sprintf('Piece URL not found in theme for piece: %s', $pieceShortName));
139-
}
140-
141-
$pieceUrl = $themeUrls[$pieceShortName];
142-
$pieceCachedPath = $this->getCachedPieceFilePathFromTheme($pieceUrl, $pieceShortName);
112+
$pieceThemeName = $config->getTheme()->getName();
113+
$cellSize = $config->getSize()->getCell();
114+
$pieceCachedPath = $this->getCachedPieceFilePath($pieceThemeName, $cellSize, $piece->getShortName());
143115

144116
try {
145117
$image = ImageManagerStatic::make($pieceCachedPath);
146118
} catch (NotReadableException $exception) {
147-
$this->downloadPieceImagesFromTheme($config);
119+
$this->downloadPieceImages($config);
148120
$image = ImageManagerStatic::make($pieceCachedPath);
149121
}
150122

151123
return $image;
152124
}
153125

126+
protected function getCachedPieceFilePath($pieceThemeName, $cellSize, $piece)
127+
{
128+
return sprintf(
129+
'%s/%s/%d/%s.%s',
130+
$this->cacheDirectory,
131+
$pieceThemeName,
132+
$cellSize,
133+
$piece,
134+
Texture::IMAGE_FORMAT_PNG
135+
);
136+
}
137+
138+
protected function getCachedTextureFilePath(Config $config)
139+
{
140+
return sprintf(
141+
'%s/board/%s/%d.%s',
142+
$this->cacheDirectory,
143+
$config->getTexture()->getImageUrlFolderName(),
144+
$config->getSize()->getCell(),
145+
$config->getTexture()->getImageFormat()
146+
);
147+
}
148+
154149
/**
155-
* Downloads all piece images from theme URLs.
150+
* Fetches remove file, and stores it locally
151+
*
152+
* @param $remoteImageUrl
153+
* @param $cachedFilePath
156154
*/
157-
private function downloadPieceImagesFromTheme(Config $config)
155+
protected function cacheImage($remoteImageUrl, $cachedFilePath)
156+
{
157+
$cachedFilePathTmp = $cachedFilePath.uniqid('', true);
158+
$ch = curl_init($remoteImageUrl);
159+
$destinationFileHandle = fopen($cachedFilePathTmp, 'wb');
160+
161+
if (!$destinationFileHandle) {
162+
throw new RuntimeException(sprintf('Could not open temporary file: %s', $cachedFilePathTmp));
163+
}
164+
165+
curl_setopt($ch, CURLOPT_FILE, $destinationFileHandle);
166+
curl_setopt($ch, CURLOPT_HEADER, 0);
167+
curl_exec($ch);
168+
curl_close($ch);
169+
fclose($destinationFileHandle);
170+
171+
rename($cachedFilePathTmp, $cachedFilePath);
172+
}
173+
174+
private function downloadPieceImages(Config $config)
158175
{
159-
$themeUrls = $config->getThemeUrls();
160176
$pieces = Piece::generateAllPieces();
161177

178+
$pieceThemeName = $config->getTheme()->getName();
179+
$cellSize = $config->getSize()->getCell();
180+
@mkdir($this->cacheDirectory.'/'.$pieceThemeName.'/'.$cellSize, 0777, true);
181+
162182
$handles = [];
163183
$fileHandles = [];
164184
$multiHandle = curl_multi_init();
165185

166186
foreach ($pieces as $piece) {
167-
$pieceShortName = $piece->getShortName();
168-
169-
if (!isset($themeUrls[$pieceShortName])) {
170-
continue; // Skip pieces without URLs
171-
}
172-
173-
$pieceUrl = $themeUrls[$pieceShortName];
174-
$filePath = $this->getCachedPieceFilePathFromTheme($pieceUrl, $pieceShortName);
175-
@mkdir(dirname($filePath), 0777, true);
176-
187+
$pieceUrl = $this->generatePieceUrl($piece, $config);
188+
$handles[$piece->getShortName()] = curl_init($pieceUrl);
189+
$filePath = $this->getCachedPieceFilePath($pieceThemeName, $cellSize, $piece->getShortName());
177190
$uniqid = uniqid();
178-
$handles[$pieceShortName] = curl_init($pieceUrl);
179-
$fileHandles[$pieceShortName] = [
191+
$fileHandles[$piece->getShortName()] = [
180192
'handle' => fopen($filePath . $uniqid, 'wb'),
181193
'tmpPath' => $filePath . $uniqid,
182194
'realPath' => $filePath,
@@ -196,78 +208,26 @@ private function downloadPieceImagesFromTheme(Config $config)
196208
} while ($running > 0);
197209

198210
foreach ($fileHandles as $fileHandle) {
199-
if (isset($fileHandle['tmpPath']) && file_exists($fileHandle['tmpPath'])) {
200-
rename($fileHandle['tmpPath'], $fileHandle['realPath']);
201-
}
211+
rename($fileHandle['tmpPath'], $fileHandle['realPath']);
202212
}
203213

204214
curl_multi_close($multiHandle);
205215
}
206216

207-
/**
208-
* Gets cached piece file path for theme URLs.
209-
* Uses the URL to generate a unique cache key.
210-
*
211-
* @param string $pieceUrl The URL of the piece image
212-
* @param string $piece The piece short name (e.g., 'wp', 'bk')
213-
* @return string
214-
*/
215-
protected function getCachedPieceFilePathFromTheme($pieceUrl, $piece)
216-
{
217-
$urlHash = md5($pieceUrl);
218-
$extension = pathinfo(parse_url($pieceUrl, PHP_URL_PATH), PATHINFO_EXTENSION) ?: Texture::IMAGE_FORMAT_PNG;
219-
220-
return sprintf(
221-
'%s/theme/%s/%s.%s',
222-
$this->cacheDirectory,
223-
$urlHash,
224-
$piece,
225-
$extension
226-
);
227-
}
228-
229-
/**
230-
* Gets cached texture file path for theme URLs.
231-
* Uses the URL to generate a unique cache key.
232-
*
233-
* @param string $boardUrl The URL of the board image
234-
* @return string
235-
*/
236-
protected function getCachedTextureFilePathFromTheme($boardUrl)
217+
private function generatePieceUrl(Piece $piece, Config $config)
237218
{
238-
$urlHash = md5($boardUrl);
239-
$extension = pathinfo(parse_url($boardUrl, PHP_URL_PATH), PATHINFO_EXTENSION) ?: Texture::IMAGE_FORMAT_PNG;
240-
241-
return sprintf(
242-
'%s/board/theme/%s.%s',
243-
$this->cacheDirectory,
244-
$urlHash,
245-
$extension
219+
$pieceThemeName = $config->getTheme()->getName();
220+
$cellSize = $config->getSize()->getCell();
221+
222+
$pieceThemeUrl = strtr(
223+
$this->pieceThemeUrl,
224+
[
225+
'__PIECE_THEME__' => $pieceThemeName,
226+
'__SIZE__' => $cellSize,
227+
'__PIECE__' => $piece->getShortName(),
228+
]
246229
);
247-
}
248230

249-
/**
250-
* Fetches remove file, and stores it locally
251-
*
252-
* @param $remoteImageUrl
253-
* @param $cachedFilePath
254-
*/
255-
protected function cacheImage($remoteImageUrl, $cachedFilePath)
256-
{
257-
$cachedFilePathTmp = $cachedFilePath.uniqid('', true);
258-
$ch = curl_init($remoteImageUrl);
259-
$destinationFileHandle = fopen($cachedFilePathTmp, 'wb');
260-
261-
if (!$destinationFileHandle) {
262-
throw new RuntimeException(sprintf('Could not open temporary file: %s', $cachedFilePathTmp));
263-
}
264-
265-
curl_setopt($ch, CURLOPT_FILE, $destinationFileHandle);
266-
curl_setopt($ch, CURLOPT_HEADER, 0);
267-
curl_exec($ch);
268-
curl_close($ch);
269-
fclose($destinationFileHandle);
270-
271-
rename($cachedFilePathTmp, $cachedFilePath);
231+
return $pieceThemeUrl . ('.' . Texture::IMAGE_FORMAT_PNG);
272232
}
273233
}

0 commit comments

Comments
 (0)