Skip to content

Commit c7e6760

Browse files
authored
Support v4 to v5
1 parent a0fe015 commit c7e6760

File tree

5 files changed

+1054
-56
lines changed

5 files changed

+1054
-56
lines changed

adviser.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
class Adviser
4+
{
5+
private $path;
6+
private $phalconClasses;
7+
private $logFile;
8+
9+
public function __construct(array $phalconClasses, string $path, string $logFile = "upgradeLog.txt") {
10+
$this->phalconClasses = $phalconClasses;
11+
$this->path = $path;
12+
$this->logFile = $logFile;
13+
}
14+
15+
public function createLogAction()
16+
{
17+
if (empty($this->path)) {
18+
die("Please provide the path to the Application");
19+
}
20+
21+
if (is_file($this->path)) {
22+
die($this->logPhalconClassesState($this->path));
23+
}
24+
25+
$phpFiles = [];
26+
27+
$this->getPhpFiles($this->path, $phpFiles);
28+
29+
$log = $this->processPhpFiles($phpFiles);
30+
31+
file_put_contents($this->logFile, $log);
32+
33+
echo "Check '$this->logFile' to review the necessary changes to upgrade\n";
34+
35+
}
36+
37+
public function travelPhpFiles(string $path): array
38+
{
39+
$files = [];
40+
41+
$this->getPhpFiles($path, $files);
42+
43+
if (count($files) == 0) {
44+
die("No PHP files found in folder $path");
45+
}
46+
47+
return $files;
48+
}
49+
50+
private function processPhpFiles(array $files): string
51+
{
52+
$log = "";
53+
54+
foreach ($files as $file) {
55+
$log .= $this->logPhalconClassesState($file);
56+
}
57+
58+
return $log;
59+
}
60+
61+
private function getPhpFiles(string $dir, array &$phpFiles = [])
62+
{
63+
$files = scandir($dir);
64+
65+
foreach ($files as $key => $value) {
66+
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
67+
68+
if (is_file($path)) {
69+
if (pathinfo($path, PATHINFO_EXTENSION) === "php") {
70+
$phpFiles[] = $path;
71+
}
72+
} else if ($value != "." && $value != ".." && $value != "vendor" && $value != ".git") {
73+
$this->getPhpFiles($path, $phpFiles);
74+
if (pathinfo($path, PATHINFO_EXTENSION) === "php") {
75+
$phpFiles[] = $path;
76+
}
77+
}
78+
}
79+
}
80+
81+
private function logPhalconClassesState(string $file): string
82+
{
83+
try {
84+
$fn = fopen($file, "r");
85+
} catch (exception $e) {
86+
return "Error opening $file => " . $e->getMessage() . ";\n";
87+
}
88+
89+
$classes = [];
90+
91+
while(! feof($fn)) {
92+
if (preg_match("/Phalcon\\\([^\s;(]+)/", fgets($fn), $match)) {
93+
$classes[] = $match[0];
94+
}
95+
}
96+
97+
fclose($fn);
98+
99+
if (count($classes) == 0) {
100+
return $file . ":\nNo Phalcon classes found\n\n";
101+
}
102+
103+
return $file . ":\n" . $this->checkClassState($classes) . "\n\n";
104+
}
105+
106+
private function checkClassState(array $classes):string
107+
{
108+
$log = "";
109+
110+
foreach ($classes as $class) {
111+
if (isset($this->phalconClasses[$class])) {
112+
$log .= $class . " => " . $this->phalconClasses[$class] . "\n";
113+
} elseif (strpos($class, "::") > 0) {
114+
$log .= $class . " => Check possible changes in constant\n";
115+
} else {
116+
$log .= $class . " => No changes\n";
117+
}
118+
}
119+
120+
return $log;
121+
}
122+
}

cli.php

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,36 @@
1-
<?php
2-
3-
use Phalcon\Di\FactoryDefault\Cli as CliDI;
4-
use Phalcon\Cli\Console;
5-
use Phalcon\Loader;
6-
7-
$phalconVersion = Phalcon\Version::get();
8-
9-
if ($phalconVersion[0] <> "4") {
10-
exit("The Upgrade Adviser only works with Phalcon 4.x");
11-
}
12-
13-
require_once('library/consts/Phalcon3to4.php');
14-
15-
$di = new CliDI;
16-
17-
$loader = new Loader();
18-
$loader->registerDirs(
19-
[
20-
__DIR__ . '/tasks'
21-
]
22-
);
23-
$loader->registerClasses(
24-
[
25-
'Utils' => __DIR__ . '/library/utils/Utils.php'
26-
]
27-
);
28-
$loader->register();
29-
30-
$console = new Console();
31-
$console->setDI($di);
32-
33-
$arguments = [];
34-
35-
foreach ($argv as $k => $arg) {
36-
if ($k === 1) {
37-
$arguments['task'] = $arg;
38-
} elseif ($k === 2) {
39-
$arguments['action'] = $arg;
40-
} elseif ($k >= 3) {
41-
$arguments['params'][] = $arg;
42-
}
43-
}
44-
45-
try {
46-
$console->handle($arguments);
47-
} catch (\Phalcon\Exception $e) {
48-
fwrite(STDERR, $e->getMessage() . PHP_EOL);
49-
exit(1);
50-
} catch (\Throwable $throwable) {
51-
fwrite(STDERR, $throwable->getMessage() . PHP_EOL);
52-
exit(1);
53-
} catch (\Exception $exception) {
54-
fwrite(STDERR, $exception->getMessage() . PHP_EOL);
55-
exit(1);
56-
}
1+
<?php
2+
3+
$arguments = [];
4+
5+
foreach ($argv as $k => $arg) {
6+
if ($k === 1) {
7+
$arguments['classes'] = $arg;
8+
} elseif ($k === 2) {
9+
$arguments['path'] = $arg;
10+
} elseif ($k === 3) {
11+
$arguments['logfile'] = $arg;
12+
}
13+
}
14+
15+
if (empty($arguments)) {
16+
die("Please type the conversion type, path to the directory or file and (optional) the log file to use");
17+
}
18+
19+
if ($arguments['classes'] === 'v3to4') {
20+
$classes = include_once('v3to4.php');
21+
} else if ($arguments['classes'] === 'v4to5') {
22+
$classes = include_once('v4to5.php');
23+
} else {
24+
die("Conversion " . $arguments['classes'] . " not supported");
25+
}
26+
27+
include_once('adviser.php');
28+
29+
$adviser = new Adviser(
30+
$classes,
31+
$arguments['path'],
32+
$arguments['logfile'] ?? ''
33+
);
34+
35+
$adviser->createLogAction();
36+

v3to4.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
return [
4+
'Phalcon\Acl' => ' Removed ',
5+
'Phalcon\Acl\Adapter' => ' Renamed to Phalcon\Acl\Adapter\AbstractAdapter',
6+
'Phalcon\Acl\Resource' => ' Renamed to Phalcon\Acl\Component',
7+
'Phalcon\Annotations\Adapter' => ' Renamed to Phalcon\Annotations\Adapter\AbstractAdapter',
8+
'Phalcon\Annotations\Adapter\Apc' => ' Removed ',
9+
'Phalcon\Annotations\Adapter\Files' => ' Renamed to Phalcon\Annotations\Adapter\Stream',
10+
'Phalcon\Annotations\Adapter\Xcache' => ' Removed ',
11+
'Phalcon\Annotations\Factory' => ' Renamed to Phalcon\Annotations\AnnotationsFactory',
12+
'Phalcon\Application' => ' Renamed to Phalcon\Application\AbstractApplication',
13+
'Phalcon\Assets\Resource' => ' Renamed to Phalcon\Assets\Asset',
14+
'Phalcon\Assets\Resource\Css' => ' Renamed to Phalcon\Assets\Asset\Css',
15+
'Phalcon\Assets\Resource\Js' => ' Renamed to Phalcon\Assets\Asset\Js',
16+
'Phalcon\Cache\Backend' => ' Renamed to Phalcon\Cache',
17+
'Phalcon\Cache\Backend\Apc' => ' Removed ',
18+
'Phalcon\Cache\Backend\Apcu' => ' Renamed to Phalcon\Cache\Adapter\Apcu',
19+
'Phalcon\Cache\Backend\Factory' => ' Renamed to Phalcon\Cache\AdapterFactory',
20+
'Phalcon\Cache\Backend\File' => ' Renamed to Phalcon\Cache\Adapter\Stream',
21+
'Phalcon\Cache\Backend\Libmemcached' => ' Renamed to Phalcon\Cache\Adapter\Libmemcached',
22+
'Phalcon\Cache\Backend\Memcache' => ' Removed ',
23+
'Phalcon\Cache\Backend\Memory' => ' Renamed to Phalcon\Cache\Adapter\Memory',
24+
'Phalcon\Cache\Backend\Mongo' => ' Removed ',
25+
'Phalcon\Cache\Backend\Redis' => ' Renamed to Phalcon\Cache\Adapter\Redis',
26+
'Phalcon\Cache\Backend\Xcache' => ' Removed ',
27+
'Phalcon\Cache\Exception' => ' Renamed to Phalcon\Cache\Exception\Exception',
28+
'Phalcon\Cache\Frontend\Base64' => ' Removed ',
29+
'Phalcon\Cache\Frontend\Data' => ' Removed ',
30+
'Phalcon\Cache\Frontend\Factory' => ' Removed ',
31+
'Phalcon\Cache\Frontend\Igbinary' => ' Removed ',
32+
'Phalcon\Cache\Frontend\Json' => ' Removed ',
33+
'Phalcon\Cache\Frontend\Msgpack' => ' Removed ',
34+
'Phalcon\Cache\Frontend\None' => ' Removed ',
35+
'Phalcon\Cache\Frontend\Output' => ' Removed ',
36+
'Phalcon\Cache\Multiple' => ' Removed ',
37+
'Phalcon\Config\Factory' => ' Renamed to Phalcon\Config\ConfigFactory',
38+
'Phalcon\Db' => ' Renamed to Phalcon\Db\AbstractDb',
39+
'Phalcon\Db\Adapter' => ' Renamed to Phalcon\Db\Adapter\AbstractAdapter',
40+
'Phalcon\Db\Adapter\Pdo' => ' Renamed to Phalcon\Db\Adapter\PdoFactory',
41+
'Phalcon\Db\Adapter\Pdo\Factory' => ' Renamed to Phalcon\Db\Adapter\Pdo\AbstractPdo',
42+
'Phalcon\Dispatcher' => ' Renamed to Phalcon\Dispatcher\AbstractDispatcher',
43+
'Phalcon\Factory' => ' Renamed to Phalcon\Factory\AbstractFactory',
44+
'Phalcon\Flash' => ' Renamed to Phalcon\Flash\AbstractFlash',
45+
'Phalcon\Forms\Element' => ' Renamed to Phalcon\Forms\Element\AbstractElement',
46+
'Phalcon\Image' => ' Removed ',
47+
'Phalcon\Image\Adapter' => ' Renamed to Phalcon\Image\Adapter\AbstractAdapter',
48+
'Phalcon\Image\Factory' => ' Renamed to Phalcon\Image\ImageFactory',
49+
'Phalcon\Logger\Adapter' => ' Renamed to Phalcon\Logger\Adapter\AbstractAdapter',
50+
'Phalcon\Logger\Adapter\Blackhole' => ' Removed ',
51+
'Phalcon\Logger\Adapter\File' => ' Renamed to Phalcon\Logger\Adapter\Stream',
52+
'Phalcon\Logger\Adapter\Firephp' => ' Removed ',
53+
'Phalcon\Logger\Factory' => ' Renamed to Phalcon\Logger\LoggerFactory',
54+
'Phalcon\Logger\Formatter' => ' Renamed to Phalcon\Logger\Formatter\AbstractFormatter',
55+
'Phalcon\Logger\Formatter\Firephp' => ' Removed ',
56+
'Phalcon\Logger\Multiple' => ' Removed ',
57+
'Phalcon\Mvc\Collection' => ' Renamed to Phalcon\Collection',
58+
'Phalcon\Mvc\Collection\Behavior' => ' Removed ',
59+
'Phalcon\Mvc\Collection\Behavior\SoftDelete' => ' Removed ',
60+
'Phalcon\Mvc\Collection\Behavior\Timestampable' => ' Removed ',
61+
'Phalcon\Mvc\Collection\Document' => ' Removed ',
62+
'Phalcon\Mvc\Collection\Exception' => ' Renamed to Phalcon\Collection\Exception ',
63+
'Phalcon\Mvc\Collection\Manager' => ' Removed ',
64+
'Phalcon\Mvc\Model\Message' => ' Renamed to Phalcon\Messages\Message',
65+
'Phalcon\Mvc\Model\MetaData\Apc' => ' Removed ',
66+
'Phalcon\Mvc\Model\MetaData\Files' => ' Renamed to Phalcon\Mvc\Model\MetaData\Stream',
67+
'Phalcon\Mvc\Model\MetaData\Memcache' => ' Removed ',
68+
'Phalcon\Mvc\Model\MetaData\Session' => ' Removed ',
69+
'Phalcon\Mvc\Model\MetaData\Xcache' => ' Removed ',
70+
'Phalcon\Mvc\Model\Validator' => ' Renamed to Phalcon\Validation\ValidatorFactory',
71+
'Phalcon\Mvc\Model\Validator\Email' => ' Removed ',
72+
'Phalcon\Mvc\Model\Validator\Exclusionin' => ' Removed ',
73+
'Phalcon\Mvc\Model\Validator\Inclusionin' => ' Removed ',
74+
'Phalcon\Mvc\Model\Validator\Ip' => ' Renamed to Phalcon\Validation\Validator\Ip',
75+
'Phalcon\Mvc\Model\Validator\Numericality' => ' Removed ',
76+
'Phalcon\Mvc\Model\Validator\PresenceOf' => ' Removed ',
77+
'Phalcon\Mvc\Model\Validator\Regex' => ' Removed ',
78+
'Phalcon\Mvc\Model\Validator\StringLength' => ' Removed ',
79+
'Phalcon\Mvc\Model\Validator\Uniqueness' => ' Removed ',
80+
'Phalcon\Mvc\Model\Validator\Url' => ' Removed ',
81+
'Phalcon\Mvc\Url' => ' Renamed to Phalcon\Url',
82+
'Phalcon\Mvc\Url\Exception' => ' Renamed to Phalcon\Url\Exception',
83+
'Phalcon\Mvc\User\Component' => ' Renamed to Phalcon\Di\AbstractInjectionAware',
84+
'Phalcon\Mvc\User\Module' => ' Renamed to Phalcon\Di\AbstractInjectionAware ',
85+
'Phalcon\Mvc\User\Plugin' => ' Renamed to Phalcon\Di\AbstractInjectionAware',
86+
'Phalcon\Mvc\View\Engine' => ' Renamed to Phalcon\Mvc\View\Engine\AbstractEngine',
87+
'Phalcon\Paginator\Adapter' => ' Renamed to Phalcon\Paginator\Adapter\AbstractAdapter',
88+
'Phalcon\Paginator\Factory' => ' Renamed to Phalcon\Paginator\PaginatorFactory',
89+
'Phalcon\Queue\Beanstalk' => ' Removed ',
90+
'Phalcon\Queue\Beanstalk\Exception' => ' Removed ',
91+
'Phalcon\Queue\Beanstalk\Job' => ' Removed ',
92+
'Phalcon\Session\Adapter' => ' Renamed to Phalcon\Session\Adapter\AbstractAdapter',
93+
'Phalcon\Session\Adapter\Files' => ' Renamed to Phalcon\Session\Adapter\Stream',
94+
'Phalcon\Session\Adapter\Memcache' => ' Removed ',
95+
'Phalcon\Session\Factory' => ' Renamed to Phalcon\Session\Manager',
96+
'Phalcon\Translate' => ' Removed ',
97+
'Phalcon\Translate\Adapter' => ' Renamed to Phalcon\Translate\Adapter\AbstractAdapter',
98+
'Phalcon\Translate\Factory' => ' Renamed to Phalcon\Translate\TranslateFactory',
99+
'Phalcon\Validation\CombinedFieldsValidator' => ' Renamed to Phalcon\Validation\AbstractCombinedFieldsValidator',
100+
'Phalcon\Validation\Message' => ' Renamed to Phalcon\Messages\Message',
101+
'Phalcon\Validation\Message\Group' => ' Renamed to Phalcon\Messages\Messages',
102+
'Phalcon\Validation\Validator' => ' Renamed to Phalcon\Validation\AbstractValidator'
103+
];

0 commit comments

Comments
 (0)