Skip to content

Instantly share code, notes, and snippets.

@the-eater
Created February 13, 2019 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-eater/4da205c90482d20192950a75b59f107c to your computer and use it in GitHub Desktop.
Save the-eater/4da205c90482d20192950a75b59f107c to your computer and use it in GitHub Desktop.
`./gen-repos.sh <BUNDLE-DIR>` Creates repositories for you
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