Skip to content

Commit e429db8

Browse files
committed
IMPROVED : Refactored resources management
1 parent 6d96e9e commit e429db8

File tree

8 files changed

+24
-89
lines changed

8 files changed

+24
-89
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
## 0.2.0 - Not Released
44
### Features
55
- MSProjectExchange Reader - @Progi1984 GH-4
6+
- MSProjectExchange Writer - @Progi1984 GH-2
67
### Bugfix
78

89
### Miscellaneous
10+
- Refactored resources management
911

1012

1113
## 0.1.0 - 2014-08-08

samples/Sample_01_Simple.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@
3535
$objTask1->setEndDate('03-01-2012');
3636
$objTask1->setProgress(0.5);
3737
$objTask1->addResource($objRes1);
38-
$objTask1Res = $objTask1->getResources();
3938
echo 'Resources "Start of the project"'.EOL;
40-
foreach ($objTask1Res as $res){
41-
echo ' > '.$objPHPProject->getResource($res)->getTitle().EOL;;
39+
foreach ($objTask1->getResources() as $oResource){
40+
echo ' > '.$oResource->getTitle().EOL;
4241
}
4342

4443
$objTask2 = $objPHPProject->createTask();
@@ -51,10 +50,11 @@
5150
$objTask21->setProgress(1);
5251
$objTask21->addResource($objRes2);
5352
$objTask21->addResource($objRes1);
54-
$objTask21Res = $objTask21->getResources();
53+
$objTask21->addResource($objRes1);
54+
5555
echo 'Resources "Analysis Code"'.EOL;
56-
foreach ($objTask21Res as $res){
57-
echo ' > '.$objPHPProject->getResource($res)->getTitle().EOL;;
56+
foreach ($objTask21->getResources() as $oResource){
57+
echo ' > '.$oResource->getTitle().EOL;;
5858
}
5959

6060
$objTask22 = $objTask2->createTask();

samples/Sample_02_ReadGanttProject.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,14 @@
2727

2828
// Ressources
2929
echo date('H:i:s') . ' Get ressources'.EOL;
30-
$oResources = $objPHPProject->getAllResources();
31-
foreach ($oResources as $item){
32-
echo 'Resource : '.$item->getTitle().EOL;
30+
foreach ($objPHPProject->getAllResources() as $oResource){
31+
echo 'Resource : '.$oResource->getTitle().EOL;
3332
}
3433
echo EOL;
3534

3635
// Tasks
3736
echo date('H:i:s') . ' Get tasks'.EOL;
38-
$arrTasks = $objPHPProject->getAllTasks();
39-
40-
foreach ($arrTasks as $oTask){
37+
foreach ($objPHPProject->getAllTasks() as $oTask){
4138
echoTask($objPHPProject, $oTask);
4239
}
4340

samples/Sample_Header.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Autoloader::register();
1616

1717
// Set writers
18-
$writers = array('GanttProject' => 'gan'/*, 'MSProjectExchange' => 'mpx'*/);
18+
$writers = array('GanttProject' => 'gan', 'MsProjectMPX' => 'mpx');
1919

2020
// Return to the caller script when runs by CLI
2121
if (CLI) {
@@ -115,8 +115,8 @@ function echoTask($oPHPProject, $oTask, $level = 0) {
115115
echo ' '.str_repeat('>', 2 * ($level + 1)).' Resources : '.EOL;
116116
$oTaskResources = $oTask->getResources();
117117
if(!empty($oTaskResources)){
118-
foreach ($oTaskResources as $itemRes){
119-
echo ' '.str_repeat('>', 2 * ($level + 2)).' Resource : '.$oPHPProject->getResourceFromIndex($itemRes)->getTitle().EOL;
118+
foreach ($oTaskResources as $oResource){
119+
echo ' '.str_repeat('>', 2 * ($level + 2)).' Resource : '.$oResource->getTitle().EOL;
120120
}
121121
}
122122
echo EOL;

src/PhpProject/PhpProject.php

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function getAllResources()
168168
/**
169169
* Get active resource
170170
*
171-
* @return Resource
171+
* @return Resource|null
172172
*/
173173
public function getActiveResource()
174174
{
@@ -178,22 +178,6 @@ public function getActiveResource()
178178
return null;
179179
}
180180

181-
/**
182-
* Get resource by index
183-
*
184-
* @param int $pIndex Resource index
185-
* @return Resource
186-
* @throws \Exception
187-
*/
188-
public function getResource($pIndex = 0)
189-
{
190-
if (!isset($this->resourceCollection[$pIndex])) {
191-
throw new \Exception('Resource index is out of bounds.');
192-
} else {
193-
return $this->resourceCollection[$pIndex];
194-
}
195-
}
196-
197181
/**
198182
* Get resource from index
199183
*
@@ -260,22 +244,6 @@ public function getActiveTask()
260244
return null;
261245
}
262246

263-
/**
264-
* Get task by index
265-
*
266-
* @param int $pIndex Task index
267-
* @return Task
268-
* @throws \Exception
269-
*/
270-
public function getTask($pIndex = 0)
271-
{
272-
if (!isset($this->taskCollection[$pIndex])) {
273-
throw new \Exception('Task index is out of bounds.');
274-
} else {
275-
return $this->taskCollection[$pIndex];
276-
}
277-
}
278-
279247
/**
280248
* Get task from index
281249
*

src/PhpProject/Task.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,18 +253,18 @@ public function setIndex($value)
253253
* Add a resource used by the current task
254254
* @param PHPProject_Resource $pResource
255255
*/
256-
public function addResource(Resource $pResource)
256+
public function addResource(Resource $oResource)
257257
{
258-
if (array_search($pResource->getIndex(), $this->resourceCollection) === false) {
259-
$this->resourceCollection[] = $pResource->getIndex();
258+
if (!in_array($oResource, $this->resourceCollection)) {
259+
$this->resourceCollection[] = &$oResource;
260260
}
261261
return $this;
262262
}
263263

264264
/**
265265
* Returns a collection of all resources used by the task
266266
*
267-
* @return integer[]
267+
* @return Resource[]
268268
*/
269269
public function getResources()
270270
{

src/PhpProject/Writer/GanttProject.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ public function save ($pFilename)
179179
$oXML->endElement();
180180

181181
// task
182-
$arrTasks = $this->phpProject->getAllTasks();
183182
$iTaskIndex = 0;
184-
foreach ($arrTasks as $oTask) {
183+
foreach ($this->phpProject->getAllTasks() as $oTask) {
185184
$iTaskIndex = $this->writeTask($oXML, $oTask, $iTaskIndex);
186185
}
187186

@@ -192,11 +191,8 @@ public function save ($pFilename)
192191
$oXML->startElement('resources');
193192

194193
// resource
195-
$arrResources = $this->phpProject->getAllResources();
196-
foreach ($arrResources as $oResource) {
197-
if ($oResource instanceof \PhpOffice\PhpProject\Resource) {
198-
$this->writeResource($oXML, $oResource);
199-
}
194+
foreach ($this->phpProject->getAllResources() as $oResource) {
195+
$this->writeResource($oXML, $oResource);
200196
}
201197

202198
// >resources
@@ -328,10 +324,9 @@ private function writeTask (XMLWriter $oXML, Task $oTask, $iNbTasks)
328324

329325
// Resources Allocations
330326
if ($oTask->getResourceCount() > 0) {
331-
$arrResources = $oTask->getResources();
332-
foreach ($arrResources as $resourceIdx) {
327+
foreach ($oTask->getResources() as $oResource) {
333328
$itmAllocation = array();
334-
$itmAllocation['id_res'] = $resourceIdx;
329+
$itmAllocation['id_res'] = $oResource->getIndex();
335330
$itmAllocation['id_task'] = $iNbTasks;
336331
$this->arrAllocations[] = $itmAllocation;
337332
}

tests/PhpProject/Tests/PhpProjectTest.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ public function testResource()
7272
$this->assertCount(1, $object->getAllResources());
7373
$this->assertInternalType('array', $object->getAllResources());
7474
$this->assertInstanceOf('PhpOffice\\PhpProject\\Resource', $object->getActiveResource());
75-
// Get Resource
76-
$this->assertInstanceOf('PhpOffice\\PhpProject\\Resource', $object->getResource());
77-
$this->assertInstanceOf('PhpOffice\\PhpProject\\Resource', $object->getResource(0));
7875
}
7976

8077
public function testResourceFromIndex()
@@ -89,16 +86,6 @@ public function testResourceFromIndex()
8986
$this->assertNull($object->getResourceFromIndex(1));
9087
}
9188

92-
/**
93-
* @expectedException \Exception
94-
* @expectedExceptionMessage Resource index is out of bounds.
95-
*/
96-
public function testResourceException()
97-
{
98-
$object = new PhpProject();
99-
$object->getResource();
100-
}
101-
10289
public function testTask()
10390
{
10491
$object = new PhpProject();
@@ -123,10 +110,6 @@ public function testTask()
123110
$this->assertEquals(1, $object->getActiveTaskIndex());
124111
$this->assertInternalType('array', $object->getAllTasks());
125112
$this->assertInstanceOf('PhpOffice\\PhpProject\\Task', $object->getActiveTask());
126-
// Get Task
127-
$this->assertInstanceOf('PhpOffice\\PhpProject\\Task', $object->getTask());
128-
$this->assertInstanceOf('PhpOffice\\PhpProject\\Task', $object->getTask(0));
129-
$this->assertInstanceOf('PhpOffice\\PhpProject\\Task', $object->getTask(1));
130113
// Active Task
131114
$this->assertInstanceOf('PhpOffice\\PhpProject\\Task', $object->setActiveTaskIndex(0));
132115
$this->assertEquals(0, $object->getActiveTaskIndex());
@@ -155,16 +138,6 @@ public function testTaskFromIndex()
155138
$this->assertNull($object->getTaskFromIndex(1));
156139
}
157140

158-
/**
159-
* @expectedException \Exception
160-
* @expectedExceptionMessage Task index is out of bounds.
161-
*/
162-
public function testTaskException()
163-
{
164-
$object = new PhpProject();
165-
$object->getTask();
166-
}
167-
168141
/**
169142
* @expectedException \Exception
170143
* @expectedExceptionMessage Task index is out of bounds.

0 commit comments

Comments
 (0)