Skip to content

Commit 9bf5a0b

Browse files
author
Webmaster
committed
Add tools for baking Opencart tables in Model files
1 parent 5b50277 commit 9bf5a0b

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

tools/bake.sh

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/bin/bash
2+
3+
VER=$1
4+
if [ -z $VER ]; then
5+
echo "Provide version"
6+
exit 255
7+
fi
8+
9+
if [ ! -d "vendor/cakephp/bake" ]; then
10+
echo "First, \`cd\` to a CakePHP project folder. Currently in: $(pwd)"
11+
exit 255
12+
fi
13+
14+
INFLECTOR="$(dirname ${0})/inflector.php"
15+
if [ ! -f "${INFLECTOR}" ]; then
16+
echo "The $(basename ${INFLECTOR}) script needs to be available in the same folder with this script. Checked for: ${INFLECTOR}"
17+
exit 255
18+
fi
19+
20+
read -p "Name of the plugin to hold Opencart connector code: " PLUGIN
21+
22+
# Shorthand
23+
E="plugins/${PLUGIN}/src/Model/Entity"
24+
T="plugins/${PLUGIN}/src/Model/Table"
25+
26+
# Clean up
27+
rm -rf ${E}/Opencart${VER}
28+
mkdir -p ${E}/Opencart${VER}
29+
#rm -rf ${E}/OpencartCommon # DON'T. oc2 has some classes removed in oc4
30+
mkdir -p ${E}/OpencartCommon
31+
rm -rf ${T}/Opencart${VER}
32+
mkdir -p ${T}/Opencart${VER}
33+
34+
# Get the list of all bake-able models. Results will be in CamelCase
35+
LIST=$(bin/cake bake model --connection opencart${VER}-clean | tail -n +2 -)
36+
# Loop through each table
37+
for NAME in ${LIST}; do
38+
if [ "${NAME}" = "-" ]; then
39+
continue;
40+
fi
41+
# Shorthand
42+
ALIAS=$(php "${INFLECTOR}" pluralize ${NAME})
43+
ENTITY=${NAME}
44+
# Only one exception for OC4, all other tables are singular
45+
if [ "${ENTITY}" = "Statistics" ]; then
46+
ENTITY="Statistic"
47+
fi
48+
TABLE=$(php "${INFLECTOR}" underscore ${NAME})
49+
printf "\n"
50+
echo "NAME:${NAME} ALIAS:${ALIAS} TABLE:${TABLE} ENTITY:${ENTITY}"
51+
# Do the actual baking
52+
bin/cake bake model ${ALIAS} --table ${TABLE} --force --no-fixture --no-test --plugin ${PLUGIN} --connection opencart${VER}-clean
53+
54+
# Handle the Table class
55+
mv "${T}/${ALIAS}Table.php" "${T}/Opencart${VER}/${ALIAS}Table.php"
56+
F="${T}/Opencart${VER}/${ALIAS}Table.php"
57+
sed -i "s/${PLUGIN}\\\Model\\\Table/${PLUGIN}\\\Model\\\Table\\\Opencart${VER}/g" "${F}"
58+
# Remove hardcoded connection name, look up the method and lines around it
59+
awk '/defaultConnectionName/ {print NR-5 "," NR+3 "d"}' "${F}" \
60+
| sed -i -f - "${F}"
61+
62+
# Descriptions will not be picked up automatically, handle them here
63+
if [ "${NAME:(-11)}" = "Description" ]; then
64+
# Convert NAME to plural (table alias) and underscore (table name)
65+
TRANSLATABLE="${NAME%"Description"}"
66+
TRANSLATABLE_ALIAS=$(php "${INFLECTOR}" pluralize ${TRANSLATABLE})
67+
TRANSLATABLE_TABLE=$(php "${INFLECTOR}" underscore ${TRANSLATABLE})
68+
# Create the snippet we will read from
69+
cat > "/tmp/descriptions_snippet" << EOF
70+
\$this->hasMany('${ALIAS}', [
71+
'foreignKey' => '${TRANSLATABLE_TABLE}_id',
72+
'className' => '${PLUGIN}.${ALIAS}',
73+
]);
74+
\$this->hasOne('${NAME}', [
75+
'foreignKey' => '${TRANSLATABLE_TABLE}_id',
76+
'className' => '${PLUGIN}.${NAME}',
77+
'conditions' => [
78+
// to be populated by ${PLUGIN}\Connector
79+
],
80+
]);
81+
EOF
82+
83+
awk '
84+
/}/ && !done { # Search for the first "}" and check if insertion is not done
85+
while ((getline line < "/tmp/descriptions_snippet") > 0) {
86+
print line # Print each line from the snippet file
87+
}
88+
print " }" # Print the original "}" after the snippet
89+
done=1 # Set done to 1 to avoid further insertions
90+
next # Skip the original line containing "}"
91+
}
92+
{ print } # Print all other lines as is
93+
' "${T}/Opencart${VER}/${TRANSLATABLE_ALIAS}Table.php" > /tmp/descriptions_tmp && mv /tmp/descriptions_tmp "${T}/Opencart${VER}/${TRANSLATABLE_ALIAS}Table.php"
94+
fi
95+
96+
# Handle the Entity class
97+
mv "${E}/${ENTITY}.php" "${E}/Opencart${VER}/${ENTITY}.php"
98+
F="${E}/Opencart${VER}/${ENTITY}.php"
99+
sed -i "s/${PLUGIN}\\\Model\\\Entity/${PLUGIN}\\\Model\\\Entity\\\Opencart${VER}/g" "${F}"
100+
# Entity: common
101+
mkdir -p "${E}/OpencartCommon"
102+
if [ ! -f "${E}/OpencartCommon/${ENTITY}.php" ]; then
103+
cat > "${E}/OpencartCommon/${ENTITY}.php" << EOF
104+
<?php
105+
106+
namespace ${PLUGIN}\Model\Entity\OpencartCommon;
107+
108+
use Cake\ORM\Entity;
109+
110+
class ${ENTITY} extends Entity
111+
{
112+
113+
114+
115+
}
116+
EOF
117+
fi
118+
sed -i "s/class ${ENTITY} extends Entity/class ${ENTITY} extends \\\\${PLUGIN}\\\Model\\\Entity\\\OpencartCommon\\\\${ENTITY}/g" "${F}"
119+
# Delete `use Cake\ORM\Entity;` and the empty line after it
120+
sed -i '/^use Cake\\\ORM\\\Entity;/{N;d;}' "${F}"
121+
done

tools/inflector.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
require('vendor/cakephp/cakephp/src/Utility/Inflector.php');
4+
5+
$inflector = new \Cake\Utility\Inflector();
6+
7+
echo $inflector::{$argv[1]}($argv[2]).PHP_EOL;

0 commit comments

Comments
 (0)