Remove product literal strings in "pht()", part 18
[phabricator.git] / src / applications / celerity / CeleritySpriteGenerator.php
blob8c09bbc59f2fceef5c16e9818af59674a213441b
1 <?php
3 final class CeleritySpriteGenerator extends Phobject {
5 public function buildTokenSheet() {
6 $icons = $this->getDirectoryList('tokens_1x');
7 $scales = array(
8 '1x' => 1,
9 '2x' => 2,
11 $template = id(new PhutilSprite())
12 ->setSourceSize(18, 18);
14 $sprites = array();
15 $prefix = 'tokens_';
16 foreach ($icons as $icon) {
17 $sprite = id(clone $template)
18 ->setName('tokens-'.$icon)
19 ->setTargetCSS('.tokens-'.$icon);
21 foreach ($scales as $scale_key => $scale) {
22 $path = $this->getPath($prefix.$scale_key.'/'.$icon.'.png');
23 $sprite->setSourceFile($path, $scale);
25 $sprites[] = $sprite;
28 $sheet = $this->buildSheet('tokens', true);
29 $sheet->setScales($scales);
30 foreach ($sprites as $sprite) {
31 $sheet->addSprite($sprite);
34 return $sheet;
37 public function buildLoginSheet() {
38 $icons = $this->getDirectoryList('login_1x');
39 $scales = array(
40 '1x' => 1,
41 '2x' => 2,
43 $template = id(new PhutilSprite())
44 ->setSourceSize(28, 28);
46 $sprites = array();
47 $prefix = 'login_';
48 foreach ($icons as $icon) {
49 $sprite = id(clone $template)
50 ->setName('login-'.$icon)
51 ->setTargetCSS('.login-'.$icon);
53 foreach ($scales as $scale_key => $scale) {
54 $path = $this->getPath($prefix.$scale_key.'/'.$icon.'.png');
55 $sprite->setSourceFile($path, $scale);
57 $sprites[] = $sprite;
60 $sheet = $this->buildSheet('login', true);
61 $sheet->setScales($scales);
62 foreach ($sprites as $sprite) {
63 $sheet->addSprite($sprite);
66 return $sheet;
69 private function getPath($to_path = null) {
70 $root = dirname(phutil_get_library_root('phabricator'));
71 return $root.'/resources/sprite/'.$to_path;
74 private function getDirectoryList($dir) {
75 $path = $this->getPath($dir);
77 $result = array();
79 $images = Filesystem::listDirectory($path, $include_hidden = false);
80 foreach ($images as $image) {
81 if (!preg_match('/\.png$/', $image)) {
82 throw new Exception(
83 pht(
84 "Expected file '%s' in '%s' to be a sprite source ending in '%s'.",
85 $image,
86 $path,
87 '.png'));
89 $result[] = substr($image, 0, -4);
92 return $result;
95 private function buildSheet(
96 $name,
97 $has_retina,
98 $type = null,
99 $extra_css = '') {
101 $sheet = new PhutilSpriteSheet();
103 $at = '@';
105 switch ($type) {
106 case PhutilSpriteSheet::TYPE_STANDARD:
107 default:
108 $type = PhutilSpriteSheet::TYPE_STANDARD;
109 $repeat_rule = 'no-repeat';
110 break;
111 case PhutilSpriteSheet::TYPE_REPEAT_X:
112 $repeat_rule = 'repeat-x';
113 break;
114 case PhutilSpriteSheet::TYPE_REPEAT_Y:
115 $repeat_rule = 'repeat-y';
116 break;
119 $retina_rules = null;
120 if ($has_retina) {
121 $retina_rules = <<<EOCSS
122 @media
123 only screen and (min-device-pixel-ratio: 1.5),
124 only screen and (-webkit-min-device-pixel-ratio: 1.5),
125 only screen and (min-resolution: 1.5dppx) {
126 .sprite-{$name}{$extra_css} {
127 background-image: url(/rsrc/image/sprite-{$name}-X2.png);
128 background-size: {X}px {Y}px;
131 EOCSS;
134 $sheet->setSheetType($type);
135 $sheet->setCSSHeader(<<<EOCSS
137 * @provides sprite-{$name}-css
138 * {$at}generated
141 .sprite-{$name}{$extra_css} {
142 background-image: url(/rsrc/image/sprite-{$name}.png);
143 background-repeat: {$repeat_rule};
146 {$retina_rules}
148 EOCSS
151 return $sheet;