Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
project_browser
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
project_browser
Merge requests
!749
Remove some uses of random_data
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Remove some uses of random_data
issue/project_browser-3509170:3509170-remove-the-randomdata
into
2.0.x
Overview
21
Commits
8
Pipelines
8
Changes
9
All threads resolved!
Hide all comments
Merged
Adam G-H
requested to merge
issue/project_browser-3509170:3509170-remove-the-randomdata
into
2.0.x
2 months ago
Overview
21
Commits
8
Pipelines
8
Changes
9
All threads resolved!
Hide all comments
Expand
Closes
#3509170
0
0
Merge request reports
Compare
2.0.x
version 7
48ac94d4
2 months ago
version 6
5146b418
2 months ago
version 5
d193043f
2 months ago
version 4
cecbc58a
2 months ago
version 3
04ebc276
2 months ago
version 2
a3024d29
2 months ago
version 1
21abe23c
2 months ago
2.0.x (base)
and
latest version
latest version
55dfa1af
8 commits,
2 months ago
version 7
48ac94d4
8 commits,
2 months ago
version 6
5146b418
7 commits,
2 months ago
version 5
d193043f
6 commits,
2 months ago
version 4
cecbc58a
5 commits,
2 months ago
version 3
04ebc276
4 commits,
2 months ago
version 2
a3024d29
2 commits,
2 months ago
version 1
21abe23c
1 commit,
2 months ago
9 files
+
62
−
526
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
9
Search (e.g. *.vue) (Ctrl+P)
modules/project_browser_devel/src/Plugin/ProjectBrowserSource/RandomDataPlugin.php deleted
100644 → 0
+
0
−
225
Options
<?php
namespace
Drupal\project_browser_devel\Plugin\ProjectBrowserSource
;
use
Drupal\Component\Utility\Random
;
use
Drupal\Core\Cache\CacheBackendInterface
;
use
Drupal\Core\StringTranslation\TranslatableMarkup
;
use
Drupal\Core\Url
;
use
Drupal\project_browser
\Attribute\ProjectBrowserSource
;
use
Drupal\project_browser
\Plugin\ProjectBrowserSourceBase
;
use
Drupal\project_browser
\ProjectBrowser\Filter\BooleanFilter
;
use
Drupal\project_browser
\ProjectBrowser\Project
;
use
Drupal\project_browser
\ProjectBrowser\ProjectsResultsPage
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Random data plugin. Used mostly for testing.
*/
#
[
ProjectBrowserSource
(
id
:
'random_data'
,
label
:
new
TranslatableMarkup
(
'Random data'
),
description
:
new
TranslatableMarkup
(
'Gets random project and filters information'
),
local_task
:
[],
)]
final
class
RandomDataPlugin
extends
ProjectBrowserSourceBase
{
/**
* Utility to create random data.
*
* @var \Drupal\Component\Utility\Random
*/
protected
Random
$randomGenerator
;
/**
* ProjectBrowser cache bin.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected
CacheBackendInterface
$cacheBin
;
/**
* Constructs a MockDrupalDotOrg object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_bin
* The cache bin.
*/
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
,
CacheBackendInterface
$cache_bin
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
$this
->
randomGenerator
=
new
Random
();
$this
->
cacheBin
=
$cache_bin
;
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
):
static
{
return
new
static
(
$configuration
,
$plugin_id
,
$plugin_definition
,
$container
->
get
(
'cache.project_browser'
),
);
}
/**
* Generate random IDs and labels.
*
* @param int $array_length
* Length of the array to generate.
*
* @return array
* Array of random IDs and names.
*/
protected
function
getRandomIdsAndNames
(
int
$array_length
=
4
):
array
{
$data
=
[];
for
(
$i
=
0
;
$i
<
$array_length
;
$i
++
)
{
$data
[]
=
[
'id'
=>
uniqid
(),
'name'
=>
ucwords
(
$this
->
randomGenerator
->
word
(
rand
(
6
,
10
))),
];
}
return
$data
;
}
/**
* Returns a random date.
*
* @return int
* Random timestamp.
*/
protected
function
getRandomDate
():
int
{
return
rand
(
strtotime
(
'2 years ago'
),
strtotime
(
'today'
));
}
/**
* {@inheritdoc}
*/
protected
function
getCategories
():
array
{
$stored_categories
=
$this
->
cacheBin
->
get
(
'RandomData:categories'
);
if
(
$stored_categories
)
{
$categories
=
$stored_categories
->
data
;
}
else
{
$categories
=
$this
->
getRandomIdsAndNames
(
20
);
$this
->
cacheBin
->
set
(
'RandomData:categories'
,
$categories
);
}
return
$categories
;
}
/**
* {@inheritdoc}
*/
public
function
getFilterDefinitions
():
array
{
$filters
=
parent
::
getFilterDefinitions
();
$filters
[
'security_advisory_coverage'
]
=
new
BooleanFilter
(
TRUE
,
$this
->
t
(
'Show projects covered by a security policy'
),
$this
->
t
(
'Show all'
),
$this
->
t
(
'Security advisory coverage'
),
NULL
,
);
$filters
[
'maintenance_status'
]
=
new
BooleanFilter
(
TRUE
,
$this
->
t
(
'Show actively maintained projects'
),
$this
->
t
(
'Show all'
),
$this
->
t
(
'Maintenance status'
),
NULL
,
);
$filters
[
'development_status'
]
=
new
BooleanFilter
(
FALSE
,
$this
->
t
(
'Show projects under active development'
),
$this
->
t
(
'Show all'
),
$this
->
t
(
'Development status'
),
NULL
,
);
return
$filters
;
}
/**
* {@inheritdoc}
*/
public
function
getProjects
(
array
$query
=
[])
:
ProjectsResultsPage
{
$projects
=
$this
->
getProjectData
();
// Filter by project machine name.
if
(
!
empty
(
$query
[
'machine_name'
]))
{
$projects
=
array_filter
(
$projects
,
fn
(
Project
$project
):
bool
=>
$project
->
machineName
===
$query
[
'machine_name'
]);
}
// Filter by categories.
if
(
!
empty
(
$query
[
'categories'
]))
{
$projects
=
array_filter
(
$projects
,
fn
(
Project
$project
):
bool
=>
empty
(
array_intersect
(
array_column
(
$project
->
categories
,
'id'
),
explode
(
','
,
$query
[
'categories'
]))));
}
// Filter by search text.
if
(
!
empty
(
$query
[
'search'
]))
{
$projects
=
array_filter
(
$projects
,
fn
(
Project
$project
):
bool
=>
stripos
(
$project
->
title
,
$query
[
'search'
])
!==
FALSE
);
}
return
$this
->
createResultsPage
(
$projects
);
}
/**
* Gets the project data from cache if available, or builds it if not.
*
* @return \Drupal\project_browser\ProjectBrowser\Project[]
* An array of projects.
*/
private
function
getProjectData
():
array
{
$stored_projects
=
$this
->
cacheBin
->
get
(
'RandomData:projects'
);
if
(
$stored_projects
)
{
return
$stored_projects
->
data
;
}
$projects
=
[];
$number_of_projects
=
rand
(
16
,
36
);
$categories
=
$this
->
getCategories
();
$broken_image
=
'https://image.not/found'
.
uniqid
()
.
'.jpg'
;
$good_image
=
'https://picsum.photos/600/400'
;
for
(
$i
=
0
;
$i
<
$number_of_projects
;
$i
++
)
{
$machine_name
=
strtolower
(
$this
->
randomGenerator
->
word
(
10
));
$project_images
=
[];
if
(
$i
!==
0
)
{
$project_images
[]
=
[
'file'
=>
Url
::
fromUri
(
str_replace
(
'4'
,
'5'
,
$good_image
)),
'alt'
=>
$machine_name
.
' something'
,
];
$project_images
[]
=
[
'file'
=>
Url
::
fromUri
(
str_replace
(
'4'
,
'6'
,
$good_image
)),
'alt'
=>
$machine_name
.
' another thing'
,
];
}
$projects
[]
=
new
Project
(
logo
:
Url
::
fromUri
(
$i
%
3
?
$good_image
:
$broken_image
),
isCompatible
:
(
bool
)
(
$i
/
4
),
isMaintained
:
(
bool
)
rand
(
0
,
1
),
isCovered
:
(
bool
)
rand
(
0
,
1
),
projectUsageTotal
:
rand
(
0
,
100000
),
machineName
:
$machine_name
,
body
:
[
'summary'
=>
$this
->
randomGenerator
->
paragraphs
(
1
),
'value'
=>
$this
->
randomGenerator
->
paragraphs
(
5
),
],
title
:
ucwords
(
$machine_name
),
packageName
:
'random/'
.
$machine_name
,
categories
:
[
$categories
[
array_rand
(
$categories
)]],
images
:
$project_images
,
id
:
$machine_name
,
);
}
$this
->
cacheBin
->
set
(
'RandomData:projects'
,
$projects
);
return
$projects
;
}
}
Loading