Skip to content

Commit ae52a12

Browse files
committed
Re-work independent 40 branch packages using sub dir instead of prefix
1 parent 371358e commit ae52a12

File tree

4 files changed

+38
-30
lines changed

4 files changed

+38
-30
lines changed

composer/repository.php

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ public function set_release(array $packages, $composer_json, $download_url, $con
153153
*
154154
* @param string $name File name
155155
* @param array $packages Packages to be dumped
156+
* @param string $subdir Optional subdirectory
156157
*/
157-
public function dump_include($name, array $packages)
158+
public function dump_include($name, array $packages, $subdir = '')
158159
{
159160
foreach ($packages as $package_name => $versions)
160161
{
@@ -164,24 +165,27 @@ public function dump_include($name, array $packages)
164165
array('packages' => $packages),
165166
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
166167
);
167-
$file = $this->build_dir . $name;
168+
$target_dir = $this->build_dir . $subdir;
169+
if ($subdir && !$this->fs->exists($target_dir))
170+
{
171+
$this->fs->mkdir($target_dir);
172+
}
173+
$file = $target_dir . $name;
168174
$this->fs->dumpFile($file, $packages);
169175
}
170176

171177
/**
172178
* Get include files.
173179
*
174-
* @param string $prefix Optional prefix to filter files
175180
* @return Finder
176181
*/
177-
protected function get_include_files($prefix = '')
182+
protected function get_include_files()
178183
{
179184
$finder = new Finder;
180-
$pattern = $prefix ? '/^packages\-' . preg_quote($prefix, '/') . '[a-z]+\-\d+\.json$/' : '/^packages\-[a-z]+\-\d+\.json$/';
181185
$finder
182186
->files()
183187
->depth('== 0')
184-
->name($pattern)
188+
->name('/^packages\-[a-z]+\-\d+\.json$/')
185189
->in($this->build_dir)
186190
;
187191

@@ -197,23 +201,29 @@ protected function build_parents()
197201
$branches = ext::get_filtered_repository_branches();
198202
foreach ($branches as $branch)
199203
{
200-
$this->build_parent_structure($branch . '-');
204+
$this->build_parent_structure($branch . '/');
201205
}
202206
}
203207

204208
/**
205-
* Build parent structure for given prefix
209+
* Build parent structure for given subdirectory
206210
*
207-
* @param string $prefix Optional prefix for files
211+
* @param string $subdir Optional subdirectory
208212
*/
209-
protected function build_parent_structure($prefix = '')
213+
protected function build_parent_structure($subdir = '')
210214
{
211-
$includes = $this->get_include_files($prefix);
215+
$includes = $this->get_include_files();
212216
$parent = $types = array();
217+
$target_dir = $this->build_dir . $subdir;
218+
219+
if ($subdir && !$this->fs->exists($target_dir))
220+
{
221+
$this->fs->mkdir($target_dir);
222+
}
213223

214224
foreach ($includes as $file)
215225
{
216-
$type = $this->get_include_type($file->getFilename(), $prefix);
226+
$type = $this->get_include_type($file->getFilename());
217227

218228
if ($type)
219229
{
@@ -229,8 +239,8 @@ protected function build_parent_structure($prefix = '')
229239

230240
foreach ($types as $type => $includes)
231241
{
232-
$type_filename = $prefix ? 'packages-' . $prefix . $type . '.json' : 'packages-' . $type . '.json';
233-
$type_filepath = $this->build_dir . $type_filename;
242+
$type_filename = 'packages-' . $type . '.json';
243+
$type_filepath = $target_dir . $type_filename;
234244
$contents = json_encode(array('includes' => $includes));
235245
$this->fs->dumpFile($type_filepath, $contents);
236246

@@ -240,26 +250,23 @@ protected function build_parent_structure($prefix = '')
240250
}
241251
if (!empty($parent))
242252
{
243-
$main_filename = $prefix ? 'packages-' . rtrim($prefix, '-') . '.json' : 'packages.json';
244253
$contents = json_encode(array('includes' => $parent));
245-
$this->fs->dumpFile($this->build_dir . $main_filename, $contents);
254+
$this->fs->dumpFile($target_dir . 'packages.json', $contents);
246255
}
247256
}
248257

249258
/**
250259
* Get contrib type name from include file name.
251260
*
252261
* @param string $filename
253-
* @param string $prefix Optional prefix to match
254262
* @return bool|string Returns contrib type name or false if not a valid filename
255263
*/
256-
protected function get_include_type($filename, $prefix = '')
264+
protected function get_include_type($filename)
257265
{
258266
$filename = utf8_basename($filename);
259267
$match = array();
260-
$pattern = $prefix ? '/^packages\-' . preg_quote($prefix, '/') . '([a-z]+)\-\d+\.json$/' : '/^packages\-([a-z]+)\-\d+\.json$/';
261268

262-
if (preg_match($pattern, $filename, $match))
269+
if (preg_match('/^packages\-([a-z]+)\-\d+\.json$/', $filename, $match))
263270
{
264271
return $match[1];
265272
}

config/routes/general.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ phpbb.titania.contribution.id:
6666

6767
phpbb.titania.composer.40.files:
6868
path: /composer/40/{filename}.json
69-
defaults: { _controller: phpbb.titania.controller.composer:serve_file }
69+
defaults: { _controller: phpbb.titania.controller.composer:serve_file, subdirectory: '40/' }
7070

7171
phpbb.titania.composer.40:
7272
path: /composer/40/
73-
defaults: { _controller: phpbb.titania.controller.composer:serve_file, filename: packages-40 }
73+
defaults: { _controller: phpbb.titania.controller.composer:serve_file, filename: packages, subdirectory: '40/' }
7474

7575
phpbb.titania.composer:
7676
path: /composer/{filename}.json

controller/composer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,21 @@ public function __construct(\phpbb\user $user, \phpbb\titania\controller\helper
4444
* Serve composer's files.
4545
*
4646
* @param string $filename Filename to serve (without extension)
47+
* @param string $subdirectory Optional subdirectory
4748
* @return \Symfony\Component\HttpFoundation\Response
4849
*/
49-
public function serve_file($filename)
50+
public function serve_file($filename, $subdirectory = '')
5051
{
51-
if (strpos($filename, '..') !== false)
52+
if (strpos($filename, '..') !== false || strpos($subdirectory, '..') !== false)
5253
{
5354
throw new http_exception(404, 'NO_PAGE_FOUND');
5455
}
5556

56-
$filename = $this->titania_root_path . 'composer_packages/prod/' . $filename . '.json';
57+
$filepath = $this->titania_root_path . 'composer_packages/prod/' . $subdirectory . $filename . '.json';
5758

5859
try
5960
{
60-
return new BinaryFileResponse($filename, 200);
61+
return new BinaryFileResponse($filepath, 200);
6162
}
6263
catch (\Exception $e)
6364
{

manage/tool/composer/rebuild_repo.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,13 @@ public function run($from_file = false, $force = false, $progress = null)
322322
* @param string $type Contrib type name
323323
* @param int $group Group id
324324
* @param array $packages Packages
325-
* @param string $suffix Optional suffix for filename
325+
* @param int $branch Optional branch number for subdirectory
326326
*/
327-
protected function dump_include($type, $group, array $packages, $prefix = '')
327+
protected function dump_include($type, $group, array $packages, $branch = null)
328328
{
329329
$type_name = $this->types->get($type)->name;
330-
$filename = $prefix ? "packages-$prefix-$type_name-$group.json" : "packages-$type_name-$group.json";
331-
$this->repo->dump_include($filename, $packages);
330+
$subdir = $branch ? $branch . '/' : '';
331+
$this->repo->dump_include("packages-$type_name-$group.json", $packages, $subdir);
332332
}
333333

334334
/**

0 commit comments

Comments
 (0)