`./gen-repos.sh <BUNDLE-DIR>` Creates repositories for you
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DIR="${1}"; | |
if [ -z "$DIR" ] || [ ! -d "$DIR/Entity" ]; then | |
echo "Given argument is or empty, or is not a valid Bundle directory"; | |
exit 1; | |
fi | |
REPO_DIR="$DIR/Repository"; | |
CONFIG="$DIR/Resources/config/repositories.yml"; | |
test -d "$REPO_DIR" || mkdir -p "$REPO_DIR" || exit 1; | |
echo "services:" > $CONFIG | |
find "$DIR/Entity" -name "*.php" -print0 | while read -d $'\0' file; do | |
NAMESPACE="$(cat "$file" | grep -m1 -oE 'namespace\s[^;]+' | awk '{print $2}' | sed 's:Entity:Repository:')"; | |
ENTITY_NAME="$(cat "$file" | grep -m1 -oE 'class\s[^\}]+' | awk '{print $2}')"; | |
REPO_FILENAME="$(basename "$file" | sed 's:\.php$:Repository.php:')"; | |
echo " $NAMESPACE\\${ENTITY_NAME}Repository:" >> $CONFIG | |
echo " factory: ['@doctrine.orm.entity_manager', getRepository]" >> $CONFIG | |
echo " lazy: true" >> $CONFIG | |
echo " arguments:" >> $CONFIG | |
echo " - $(sed 's:Repository:Entity:' <<< $NAMESPACE)\\${ENTITY_NAME}" >> $CONFIG; | |
echo >> $CONFIG; | |
sed -i 's:@ORM\\Entity\(()\)\?:@ORM\\Entity(repositoryClass="'"$(sed -e 's:[]\:$*.^[]:\\&:g' <<< "${NAMESPACE}\\${ENTITY_NAME}")"'Repository"):' $file; | |
[ -f "$REPO_DIR/$REPO_FILENAME" ] && continue; | |
cat <<EOF > "$REPO_DIR/$REPO_FILENAME"; | |
<?php | |
namespace $NAMESPACE; | |
use Doctrine\\ORM\\EntityRepository; | |
class ${ENTITY_NAME}Repository extends EntityRepository { | |
} | |
EOF | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment