Skip to content

Commit 56e638f

Browse files
committed
Initial fix for containing the deletion Actions
These Controller actions are much bigger than they should be, and so the main logic should go to a Handler. Initial implementation for the 1st one (untelsted)
1 parent c1b4154 commit 56e638f

File tree

3 files changed

+86
-41
lines changed

3 files changed

+86
-41
lines changed

Symfony/src/Codebender/CompilerBundle/Controller/DefaultController.php

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Codebender\CompilerBundle\Handler\CompilerHandler;
18+
use Codebender\CompilerBundle\Handler\DeletionHandler;
1819

1920
class DefaultController extends Controller
2021
{
@@ -78,50 +79,19 @@ public function deleteAllObjectsAction($auth_key, $version)
7879
if ($version != "v1")
7980
return new Response(json_encode(array("success" => false, "step" => 0, "message" => "Invalid API version.")));
8081

81-
$tempDir = $this->container->getParameter('temp_dir');
82-
$objectFilesDir = $this->container->getParameter('objdir');
83-
$fileCount = 0;
84-
$undeletedFiles = "";
85-
$deletionStats = array("success_dot_a" => 0,
86-
"failure_dot_a" => 0,
87-
"success_dot_o" => 0,
88-
"failure_dot_o" => 0,
89-
"success_dot_d" => 0,
90-
"failure_dot_d" => 0,
91-
"success_dot_LOCK" => 0,
92-
"failure_dot_LOCK" => 0);
82+
//Get the compiler service
83+
/** @var DeletionHandler $deleter */
84+
$deleter = $this->get('deletion_handler');
9385

94-
if ($handle = opendir("$tempDir/$objectFilesDir"))
95-
{
86+
$deleter->deleteAllObjects($success, $fileCount, $deletionStats, $undeletedFiles);
9687

97-
while (false !== ($entry = readdir($handle)))
98-
{
99-
if ($entry != "." && $entry != ".." && $entry != ".DS_Store")
100-
{
101-
$fileCount++;
102-
$extension = pathinfo($entry, PATHINFO_EXTENSION);
103-
104-
if (!in_array($extension, array("a", "o", "d", "LOCK")))
105-
continue;
106-
107-
if (@unlink("$tempDir/$objectFilesDir/$entry") === false)
108-
{
109-
$deletionStats["failure_dot_$extension"]++;
110-
$undeletedFiles .= $entry."\n";
111-
}
112-
else
113-
$deletionStats["success_dot_$extension"]++;
114-
}
115-
}
116-
closedir($handle);
117-
}
118-
else
88+
if ($success === false)
11989
return new Response(json_encode(array("success" => false, "step" => 0, "message" => "Failed to access object files directory.")));
120-
121-
return new Response(json_encode(array_merge(array("success" => true,
122-
"message" => "Object files deletion complete. Found $fileCount files."),
123-
$deletionStats,
124-
array("Files not deleted" => $undeletedFiles))));
90+
else
91+
return new Response(json_encode(array_merge(array("success" => true,
92+
"message" => "Object files deletion complete. Found $fileCount files."),
93+
$deletionStats,
94+
array("Files not deleted" => $undeletedFiles))));
12595
}
12696

12797
public function deleteSpecificObjectsAction($auth_key, $version, $option, $to_delete)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: iluvatar
5+
* Date: 9/1/15
6+
* Time: 4:44 AM
7+
*
8+
* \file
9+
* \brief Functions used to delete caches (object files) from past compilations.
10+
*
11+
* \author Vasilis Georgitzikis
12+
*
13+
* \copyright (c) 2012-2015, The Codebender Development Team
14+
* \copyright Licensed under the Simplified BSD License
15+
*/
16+
17+
namespace Codebender\CompilerBundle\Handler;
18+
19+
use Symfony\Bridge\Monolog\Logger;
20+
21+
class DeletionHandler
22+
{
23+
private $compiler_logger;
24+
private $object_directory;
25+
26+
function __construct(Logger $logger, $objdir)
27+
{
28+
$this->compiler_logger = $logger;
29+
$this->object_directory = $objdir;
30+
}
31+
32+
function deleteAllObjects(&$success, &$fileCount, &$deletionStats, &$undeletedFiles)
33+
{
34+
$success = false;
35+
$fileCount = 0;
36+
$undeletedFiles = "";
37+
$deletionStats = array("success_dot_a" => 0,
38+
"failure_dot_a" => 0,
39+
"success_dot_o" => 0,
40+
"failure_dot_o" => 0,
41+
"success_dot_d" => 0,
42+
"failure_dot_d" => 0,
43+
"success_dot_LOCK" => 0,
44+
"failure_dot_LOCK" => 0);
45+
46+
if ($handle = opendir($this->object_directory))
47+
{
48+
$success = true;
49+
50+
while (false !== ($entry = readdir($handle)))
51+
{
52+
if ($entry != "." && $entry != ".." && $entry != ".DS_Store")
53+
{
54+
$fileCount++;
55+
$extension = pathinfo($entry, PATHINFO_EXTENSION);
56+
57+
if (!in_array($extension, array("a", "o", "d", "LOCK")))
58+
continue;
59+
60+
if (@unlink($this->object_directory."/$entry") === false)
61+
{
62+
$deletionStats["failure_dot_$extension"]++;
63+
$undeletedFiles .= $entry."\n";
64+
}
65+
else
66+
$deletionStats["success_dot_$extension"]++;
67+
}
68+
}
69+
closedir($handle);
70+
}
71+
}
72+
}

Symfony/src/Codebender/CompilerBundle/Resources/config/services.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ services:
1818
class: "%preprocessing_handler.class%"
1919
postprocessing_handler:
2020
class: "%postprocessing_handler.class%"
21+
compiler_handler:
22+
class: "%deletion_handler.class%"
23+
arguments: ["@compiler_logger", "%temp_dir%/%objdir%"]
2124
compiler_logger:
2225
class: Symfony\Bridge\Monolog\Logger
2326
arguments: [cmplr_log]

0 commit comments

Comments
 (0)