Skip to content

Commit 2ab4d1e

Browse files
committed
Use IAppManager instead of OC_App
Signed-off-by: Julius Haertl <jus@bitgrid.net>
1 parent d409fe1 commit 2ab4d1e

5 files changed

Lines changed: 81 additions & 36 deletions

File tree

apps/theming/lib/IconBuilder.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use Imagick;
2727
use ImagickPixel;
28+
use OCP\App\AppPathNotFoundException;
2829

2930
class IconBuilder {
3031
/** @var ThemingDefaults */
@@ -85,8 +86,13 @@ public function getTouchIcon($app) {
8586
* @return Imagick|false
8687
*/
8788
public function renderAppIcon($app) {
88-
$appIcon = $this->util->getAppIcon($app);
89-
$appIconContent = file_get_contents($appIcon);
89+
try {
90+
$appIcon = $this->util->getAppIcon($app);
91+
$appIconContent = file_get_contents($appIcon);
92+
} catch (AppPathNotFoundException $e) {
93+
return false;
94+
}
95+
9096
if($appIconContent === false) {
9197
return false;
9298
}
@@ -158,7 +164,11 @@ public function renderAppIcon($app) {
158164
}
159165

160166
public function colorSvg($app, $image) {
161-
$imageFile = $this->util->getAppImage($app, $image);
167+
try {
168+
$imageFile = $this->util->getAppImage($app, $image);
169+
} catch (AppPathNotFoundException $e) {
170+
return false;
171+
}
162172
$svg = file_get_contents($imageFile);
163173
if ($svg !== false) {
164174
$color = $this->util->elementColor($this->themingDefaults->getMailHeaderColor());

apps/theming/lib/Util.php

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
namespace OCA\Theming;
2525

26+
use OCP\App\AppPathNotFoundException;
27+
use OCP\App\IAppManager;
2628
use OCP\IConfig;
2729
use OCP\Files\IRootFolder;
2830

@@ -34,15 +36,20 @@ class Util {
3436
/** @var IRootFolder */
3537
private $rootFolder;
3638

39+
/** @var IAppManager */
40+
private $appManager;
41+
3742
/**
3843
* Util constructor.
3944
*
4045
* @param IConfig $config
4146
* @param IRootFolder $rootFolder
47+
* @param IAppManager $appManager
4248
*/
43-
public function __construct(IConfig $config, IRootFolder $rootFolder) {
49+
public function __construct(IConfig $config, IRootFolder $rootFolder, IAppManager $appManager) {
4450
$this->config = $config;
4551
$this->rootFolder = $rootFolder;
52+
$this->appManager = $appManager;
4653
}
4754

4855
/**
@@ -108,8 +115,8 @@ public function generateRadioButton($color) {
108115
*/
109116
public function getAppIcon($app) {
110117
$app = str_replace(array('\0', '/', '\\', '..'), '', $app);
111-
$appPath = \OC_App::getAppPath($app);
112-
if ($appPath !== false) {
118+
try {
119+
$appPath = $this->appManager->getAppPath($app);
113120
$icon = $appPath . '/img/' . $app . '.svg';
114121
if (file_exists($icon)) {
115122
return $icon;
@@ -118,7 +125,8 @@ public function getAppIcon($app) {
118125
if (file_exists($icon)) {
119126
return $icon;
120127
}
121-
}
128+
} catch (AppPathNotFoundException $e) {}
129+
122130
if($this->config->getAppValue('theming', 'logoMime', '') !== '' && $this->rootFolder->nodeExists('/themedinstancelogo')) {
123131
return $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/') . '/themedinstancelogo';
124132
}
@@ -128,40 +136,45 @@ public function getAppIcon($app) {
128136
/**
129137
* @param $app string app name
130138
* @param $image string relative path to image in app folder
131-
* @return string absolute path to image
139+
* @return string|false absolute path to image
132140
*/
133141
public function getAppImage($app, $image) {
134142
$app = str_replace(array('\0', '/', '\\', '..'), '', $app);
135143
$image = str_replace(array('\0', '\\', '..'), '', $image);
136-
$appPath = \OC_App::getAppPath($app);
137-
if ($app === "core") {
138-
$icon = \OC::$SERVERROOT . '/core/img/' . $image;
139-
if (file_exists($icon)) {
140-
return $icon;
141-
}
142-
}
143-
if ($appPath !== false) {
144-
$icon = $appPath . '/img/' . $image;
145-
if (file_exists($icon)) {
146-
return $icon;
147-
}
148-
$icon = $appPath . '/img/' . $image . '.svg';
149-
if (file_exists($icon)) {
150-
return $icon;
151-
}
152-
$icon = $appPath . '/img/' . $image . '.png';
153-
if (file_exists($icon)) {
154-
return $icon;
155-
}
156-
$icon = $appPath . '/img/' . $image . '.gif';
157-
if (file_exists($icon)) {
158-
return $icon;
159-
}
160-
$icon = $appPath . '/img/' . $image . '.jpg';
144+
if ($app === "core") {
145+
$icon = \OC::$SERVERROOT . '/core/img/' . $image;
161146
if (file_exists($icon)) {
162147
return $icon;
163148
}
164149
}
150+
151+
try {
152+
$appPath = $this->appManager->getAppPath($app);
153+
} catch (AppPathNotFoundException $e) {
154+
return false;
155+
}
156+
157+
$icon = $appPath . '/img/' . $image;
158+
if (file_exists($icon)) {
159+
return $icon;
160+
}
161+
$icon = $appPath . '/img/' . $image . '.svg';
162+
if (file_exists($icon)) {
163+
return $icon;
164+
}
165+
$icon = $appPath . '/img/' . $image . '.png';
166+
if (file_exists($icon)) {
167+
return $icon;
168+
}
169+
$icon = $appPath . '/img/' . $image . '.gif';
170+
if (file_exists($icon)) {
171+
return $icon;
172+
}
173+
$icon = $appPath . '/img/' . $image . '.jpg';
174+
if (file_exists($icon)) {
175+
return $icon;
176+
}
177+
165178
return false;
166179
}
167180

apps/theming/tests/Controller/ThemingControllerTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
use OCA\Theming\Controller\ThemingController;
2828
use OCA\Theming\Util;
29+
use OCP\App\IAppManager;
2930
use OCP\AppFramework\Http;
3031
use OCP\AppFramework\Http\DataResponse;
3132
use OCP\Files\IRootFolder;
@@ -55,6 +56,8 @@ class ThemingControllerTest extends TestCase {
5556
private $rootFolder;
5657
/** @var ITempManager */
5758
private $tempManager;
59+
/** @var IAppManager */
60+
private $appManager;
5861

5962
public function setUp() {
6063
$this->request = $this->getMockBuilder('OCP\IRequest')->getMock();
@@ -66,7 +69,8 @@ public function setUp() {
6669
->getMock();
6770
$this->l10n = $this->getMockBuilder('OCP\IL10N')->getMock();
6871
$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
69-
$this->util = new Util($this->config, $this->rootFolder);
72+
$this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock();
73+
$this->util = new Util($this->config, $this->rootFolder, $this->appManager);
7074
$this->timeFactory->expects($this->any())
7175
->method('getTime')
7276
->willReturn(123);

apps/theming/tests/IconBuilderTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use OCA\Theming\IconBuilder;
2626
use OCA\Theming\ThemingDefaults;
2727
use OCA\Theming\Util;
28+
use OCP\App\IAppManager;
2829
use OCP\AppFramework\Http\NotFoundResponse;
2930
use OCP\Files\IRootFolder;
3031
use OCP\IConfig;
@@ -42,6 +43,8 @@ class IconBuilderTest extends TestCase {
4243
protected $util;
4344
/** @var IconBuilder */
4445
protected $iconBuilder;
46+
/** @var IAppManager */
47+
protected $appManager;
4548

4649
protected function setUp() {
4750
parent::setUp();
@@ -50,7 +53,8 @@ protected function setUp() {
5053
$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
5154
$this->themingDefaults = $this->getMockBuilder('OCA\Theming\ThemingDefaults')
5255
->disableOriginalConstructor()->getMock();
53-
$this->util = new Util($this->config, $this->rootFolder);
56+
$this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock();
57+
$this->util = new Util($this->config, $this->rootFolder, $this->appManager);
5458
$this->iconBuilder = new IconBuilder($this->themingDefaults, $this->util);
5559
}
5660

apps/theming/tests/UtilTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
namespace OCA\Theming\Tests;
2424

2525
use OCA\Theming\Util;
26+
use OCP\App\IAppManager;
2627
use OCP\IConfig;
2728
use OCP\Files\IRootFolder;
2829
use Test\TestCase;
@@ -35,12 +36,15 @@ class UtilTest extends TestCase {
3536
protected $config;
3637
/** @var IRootFolder */
3738
protected $rootFolder;
39+
/** @var IAppManager */
40+
protected $appManager;
3841

3942
protected function setUp() {
4043
parent::setUp();
4144
$this->config = $this->getMockBuilder('\OCP\IConfig')->getMock();
4245
$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
43-
$this->util = new Util($this->config, $this->rootFolder);
46+
$this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock();
47+
$this->util = new Util($this->config, $this->rootFolder, $this->appManager);
4448
}
4549

4650
public function testInvertTextColorLight() {
@@ -108,6 +112,10 @@ public function testGenerateRadioButtonBlack() {
108112
* @dataProvider dataGetAppIcon
109113
*/
110114
public function testGetAppIcon($app, $expected) {
115+
$this->appManager->expects($this->once())
116+
->method('getAppPath')
117+
->with($app)
118+
->willReturn(\OC_App::getAppPath($app));
111119
$icon = $this->util->getAppIcon($app);
112120
$this->assertEquals($expected, $icon);
113121
}
@@ -134,6 +142,12 @@ public function testGetAppIconThemed() {
134142
* @dataProvider dataGetAppImage
135143
*/
136144
public function testGetAppImage($app, $image, $expected) {
145+
if($app !== 'core') {
146+
$this->appManager->expects($this->once())
147+
->method('getAppPath')
148+
->with($app)
149+
->willReturn(\OC_App::getAppPath($app));
150+
}
137151
$this->assertEquals($expected, $this->util->getAppImage($app, $image));
138152
}
139153

0 commit comments

Comments
 (0)